|
20 | 20 | "use strict"; |
21 | 21 |
|
22 | 22 | var version = '1.0.0'; |
| 23 | + var defaultOption = { |
| 24 | + speed:'300ms', |
| 25 | + delay:'5000', |
| 26 | + bounce:0.3, |
| 27 | + auto:true |
| 28 | + }; |
23 | 29 |
|
24 | | - function Slider( selector, context ) { |
25 | | - return new Slider.init( selector, context ); |
| 30 | + var extend = function(target,source){ |
| 31 | + var res = {}; |
| 32 | + if(Object.assign){ |
| 33 | + return Object.assign(res,target,source); |
| 34 | + } |
| 35 | + for(var i in target){ |
| 36 | + res[i] = target[i]; |
| 37 | + } |
| 38 | + if(source && typeof source === 'object'){ |
| 39 | + for(var i in source){ |
| 40 | + if(res[i] !== null && res[i] !== undefined){ |
| 41 | + res[i] = source[i] |
| 42 | + }; |
| 43 | + } |
| 44 | + } |
| 45 | + return res; |
| 46 | + } |
| 47 | + |
| 48 | + function Slider( selector, options ) { |
| 49 | + var config = extend(defaultOption,options); |
| 50 | + return new Slider.init( selector, config ); |
26 | 51 | } |
27 | 52 |
|
28 | | - Slider.init=function(selector, context){ |
| 53 | + Slider.init=function(selector, options){ |
29 | 54 | var wrap = document.querySelector(selector); |
30 | 55 | var box = wrap.children[0]; |
31 | 56 | var pages = box.children; |
| 57 | + this.wrap = wrap; |
32 | 58 | this.box = box; |
33 | 59 | this.pages = pages; |
34 | | - this.screenWidth = window.innerWidth; |
| 60 | + this.speed = options.speed; |
| 61 | + this.delay = options.delay; |
| 62 | + this.auto = options.auto; |
| 63 | + this.bounce = options.bounce; |
35 | 64 | this.pageLength = pages.length; |
36 | 65 | this.initPages(); |
37 | 66 | } |
38 | 67 |
|
39 | | - Slider.init.prototype = Slider.prototype = { |
| 68 | + Slider.prototype = { |
40 | 69 | initPages:function(){ |
41 | 70 | var length = this.pageLength; |
| 71 | + if(length<1){ |
| 72 | + console.warn('Slider children require at least one'); |
| 73 | + return false; |
| 74 | + } |
42 | 75 | if(length < 3){ |
43 | 76 | var lastItem = this.pages[length-1]; |
44 | 77 | while(length < 3) { |
|
50 | 83 | this.prevIndex = length - 1; |
51 | 84 | this.currIndex = 0; |
52 | 85 | this.nextIndex = this.currIndex + 1; |
53 | | - this.translate(this.prevIndex,'-100%'); |
54 | | - this.translate(this.currIndex,'0px'); |
55 | | - this.translate(this.nextIndex,'100%'); |
56 | | - this.auto() |
| 86 | + this.reset(); |
| 87 | + this.width = window.innerWidth; |
| 88 | + this.auto && this.autoRun(); |
| 89 | + this.wrap.addEventListener('touchstart',this,false); |
| 90 | + this.wrap.addEventListener('touchmove',this,false); |
| 91 | + this.wrap.addEventListener('touchend',this,false); |
| 92 | + this.wrap.addEventListener('mousedown',this,false); |
| 93 | + this.wrap.addEventListener('mousemove',this,false); |
| 94 | + document.addEventListener('mouseup',this,false); |
57 | 95 | }, |
58 | 96 | handleEvent:function(e){ |
59 | | - |
| 97 | + var type = e.type; |
| 98 | + if(e.touches){ |
| 99 | + console.log(e.touches[0]) |
| 100 | + e = e.touches[0]||e.changedTouches[0]; |
| 101 | + } |
| 102 | + switch(type){ |
| 103 | + case 'mousedown': |
| 104 | + case 'touchstart': |
| 105 | + this.axis.x = e.clientX; |
| 106 | + this.axis.y = e.clientY; |
| 107 | + this.enableSlide = true; |
| 108 | + this.auto = false; |
| 109 | + clearTimeout(this.timmerId); |
| 110 | + break; |
| 111 | + case 'mouseup': |
| 112 | + case 'touchend': |
| 113 | + var distance = e.clientX - this.axis.x; |
| 114 | + var bounce = Math.round(this.width * this.bounce); |
| 115 | + this.enableSlide = false; |
| 116 | + if(Math.abs(distance)>bounce){ |
| 117 | + distance > 0 ? this.prev() : this.next(); |
| 118 | + }else{ |
| 119 | + //反弹 |
| 120 | + if(distance>0){ |
| 121 | + this.css(this.currIndex,this.animate('0px')); |
| 122 | + this.css(this.prevIndex,this.animate('100%')); |
| 123 | + }else{ |
| 124 | + this.css(this.currIndex,this.animate('0px')); |
| 125 | + this.css(this.nextIndex,this.animate('100%')); |
| 126 | + } |
| 127 | + } |
| 128 | + break; |
| 129 | + case 'mousemove': |
| 130 | + case 'touchmove': |
| 131 | + var distance = e.clientX - this.axis.x; |
| 132 | + if(this.enableSlide){ |
| 133 | + this.move(distance); |
| 134 | + } |
| 135 | + break; |
| 136 | + default: |
| 137 | + break; |
| 138 | + } |
| 139 | + |
60 | 140 | }, |
61 | | - |
62 | | - setStyle:function(index,distance){ |
63 | | - var element = this.pages[index]; |
64 | | - this.translate(index,distance); |
65 | | - element.style.WebkitTransition = 'all 6s ease'; |
66 | | - element.style.transition = 'all 6s ease'; |
67 | | - element.style.visibility = 'visible'; |
68 | | - element.style.zIndex = 10; |
| 141 | + reset:function(){ |
| 142 | + this.css(this.prevIndex,this.translate('-100%')); |
| 143 | + this.css(this.currIndex,this.translate('0px')); |
| 144 | + this.css(this.nextIndex,this.translate('100%')); |
| 145 | + for(var i=this.nextIndex+1;i<this.pageLength-1;i++){ |
| 146 | + this.css(i,this.translate('100%')); |
| 147 | + } |
| 148 | + this.axis = { |
| 149 | + x:0, |
| 150 | + y:0 |
| 151 | + } |
69 | 152 | }, |
70 | | - move:function(index,distance){ |
| 153 | + css:function(index,style){ |
71 | 154 | var element = this.pages[index]; |
72 | | - this.translate(index,distance); |
73 | | - element.style.WebkitTransition = 'none'; |
74 | | - element.style.transition = 'none'; |
75 | | - element.style.visibility = 'hidden'; |
76 | | - element.style.zIndex = 5; |
| 155 | + for(var i in style){ |
| 156 | + element.style[i] = style[i]; |
| 157 | + } |
77 | 158 | }, |
78 | | - translate:function(index,distance){ |
79 | | - var element = this.pages[index]; |
80 | | - element.style.WebkitTransform = "translate3d(" + distance + ",0,0)"; |
81 | | - element.style.transform = "translate3d(" + distance + ",0,0)"; |
| 159 | + move:function(distance){ |
| 160 | + var width = this.width; |
| 161 | + if(distance > 0){ |
| 162 | + //prev |
| 163 | + this.css(this.currIndex,this.translate(distance+'px')); |
| 164 | + this.css(this.prevIndex,this.translate((distance-width)+'px')); |
| 165 | + }else{ |
| 166 | + //next |
| 167 | + this.css(this.currIndex,this.translate(distance+'px')); |
| 168 | + this.css(this.nextIndex,this.translate((width+distance)+'px')); |
| 169 | + } |
| 170 | + }, |
| 171 | + translate:function(distance){ |
| 172 | + return { |
| 173 | + WebkitTransform:"translate3d(" + distance + ",0px,0px)", |
| 174 | + transform:"translate3d(" + distance + ",0px,0px)" |
| 175 | + } |
| 176 | + }, |
| 177 | + animate:function(distance){ |
| 178 | + return { |
| 179 | + WebkitTransform:"translate3d(" + distance + ",0px,0px)", |
| 180 | + transform:"translate3d(" + distance + ",0px,0px)", |
| 181 | + WebkitTransition : 'all '+ this.speed+' ease', |
| 182 | + transition:'all '+ this.speed+' ease', |
| 183 | + zIndex:5 |
| 184 | + } |
| 185 | + }, |
| 186 | + noAnimate:function(distance){ |
| 187 | + return { |
| 188 | + WebkitTransform:"translate3d(" + distance + ",0px,0px)", |
| 189 | + transform:"translate3d(" + distance + ",0px,0px)", |
| 190 | + WebkitTransition : 'none', |
| 191 | + transition:'none', |
| 192 | + zIndex:-1 |
| 193 | + } |
82 | 194 | }, |
83 | 195 | next:function(){ |
84 | | - this.setStyle(this.currIndex,'-100%'); |
85 | | - this.setStyle(this.nextIndex,'0px'); |
86 | | - this.move(this.prevIndex,'100%'); |
87 | 196 | var length = this.pageLength-1; |
88 | | - var prevIndex = this.prevIndex; |
| 197 | + this.css(this.currIndex,this.animate('-100%')); |
| 198 | + this.css(this.nextIndex,this.animate('0px')); |
| 199 | + this.css(this.prevIndex,this.noAnimate('100%')); |
89 | 200 | this.prevIndex = this.currIndex; |
90 | 201 | this.currIndex = this.nextIndex; |
91 | 202 | if(++this.nextIndex > length){ |
92 | | - this.nextIndex = prevIndex; |
| 203 | + this.nextIndex = 0; |
93 | 204 | } |
94 | | - this.step(); |
95 | 205 | }, |
96 | 206 | prev:function(){ |
97 | 207 | var length = this.pageLength-1; |
| 208 | + this.css(this.currIndex,this.animate('100%')); |
| 209 | + this.css(this.prevIndex,this.animate('0px')); |
| 210 | + this.css(this.nextIndex,this.noAnimate('-100%')); |
98 | 211 | this.nextIndex = this.currIndex; |
99 | 212 | this.currIndex = this.prevIndex; |
100 | 213 | if(--this.prevIndex<0){ |
101 | 214 | this.prevIndex = length; |
102 | 215 | } |
103 | | - this.step(); |
104 | 216 | }, |
105 | | - auto:function(){ |
| 217 | + autoRun:function(){ |
106 | 218 | var that = this; |
107 | | - setTimeout(function(){ |
| 219 | + if(!this.auto){ |
| 220 | + return false; |
| 221 | + } |
| 222 | + this.timmerId = setTimeout(function(){ |
108 | 223 | that.next(); |
109 | | - that.auto(); |
110 | | - },3000) |
111 | | - }, |
112 | | - step:function(){ |
113 | | - |
114 | | - console.log(this.currIndex) |
| 224 | + that.autoRun(); |
| 225 | + },this.delay); |
115 | 226 | } |
116 | 227 | } |
117 | 228 |
|
118 | | - |
119 | | - |
| 229 | + Slider.init.prototype = Slider.prototype; |
120 | 230 |
|
121 | 231 | if(!noGlobal){ |
122 | 232 | window.Slider = Slider; |
|
0 commit comments