Elffuss BraidHub

kikocis/iscroll public

05f2dd0b824b · 9.0 KiBRawPermalink
1
2function createDefaultScrollbar (direction, interactive, type) {
3 var scrollbar = document.createElement('div'),
4 indicator = document.createElement('div');
5
6 if ( type === true ) {
7 scrollbar.style.cssText = 'position:absolute;z-index:9999';
8 indicator.style.cssText = '-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;position:absolute;background:rgba(0,0,0,0.5);border:1px solid rgba(255,255,255,0.9);border-radius:3px';
9 }
10
11 indicator.className = 'iScrollIndicator';
12
13 if ( direction == 'h' ) {
14 if ( type === true ) {
15 scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
16 indicator.style.height = '100%';
17 }
18 scrollbar.className = 'iScrollHorizontalScrollbar';
19 } else {
20 if ( type === true ) {
21 scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
22 indicator.style.width = '100%';
23 }
24 scrollbar.className = 'iScrollVerticalScrollbar';
25 }
26
27 if ( !interactive ) {
28 scrollbar.style.pointerEvents = 'none';
29 }
30
31 scrollbar.appendChild(indicator);
32
33 return scrollbar;
34}
35
36function Indicator (scroller, options) {
37 this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
38 this.indicator = this.wrapper.children[0];
39 this.indicatorStyle = this.indicator.style;
40 this.scroller = scroller;
41
42 this.options = {
43 listenX: true,
44 listenY: true,
45 interactive: false,
46 resize: true,
47 defaultScrollbars: false,
48 speedRatioX: 0,
49 speedRatioY: 0
50 };
51
52 for ( var i in options ) {
53 this.options[i] = options[i];
54 }
55
56 this.sizeRatioX = 1;
57 this.sizeRatioY = 1;
58 this.maxPosX = 0;
59 this.maxPosY = 0;
60
61 if ( this.options.interactive ) {
62 if ( !this.options.disableTouch ) {
63 utils.addEvent(this.indicator, 'touchstart', this);
64 utils.addEvent(window, 'touchend', this);
65 }
66 if ( !this.options.disablePointer ) {
67 utils.addEvent(this.indicator, 'MSPointerDown', this);
68 utils.addEvent(window, 'MSPointerUp', this);
69 }
70 if ( !this.options.disableMouse ) {
71 utils.addEvent(this.indicator, 'mousedown', this);
72 utils.addEvent(window, 'mouseup', this);
73 }
74 }
75}
76
77Indicator.prototype = {
78 handleEvent: function (e) {
79 switch ( e.type ) {
80 case 'touchstart':
81 case 'MSPointerDown':
82 case 'mousedown':
83 this._start(e);
84 break;
85 case 'touchmove':
86 case 'MSPointerMove':
87 case 'mousemove':
88 this._move(e);
89 break;
90 case 'touchend':
91 case 'MSPointerUp':
92 case 'mouseup':
93 case 'touchcancel':
94 case 'MSPointerCancel':
95 case 'mousecancel':
96 this._end(e);
97 break;
98 }
99 },
100
101 destroy: function () {
102 if ( this.options.interactive ) {
103 utils.removeEvent(this.indicator, 'touchstart', this);
104 utils.removeEvent(this.indicator, 'MSPointerDown', this);
105 utils.removeEvent(this.indicator, 'mousedown', this);
106
107 utils.removeEvent(window, 'touchmove', this);
108 utils.removeEvent(window, 'MSPointerMove', this);
109 utils.removeEvent(window, 'mousemove', this);
110
111 utils.removeEvent(window, 'touchend', this);
112 utils.removeEvent(window, 'MSPointerUp', this);
113 utils.removeEvent(window, 'mouseup', this);
114 }
115
116 if ( this.options.defaultScrollbars ) {
117 this.wrapper.parentNode.removeChild(this.wrapper);
118 }
119 },
120
121 _start: function (e) {
122 var point = e.touches ? e.touches[0] : e;
123
124 e.preventDefault();
125 e.stopPropagation();
126
127 this.transitionTime();
128
129 this.initiated = true;
130 this.moved = false;
131 this.lastPointX = point.pageX;
132 this.lastPointY = point.pageY;
133
134 this.startTime = utils.getTime();
135
136 if ( !this.options.disableTouch ) {
137 utils.addEvent(window, 'touchmove', this);
138 }
139 if ( !this.options.disablePointer ) {
140 utils.addEvent(window, 'MSPointerMove', this);
141 }
142 if ( !this.options.disableMouse ) {
143 utils.addEvent(window, 'mousemove', this);
144 }
145
146 this.scroller._execEvent('beforeScrollStart');
147 },
148
149 _move: function (e) {
150 var point = e.touches ? e.touches[0] : e,
151 deltaX, deltaY,
152 newX, newY,
153 timestamp = utils.getTime();
154
155 if ( !this.moved ) {
156 this.scroller._execEvent('scrollStart');
157 }
158
159 this.moved = true;
160
161 deltaX = point.pageX - this.lastPointX;
162 this.lastPointX = point.pageX;
163
164 deltaY = point.pageY - this.lastPointY;
165 this.lastPointY = point.pageY;
166
167 newX = this.x + deltaX;
168 newY = this.y + deltaY;
169
170 this._pos(newX, newY);
171
172 e.preventDefault();
173 e.stopPropagation();
174 },
175
176 _end: function (e) {
177 if ( !this.initiated ) {
178 return;
179 }
180
181 this.initiated = false;
182
183 e.preventDefault();
184 e.stopPropagation();
185
186 utils.removeEvent(window, 'touchmove', this);
187 utils.removeEvent(window, 'MSPointerMove', this);
188 utils.removeEvent(window, 'mousemove', this);
189
190 if ( this.scroller.options.snap ) {
191 var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
192
193 var time = this.options.snapSpeed || Math.max(
194 Math.max(
195 Math.min(Math.abs(this.scroller.x - snap.x), 1000),
196 Math.min(Math.abs(this.scroller.y - snap.y), 1000)
197 ), 300);
198
199 if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
200 this.scroller.directionX = 0;
201 this.scroller.directionY = 0;
202 this.scroller.currentPage = snap;
203 this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
204 }
205 }
206
207 if ( this.moved ) {
208 this.scroller._execEvent('scrollEnd');
209 }
210 },
211
212 transitionTime: function (time) {
213 time = time || 0;
214 this.indicatorStyle[utils.style.transitionDuration] = time + 'ms';
215
216 if ( !time && utils.isBadAndroid ) {
217 this.indicatorStyle[utils.style.transitionDuration] = '0.001s';
218 }
219 },
220
221 transitionTimingFunction: function (easing) {
222 this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
223 },
224
225 refresh: function () {
226 this.transitionTime();
227
228 if ( this.options.listenX && !this.options.listenY ) {
229 this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
230 } else if ( this.options.listenY && !this.options.listenX ) {
231 this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
232 } else {
233 this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
234 }
235
236 if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
237 utils.addClass(this.wrapper, 'iScrollBothScrollbars');
238 utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
239
240 if ( this.options.defaultScrollbars && this.options.customStyle ) {
241 if ( this.options.listenX ) {
242 this.wrapper.style.right = '8px';
243 } else {
244 this.wrapper.style.bottom = '8px';
245 }
246 }
247 } else {
248 utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
249 utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
250
251 if ( this.options.defaultScrollbars && this.options.customStyle ) {
252 if ( this.options.listenX ) {
253 this.wrapper.style.right = '2px';
254 } else {
255 this.wrapper.style.bottom = '2px';
256 }
257 }
258 }
259
260 var r = this.wrapper.offsetHeight; // force refresh
261
262 if ( this.options.listenX ) {
263 this.wrapperWidth = this.wrapper.clientWidth;
264 if ( this.options.resize ) {
265 this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
266 this.indicatorStyle.width = this.indicatorWidth + 'px';
267 } else {
268 this.indicatorWidth = this.indicator.clientWidth;
269 }
270 this.maxPosX = this.wrapperWidth - this.indicatorWidth;
271 this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
272 }
273
274 if ( this.options.listenY ) {
275 this.wrapperHeight = this.wrapper.clientHeight;
276 if ( this.options.resize ) {
277 this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
278 this.indicatorStyle.height = this.indicatorHeight + 'px';
279 } else {
280 this.indicatorHeight = this.indicator.clientHeight;
281 }
282
283 this.maxPosY = this.wrapperHeight - this.indicatorHeight;
284 this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
285 }
286
287 this.updatePosition();
288 },
289
290 updatePosition: function () {
291 var x = Math.round(this.sizeRatioX * this.scroller.x) || 0,
292 y = Math.round(this.sizeRatioY * this.scroller.y) || 0;
293
294 if ( !this.options.ignoreBoundaries ) {
295 if ( x < 0 ) {
296 x = 0;
297 } else if ( x > this.maxPosX ) {
298 x = this.maxPosX;
299 }
300
301 if ( y < 0 ) {
302 y = 0;
303 } else if ( y > this.maxPosY ) {
304 y = this.maxPosY;
305 }
306 }
307
308 this.x = x;
309 this.y = y;
310
311 if ( this.scroller.options.useTransform ) {
312 this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
313 } else {
314 this.indicatorStyle.left = x + 'px';
315 this.indicatorStyle.top = y + 'px';
316 }
317 },
318
319 _pos: function (x, y) {
320 if ( x < 0 ) {
321 x = 0;
322 } else if ( x > this.maxPosX ) {
323 x = this.maxPosX;
324 }
325
326 if ( y < 0 ) {
327 y = 0;
328 } else if ( y > this.maxPosY ) {
329 y = this.maxPosY;
330 }
331
332 x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
333 y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
334
335 this.scroller.scrollTo(x, y);
336 }
337};