-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.html
195 lines (166 loc) · 6.93 KB
/
carousel.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
191
192
193
194
195
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="./carousel.css">
</head>
<body>
<div class="carousel">
<div class="main animateLeft" id="carousel-image-container">
<img src="images/5.jpg" alt="">
<img src="images/1.jpg" alt="">
<img src="images/2.jpg" alt="">
<img src="images/3.jpg" alt="">
<img src="images/4.jpg" alt="">
<img src="images/5.jpg" alt="">
<img src="images/1.jpg" alt="">
</div>
<ul class="carousel-navigator clearfix">
<div class="container">
<li>
<button id="carousel-prev-button"><</button>
</li>
<li>
<button id="carousel-next-button">></button>
</li>
</div>
</ul>
<ul class="carousel-buttons">
<li>
<button class="carousel-button-active" id="carousel-buttons-1"></button>
</li>
<li>
<button id="carousel-buttons-2"></button>
</li>
<li>
<button id="carousel-buttons-3"></button>
</li>
<li>
<button id="carousel-buttons-4"></button>
</li>
<li>
<button id="carousel-buttons-5"></button>
</li>
</ul>
</div>
<script src="scripts/observer.js"></script>
<script>
var config = {
};
(function(window, config) {
var document = window.document;
var $ = document.querySelector.bind(document);
var buttonsContainer = $('.carousel-buttons');
var currentImg = 0;
function extend(child, parent) {
function Ctor() {parent.constructor = child;}
Ctor.prototype = parent.prototype;
child.prototype = new Ctor();
return child;
}
function ImgView(selector) {
this.imagesContainer = selector || $('#carousel-image-container');
this.imgLength = this.imagesContainer.children.length - 2;
this.lastImgIndex = 0;
}
extend(ImgView, OB);
ImgView.prototype.getContainerWidth = function() {
// 为了以后的响应式设计, 这里没有做缓存, 也许会很浪费资源
return parseInt(getComputedStyle($('.carousel')).width);
};
ImgView.prototype.moveHorizontal = function(target, offset, speed, callback) {
offset = parseInt(offset);
var start = null;
var from = parseInt(window.getComputedStyle(target).left);
function handler(ts) {
if (!start) start = ts;
var process = ts - start;
var distance;
if (offset > 0) {
distance = Math.min(process * speed, offset);
} else {
distance = -Math.min(process * speed, -offset);
}
target.style.left = from + distance + 'px';
if (process * speed < Math.abs(offset)) {
window.animateId = window.requestAnimationFrame(handler.bind(this));
} else if(callback) {
window.cancelAnimationFrame(window.animateId, callback.bind(this));
// 动画结束, 运行回调函数
callback.bind(this);
}
}
window.requestAnimationFrame(handler.bind(this));
};
function ButtonView() {};
extend(ButtonView, OB);
var imgView = new ImgView();
var buttonView = new ButtonView();
// 绑定事件
imgView.on('nav', function(currentIndex) {
var imgWidth = this.getContainerWidth();
var currentPos = 0;
// 做两端越界判断
if (this.lastImgIndex === this.imgLength - 1 && currentIndex === 0) {
// 从最后一张跳到第一张
currentPos = - (this.imgLength + 1) * imgWidth;
this.imagesContainer.style.left = currentPos + 'px';
} else if (this.lastImgIndex === 0 && currentIndex === this.imgLength - 1) {
// 从第一张跳到最后一张
currentPos = 0;
this.imagesContainer.style.left = currentPos + 'px';
} else {
// 普通导航
// +1是因为第一个img是过度图片, 我们应该从第二张开始展示
currentPos = - (currentIndex + 1) * imgWidth;
this.imagesContainer.style.left = currentPos + 'px';
}
// 做一些判断, 图片是否在过度位置
// 因为页面是异步更新的, 这里left获得的其实是处理前的代码, 即上面代码是无效的, 我们需要手动更新, 已经在上述代码中修改好了..
// 当前在最后一张的过渡照片, 放回适合的地方
// 先去掉动画
this.imagesContainer.classList.remove('animateLeft');
if (currentPos >= 0) {
this.imagesContainer.style = this.imagesContainer.style.left = -(this.imgLength) * imgWidth + 'px';
}
// 在第一张照片的过渡照片处, 回到第一张
else if (currentPos <= -(this.imgLength + 1) * imgWidth) {
this.imagesContainer.style.left = -imgWidth + 'px';
}
this.imagesContainer.classList.add('animateLeft');
this.lastImgIndex = currentIndex;
});
buttonView.on('nav', function(index) {
changeButtonActive(index);
});
function changeButtonActive(index) {
var length = buttonsContainer.children.length;
for (var i = 0; i < length; i++) {
buttonsContainer.children[i].children[0].classList.remove('carousel-button-active');
}
buttonsContainer.children[index].children[0].classList.add('carousel-button-active');
}
var navigatorGroup = $('.carousel-navigator');
var ifPressed = false;
navigatorGroup.addEventListener('click', function(event) {
if (ifPressed) return;
else ifPressed = true;
if (event.target.matches('#carousel-next-button')) {
currentImg = (currentImg + 1) % imgView.imgLength;
imgView.emmit('nav', currentImg);
buttonView.emmit('nav', currentImg);
}
else if (event.target.matches('#carousel-prev-button')) {
currentImg === 0 ? currentImg = imgView.imgLength - 1 : currentImg--;
imgView.emmit('nav', currentImg);
buttonView.emmit('nav', currentImg);
}
setTimeout(function() {
ifPressed = false;
}, 100); // TODO config!
});
})(window, config);
</script>
</body>
</html>