-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslider.js
More file actions
225 lines (157 loc) · 5.58 KB
/
slider.js
File metadata and controls
225 lines (157 loc) · 5.58 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//slider.js
;(function(_){
// 将HTML转换为节点
function html2node(str){
var container = document.createElement('div');
container.innerHTML = str;
return container.children[0];
}
var template =
'<div class="m-slider">\
<div class="slide"></div>\
<div class="slide"></div>\
<div class="slide"></div>\
</div>';
function Slider(opt){
_.extend(this, opt);
//节点及样式设置
this.container =document.getElementById('bannerBody');
this.container.style.overflow = 'hidder';
//组件节点
this.slider = this._layout.cloneNode(true);
this.slides = [].slice.call(this.slider.querySelectorAll('.slide'));
//拖拽
this.offsetWidth = this.container.offsetWidth;
this.breakPoint = this.offsetWidth / 4;
this.pageNum = this.images.length;
//内部数据结构
this.slideIndex = 1;
this.pageIndex = this.pageIndex || 0;
this.offsetAll = this.pageIndex;
// this.pageNum 必须传入
// 初始化动作
this.container.appendChild(this.slider);
// 如果需要拖拽切换
if(this.drag) this._initDrag();
}
_.extend(Slider.prototype, _.emitter);
_.extend( Slider.prototype, {
_layout: html2node(template),
//直接跳转到指定页
nav: function(pageIndex){
this.pageIndex = pageIndex;
this.slideIndex = typeof this.slideIndex === 'number' ? this.slideIndex: (pageIndex+1)%3;
this.offsetAll = pageIndex;
this.slider.style.transitionDuration = '0s';
this._calcSlide();
},
//next
next: function(){
this._step(1);
},
//上一页
prev:function(){
this._step(-1);
},
// // 单步移动
_step(offset){
this.pagfeIndex += offset;
this.offsetAll += offset;
this.slideIndex += offset;
this.slider.style.transitionDuration = '.5s';
this._calcSlide();
},
// 计算Slide
// 每个slide的left = (offsetAll + offset(1, -1)) * 100%;
// 外层容器 (.m-slider) 的偏移 = offsetAll * 宽度
_calcSlide: function(){
var slideIndex = this.slideIndex = this._normIndex(this.slideIndex, 3),
pageIndex = this.pageIndex = this._normIndex(this.pageIndex, this.pageNum),
offsetAll = this.offsetAll,
pageNum = this.pageNum;
var prevSlideIndex = this._normIndex(slideIndex -1, 3),
nextSlideIndex = this._normIndex(slideIndex +1, 3);
var slides = this.slides;
slides[slideIndex].style.left = (offsetAll) * 100 + '%';
slides[prevSlideIndex].style.left = (offsetAll -1) * 100 + '%';
slides[nextSlideIndex].style.left = (offsetAll +1) * 100 + '%';
// 容器偏移
this.slider.style.transform = 'translateX(' + (- offsetAll * 100) +'%) translateZ(0)';
// 当前slide 添加 'z-active'的className
slides.forEach(function(node){
console.log(node.className);
_.delClass(node, 'z-active');
})
_.addClass(slides[slideIndex], 'z-active');
this._onNav(this.pageIndex, this.slideIndex);
},
// 标准化下标
_normIndex: function(index, len){
return (len + index) % len;
},
// 跳转时完成的逻辑, 这里是设置图片的url
_onNav: function( pageIndex, slideIndex){
var slides = this.slides;
console.log("slideIndex:"+slideIndex+" pageIndex: "+pageIndex)
for(var i = -1; i<=1; i++){
var index = (slideIndex + i + 3) %3;
var img = slides[index].querySelector('img');
if(!img){
img = document.createElement('img');
slides[index].appendChild(img);
}
img.src = './imgs/banner'+ (this._normIndex(pageIndex + i, this.pageNum)+1)+ '.jpg';
}
this.emit('nav', {
pageIndex: pageIndex,
slideIndex: slideIndex
})
},
// // 拖动相关, 有兴趣的同学
// // ----------
_initDrag: function(){
this._dragInfo = {};
this.slider.addEventListener('mousedown', this._dragstart.bind(this));
this.slider.addEventListener('mousemove', this._dragmove.bind(this));
this.slider.addEventListener('mouseup', this._dragend.bind(this));
this.slider.addEventListener('mouseleave', this._dragend.bind(this));
},
_dragstart: function(ev){
var dragInfo = this._dragInfo;
dragInfo.start = {x: ev.pageX, y: ev.pageY};
},
_dragmove: function(ev){
var dragInfo = this._dragInfo;
// 如果还没有开始拖拽则退出
if(!dragInfo.start) return;
ev.preventDefault();
this.slider.style.transitionDuration = '0s';
var start = dragInfo.start;
// 清除恼人的选区
if (window.getSelection) {
window.getSelection().removeAllRanges();
} else if (window.document.selection) {
window.document.selection.empty();
}
// 加translateZ 分量是为了触发硬件加速
this.slider.style.transform =
'translateX(' + (-(this.offsetWidth * this.offsetAll - ev.pageX+start.x)) + 'px) translateZ(0)'
},
_dragend: function( ev ){
var dragInfo = this._dragInfo;
if(!dragInfo.start) return;
ev.preventDefault();
var start = dragInfo.start;
this._dragInfo = {};
var pageX = ev.pageX;
// 看走了多少距离
var deltX = pageX - start.x;
if( Math.abs(deltX) > this.breakPoint ){
this._step(deltX>0? -1: 1)
}else{
this._step(0)
}
}
})
window.Slider = Slider;
})(util);