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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
| <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewprot" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>ddd</title>
<style type="text/css">
#wrapper {
width: 400px;
height: 300px;
border: 1px solid black;
/*box-sizing: border-box;*/
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
overflow: hidden;
}
#father{
position: absolute;
top: 0;
left: 0;
width: 500%;
height: 100%;
font-size: 0;
}
#father>div{
float: left;
font-size: 20px;
width: 20%;
height: 100%;
line-height: 300px;
text-align: center;
}
#item1{
background-color: #f00;
}
#item2{
background-color: #ff0;
}
#item3{
background-color: #f0f;
}
#item4{
background-color: #00f;
}
#item5{
background-color: #f00;
}
#btn1,#btn2{
display: block;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
border-radius: 50% 50%;
background-color: #EEEEEE;
text-align: center;
line-height: 50px;
font-size: 20px;
font-weight: bold;
opacity: 0.3;
cursor: pointer;
}
#btn1:hover,#btn2:hover{
background-color: #333;
color: #eee;
opacity: 1;
}
#btn1 {
left: 10px;
}
#btn2 {
right: 10px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="father">
<div id="item1">1</div>
<div id="item2">2</div>
<div id="item3">3</div>
<div id="item4">4</div>
<div id="item5">1</div>
</div>
<span id="btn1"><</span>
<span id="btn2">></span>
</div>
<script type="text/javascript">
// 首先要解决一个元素的运动问题.
// 运动是什么, 是状态的变化, 是数据有规律的变化
// 是数据有规律的模拟自然的变化
// 一定要搞清楚 哪个数据是基础.有些数据是作为另一个数据的判断条件的.
var len = father.children.length;// 有几个子元素
var width = father.getBoundingClientRect().width / len; // 一个子元素的长度
function move (ele,target,duration,direction,callback) {// 匀速运动
var icur;// 当前位置
var duration = duration || 20;//计时器刷新间隔
var speed = speed || 5;// 速度//流畅度?
var direction = direction || 'left';// 方向
if(direction === 'right') {
speed = -Math.abs(speed);// 用速度的正负值控制运动方向
}else {
speed = Math.abs(speed);
}
clearInterval(ele.timer);//防止重复调用引起的计时器叠加
ele.timer = setInterval(function () {
icur = ele.offsetLeft;//当前位置
icur -= speed;//要到达的位置
if(Math.abs(icur - target) <= Math.abs(speed)){//运动停止
clearInterval(ele.timer);
icur = target;
callback(ele,direction);//回调函数
}
ele.style.left = icur + 'px';//渲染
},duration);//
}
function quickmove (father,target) {// 快速运动
father.style.left = target + 'px';
}
// 根据现在的位置和方向,决定下一个运动的位置.
var goToTarget = (function () {// 用来获得下一个运动需要到达的地点.
var direction = 'left'; //故意用了立即函数放了个私有变量,结构有点丑.
return function (directiona) {
var target;
var icur = parseInt(window.getComputedStyle(father,null).left);
direction = directiona || direction;//存改变的方向
if(direction == 'left'){
if(icur == -width * (len - 1)){
target = 0;
return {target,wich : 'quickmove',direction};//wich用来决定调用哪个运动函数
}else {
return {target : icur - width,wich : 'move',direction} ;
}
} else {
if(icur == 0){
target = -width * (len - 1);
return {target,wich : 'quickmove',direction};
}else {
return {target : icur + width,wich : 'move',direction} ;
}
}
}
})();
function recurse (ele,direction) {// 循环
clearTimeout(ele.time2);
ele.time2 = setTimeout(function () {
var obj = goToTarget(direction);
while(obj.wich == 'quickmove') {
quickmove(ele,obj.target);
obj = goToTarget();
}
move(ele,obj.target,20,obj.direction,recurse);
},1000);
}
function init () {
recurse(father,'left');
bindEvent();
}
function bindEvent () {
btn1.onclick = function (e) {
recurse(father,'left');
}
btn2.onclick = function (e) {
recurse(father,'right');
}
}
init();
</script>
</body>
</html>
|