|
25 | 25 | delay:5000, |
26 | 26 | direction: 'horizontal', |
27 | 27 | autoplay: true, |
28 | | - bounceRatio:0.3, |
| 28 | + bounceRatio:0.1, |
29 | 29 | pagination:true, |
30 | 30 | paginationClass:'slider-pagination', |
31 | 31 | bulletClass:'slider-bullet', |
|
48 | 48 | } |
49 | 49 | return source; |
50 | 50 | } |
| 51 | + var Device = (function () { |
| 52 | + var ua = navigator.userAgent; |
| 53 | + var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/); |
| 54 | + var ipad = ua.match(/(iPad).*OS\s([\d_]+)/); |
| 55 | + var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/); |
| 56 | + var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/); |
| 57 | + return { |
| 58 | + ios: ipad || iphone || ipod, |
| 59 | + android: android, |
| 60 | + desktop:!(ipad || iphone || ipod || android) |
| 61 | + }; |
| 62 | + })(); |
| 63 | + |
| 64 | + function translate3d(element,x,y) { |
| 65 | + x = x === undefined ? 0 : x; |
| 66 | + y = y === undefined ? 0 : x; |
| 67 | + element.style['-webkit-transform'] = 'translate3d(-'+x+'px,'+y+'px,0px)'; |
| 68 | + element.style['transform'] = 'translate3d(-'+x+'px,'+y+'px,0px)'; |
| 69 | + } |
| 70 | + |
| 71 | + function transition(element,time){ |
| 72 | + element.style['-webkit-transition-duration'] = time+'ms'; |
| 73 | + element.style['transition-duration'] = time+'ms'; |
| 74 | + } |
51 | 75 |
|
52 | 76 | function Slider( selector, options ) { |
53 | 77 | options = extend(defaults,options); |
|
71 | 95 | this.sliderCount = sliderCount; |
72 | 96 | this.lastIndex = sliderCount - 1; |
73 | 97 | this.firstIndex = 0; |
| 98 | + this.isAnimating = false; |
| 99 | + this.axis = {x:0,y:0}; |
74 | 100 | this.initSlides(); |
| 101 | + this.bindEvents(); |
75 | 102 | } |
76 | 103 |
|
77 | 104 | var fn = Slider.init.prototype; |
|
80 | 107 | var width = this.wrap.clientWidth; |
81 | 108 | this.slideWidth = width; |
82 | 109 | this.wrap.style.width = width * this.sliderCount + 'px'; |
| 110 | + this.bounceWidth = this.params.bounceRatio * width; |
83 | 111 | this.activeIndex = 0; |
84 | 112 | this.slideStack = []; |
85 | 113 | for(var i = 0;i<this.sliderCount;i++){ |
|
118 | 146 | this.lastIndex += 2; |
119 | 147 | } |
120 | 148 |
|
| 149 | + fn.fixedLoop = function(activeIndex){ |
| 150 | + if(!this.params.loop) return; |
| 151 | + translate3d(this.wrap,this.slideWidth*activeIndex); |
| 152 | + transition(this.wrap,0); |
| 153 | + this.activeIndex = activeIndex; |
| 154 | + this.isAnimating = false; |
| 155 | + } |
| 156 | + |
121 | 157 | fn.fixedNextLoop = function(){ |
122 | 158 | var that = this; |
123 | | - var x = this.slideWidth; |
124 | 159 | setTimeout(function(){ |
125 | | - translate3d(that.wrap,x); |
126 | | - transition(that.wrap,0); |
127 | | - that.activeIndex = 1; |
| 160 | + that.fixedLoop(1) |
128 | 161 | },that.params.speed); |
129 | 162 | } |
130 | 163 |
|
131 | 164 | fn.fixedPrevLoop = function(){ |
132 | 165 | var that = this; |
133 | | - var x = this.slideWidth*(this.lastIndex-1); |
134 | 166 | setTimeout(function(){ |
135 | | - translate3d(that.wrap,x); |
136 | | - transition(that.wrap,0); |
137 | | - that.activeIndex = that.lastIndex-1; |
| 167 | + that.fixedLoop(that.lastIndex-1) |
138 | 168 | },that.params.speed); |
139 | 169 | } |
140 | 170 |
|
|
186 | 216 | this.params.autoplay && clearTimeout(this.timeId); |
187 | 217 | return; |
188 | 218 | } |
| 219 | + this.isAnimating = true; |
189 | 220 | translate3d(this.wrap,activeIndex*this.slideWidth); |
190 | 221 | transition(this.wrap,this.params.speed); |
191 | 222 | this.setActivePagination(); |
192 | 223 | if(activeIndex === this.lastIndex){ |
193 | | - this.params.loop && this.fixedNextLoop(); |
| 224 | + this.fixedNextLoop(); |
194 | 225 | } |
195 | 226 | } |
196 | 227 |
|
|
200 | 231 | this.params.autoplay && clearTimeout(this.timeId); |
201 | 232 | return; |
202 | 233 | } |
| 234 | + this.isAnimating = true; |
203 | 235 | translate3d(this.wrap,activeIndex*this.slideWidth); |
204 | 236 | transition(this.wrap,this.params.speed); |
205 | 237 | this.setActivePagination(); |
206 | 238 | if(activeIndex === this.firstIndex){ |
207 | | - this.params.loop && this.fixedPrevLoop(); |
| 239 | + this.fixedPrevLoop(); |
208 | 240 | } |
209 | 241 | } |
210 | 242 |
|
211 | | - function translate3d(element,x,y) { |
212 | | - x = x === undefined ? 0 : x; |
213 | | - y = y === undefined ? 0 : x; |
214 | | - element.style['-webkit-transform'] = 'translate3d(-'+x+'px,'+y+'px,0px)'; |
215 | | - element.style['transform'] = 'translate3d(-'+x+'px,'+y+'px,0px)'; |
| 243 | + fn.bindEvents = function(){ |
| 244 | + if(Device.desktop){ |
| 245 | + this.wrap.addEventListener('mousedown',this,false); |
| 246 | + this.wrap.addEventListener('mousemove',this,false); |
| 247 | + this.wrap.addEventListener('mouseup',this,false); |
| 248 | + }else{ |
| 249 | + this.wrap.addEventListener('touchstart',this,false); |
| 250 | + this.wrap.addEventListener('touchmove',this,false); |
| 251 | + this.wrap.addEventListener('touchend',this,false); |
| 252 | + } |
| 253 | + this.wrap.addEventListener('transitionend',this,false); |
| 254 | + this.container.addEventListener('click',this,false); |
| 255 | + } |
| 256 | + |
| 257 | + fn.transitionend = function(){ |
| 258 | + this.isAnimating = false; |
| 259 | + } |
| 260 | + |
| 261 | + fn.start = function(pageX){ |
| 262 | + this.axis.x = pageX; |
| 263 | + if(this.params.autoplay){ |
| 264 | + this.params.autoplay = false; |
| 265 | + this.timeId && clearTimeout(this.timeId); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + fn.move = function(pageX){ |
| 270 | + if(this.isAnimating) return; |
| 271 | + var distance = this.axis.x - pageX; |
| 272 | + translate3d(this.wrap,distance + this.slideWidth*this.activeIndex); |
| 273 | + transition(this.wrap,0); |
| 274 | + if(distance > 0){ |
| 275 | + if(distance > this.bounceWidth){ |
| 276 | + this.next(); |
| 277 | + } |
| 278 | + }else{ |
| 279 | + if(-distance > this.bounceWidth){ |
| 280 | + this.prev(); |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + fn.stop = function(){ |
| 286 | + if(this.isAnimating)return; |
| 287 | + translate3d(this.wrap,this.slideWidth*this.activeIndex); |
| 288 | + transition(this.wrap,this.params.speed); |
| 289 | + } |
| 290 | + |
| 291 | + fn.handleEvent = function(e){ |
| 292 | + var type = e.type; |
| 293 | + switch(type){ |
| 294 | + case 'mousedown': |
| 295 | + this.start(e.pageX); |
| 296 | + break; |
| 297 | + case 'mousemove': |
| 298 | + if(this.axis.x === 0) return; |
| 299 | + this.move(e.pageX); |
| 300 | + break; |
| 301 | + case 'mouseup': |
| 302 | + this.axis.x = 0; |
| 303 | + this.stop(); |
| 304 | + break; |
| 305 | + case 'touchstart': |
| 306 | + this.start(e.targetTouches[0].pageX); |
| 307 | + break; |
| 308 | + case 'touchmove': |
| 309 | + this.move(e.targetTouches[0].pageX); |
| 310 | + break; |
| 311 | + case 'touchend': |
| 312 | + this.stop(); |
| 313 | + break; |
| 314 | + case 'transitionend': |
| 315 | + case 'WebkitTransitionend': |
| 316 | + this.transitionend(); |
| 317 | + break; |
| 318 | + case 'click': |
| 319 | + e.stopPropagation(); |
| 320 | + var evt = e.target.className.split(' ')[1]; |
| 321 | + if(evt==='prev'||evt==='next'){ |
| 322 | + this[evt](); |
| 323 | + } |
| 324 | + break; |
| 325 | + default: |
| 326 | + break; |
| 327 | + } |
216 | 328 | } |
217 | | - |
218 | | - function transition(element,time){ |
219 | | - element.style['-webkit-transition-duration'] = time+'ms'; |
220 | | - element.style['transition-duration'] = time+'ms'; |
221 | | - } |
222 | 329 |
|
223 | 330 | if(!noGlobal){ |
224 | 331 | window.Slider = Slider; |
|
0 commit comments