弹性运动写的滑动菜单

本文最后更新于:2023年3月30日 下午

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

<style>
* { padding: 0; margin: 0; }
li { list-style: none; }

ul { width: 400px; height: 30px; position: relative; margin: 100px auto 0; }
li { float: left; width: 98px; height: 28px; line-height: 28px; border: 1px solid #ccc; text-align: center; z-index: 2; position: relative; cursor: pointer; }
.bg { width: 100px; height: 5px; overflow: hidden; background: red; border: none; position: absolute; top: 24px; left: 0; z-index: 1; }
</style>
<script type="text/javascript">
window.onload=function (){
var oUl = document.getElementById('ul1');
var oLi = oUl.getElementsByTagName('li');
var oBg = oLi[oLi.length - 1];
for(var i = 0; i < oLi.length - 1; i++) {
oLi[i].onmouseover = function() {
startMove(oBg,this.offsetLeft);
}
}
}
var ispeed = 0;
var left = 0;
function startMove(obj, iTarget) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
ispeed += (iTarget - obj.offsetLeft) / 5;
ispeed *= 0.7;
left += ispeed;
if(Math.abs(ispeed) < 1 && Math.abs(left - iTarget) < 1) {
ispeed = 0;
obj.style.left = iTarget + 'px';
clearInterval(obj.timer);
}else{
obj.style.left = left + 'px';
}
document.title = obj.offsetLeft + ' | ' + iTarget +' | ' + left;
}, 30);
}
</script>
</head>
<body>
<ul id="ul1">
<li>首页</li>
<li>关于我们</li>
<li>产品</li>
<li>联系方式</li>
<li class="bg"></li>
</ul>
</body>
</html>

查看演示


弹性运动写的滑动菜单
https://seven3.site/js/弹性运动写的滑动菜单/
作者
Seven3s
发布于
2015年1月29日
许可协议