/
task27-3 kai.html
190 lines (181 loc) · 6.23 KB
/
task27-3 kai.html
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
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>append$prepend挪动DOM轮播</title>
</head>
<style type="text/css">
*{
padding:0;
margin: 0;
}
body{
background-color: #666;
}
a{
text-decoration: none;
}
li{
list-style: none;
}
.wrap{
position: relative;
margin: 100px auto;
width: 400px;
height: 300px;
overflow: hidden;
}
.wrap .ct {
position: absolute;
/*如果这里不设置absolute的话,图片队列不能横过来了*/
}
.ct li img{
float: left;
width: 400px;
height: 300px;
}
.arrow{
position: absolute;
top:50%;
margin-top: -20px;
background-color: #666666;
opacity: 0.7;
height: 40px;
width: 40px;
border-radius: 50%;
line-height: 40px;
color: #fff;
text-align: center;
}
.pre{
left: 2%;
}
.next{
right: 2%;
}
.bullet{
position: absolute;
top:90%;
left: 50%;
transform:translateX(-50%);
}
.bullet li{
display: inline-block;
width: 25px;
height: 7px;
background-color: #666;
margin-left: 10px;
-webkit-border-radius: 40px;
-moz-border-radius: 40px;
border-radius: 40px;
}
.bullet li.active{
background-color: #fff;
}
.clearfix{
content:'';
clear: both;
display: block;
}
</style>
<body>
<div class="wrap">
<ul class="ct clearfix">
<li><img data-img='0' src="img/tifa.png"></li>
<li><img data-img='1' src="img/cloud.png"></li>
<li><img data-img='2' src="img/yuffie.png"></li>
<li><img data-img='3' src="img/barret.png"></li>
</ul>
<a class="arrow pre" href="#"><</a>
<a class="arrow next" href="#">></a>
<ul class="bullet clearfix">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
var $wrap = $('.wrap'),
$ct = $('.ct'),
$pre = $('.pre'),
$next = $('.next'),
$items = $ct.children(),
$bullet = $('.bullet'),
imgWidth = $items.width(),
imgCount = $ct.children().length;
var isAnimate = false,//状态锁
nowIdx = 0;//这里可以控制一开始的时候,需要展现哪个图
$ct.css({width: imgWidth * imgCount});
$pre.on('click',function () {
playPre();
});
$next.on('click',function () {
playNext();
});
$bullet.find('li').on('click',function(){
var idx = $(this).index();
if (idx < nowIdx){
playPre(nowIdx-idx);
}
else if (idx > nowIdx){
playNext(idx-nowIdx);
}
});
function playPre (idx) {
var idx = idx || 1;
if(!isAnimate){
isAnimate = true;
//一开始只考虑到了点击pre和next时prepend last 和 append first,
// 但是这样每次只能满足移动一次,所以就用了遍历,正好符合挪动的次数
for(var i = 0;i < idx;i++){
$ct.children().last().prependTo($ct);
}
$ct.css('left',-(imgWidth*idx));
//因为.wrap是一个镜头,是不动的,这个镜头一开始对准了nowIdx图,要发生向前移动时,
//会先抽走.ct的最后一个li的DOM,添加到最前面的时候,相当于将原来镜头对准的图向后顺延了一样,
//其实这里就已经够了,css操作和animate操作都可以不要了,就是动画效果是闪现的,为什么是闪现的?
//这类似插队一样,后面的跑到前面来,本来一个固定的镜头看着你,就会因为顺位变化,镜头就会看着你的前面一个人
//但是这种效果是是DOM操作,特别快,造成闪现,所以需要用.css这种也很快的方法给修补回来
//这种修补类似与把你的整个队伍向前推动一个距离,让你再次回到镜头下面,一切发生的太快,镜头依旧看着你,仿佛没有插队
//接下来运行到animate操纵整个队伍,向右边缓慢移动,让镜头慢慢对准你前面的那一位,就出现了一个过渡的动画
$ct.animate({ left:'+=' + (imgWidth*idx) },function () {
nowIdx = (imgCount+nowIdx-idx)%imgCount;
setBullet();
isAnimate = false;
});
}
}
function playNext(idx){
var idx = idx || 1;
if(!isAnimate){
isAnimate = true;
$ct.animate({ left:'-=' + (imgWidth*idx) },function () {
nowIdx = (nowIdx+idx)%imgCount;
for(var i = 0; i < idx; i++){
$ct.children().first().appendTo($ct);
}
$ct.css('left',0);
//相当于animate把整个图片队列向左边推动之后,需要重新用css操作DOM极快的把队伍拉回来
//如果不拉回来,镜头里面就会显示,动画效果挪动到你需要的图片上,但是瞬间闪到下idx个图上
//因为DOM在appendTo之后,镜头会对准重组之后的队列里面本应该对着的下idx个图,如果不修正,图就会跑没了
//为了避免这种状况,需要重新将队伍给拉回来,让nowIdx的图回到固定镜头.wrap下面
setBullet();
isAnimate = false;
});
}
}
function setBullet(){
$bullet.find('li').removeClass('active').eq(nowIdx).addClass('active');
console.log('nowIdx:'+nowIdx);
}
function autoPlay () {
setInterval(function(){
playNext();
}, 2500);
}
autoPlay();
</script>
</body>
</html>