本文实例为大家分享了js实现缓动动画的具体代码,供大家参考,具体内容如下
利用定时器来控制元素的offsetLeft的值,offsetLeft = 开始位置 + (最终位置 - 开始位置)* 缓动系数
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >Title</ title >
< style >
* {
margin: 0;
padding: 0;
border: none;
list-style: none;
}
body {
background-color: pink;
}
#nav {
width: 900px;
height: 63px;
background: url("images/doubleOne.png") no-repeat right center #fff;
margin: 0 auto;
margin-top: 50px;
border-radius: 5px;
position: relative;
}
#nav ul {
line-height: 70px;
}
#nav ul li {
float: left;
height: 63px;
width: 88px;
text-align: center;
cursor: pointer;
position: relative;
}
#t_mail {
width: 88px;
height: 63px;
background: url("images/tMall.png") no-repeat;
position: absolute;
}
</ style >
</ head >
< body >
< nav id = "nav" >
< span id = "t_mail" ></ span >
< ul >
< li >双11狂欢</ li >
< li >服装会场</ li >
< li >数码家电</ li >
< li >母婴童装</ li >
< li >手机会场</ li >
< li >美妆会场</ li >
< li >家居建材</ li >
< li >进口会场</ li >
< li >飞猪旅行</ li >
</ ul >
</ nav >
< script >
window.onload = function () {
var nav = $('nav');
var t_mall = nav.children[0];
var ul = nav.children[1];
var allLis = ul.children;
var beginX = 0;
for (var i = 0; i < allLis.length ; i++) {
var li = allLis [i];
li.onmouseover = function () {
end = this .offsetLeft;
}
li.onmousedown = function () {
beginX = this .offsetLeft;
}
li.onmouseout = function () {
end = beginX ;
}
}
var begin = 0 , end = 0 ;
setInterval(function () {
begin = begin + (end - begin) / 10;
t_mall.style.left = begin + 'px';
}, 10)
function $(id) {
return typeof id ? document.getElementById(id) : null;
}
}
</script>
</ body >
</ html >
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/ojb98K/article/details/106972102