Elffuss BraidHub

kikocis/iscroll public

3dfb8d292bea · 7.5 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 utils.addEvent(this.indicator, 'touchstart', this);
63 utils.addEvent(this.indicator, 'MSPointerDown', this);
64 utils.addEvent(this.indicator, 'mousedown', this);
65
66 utils.addEvent(window, 'touchend', this);
67 utils.addEvent(window, 'MSPointerUp', this);
68 utils.addEvent(window, 'mouseup', this);
69 }
70}
71
72Indicator.prototype = {
73 handleEvent: function (e) {
74 switch ( e.type ) {
75 case 'touchstart':
76 case 'MSPointerDown':
77 case 'mousedown':
78 this._start(e);
79 break;
80 case 'touchmove':
81 case 'MSPointerMove':
82 case 'mousemove':
83 this._move(e);
84 break;
85 case 'touchend':
86 case 'MSPointerUp':
87 case 'mouseup':
88 case 'touchcancel':
89 case 'MSPointerCancel':
90 case 'mousecancel':
91 this._end(e);
92 break;
93 }
94 },
95
96 destroy: function () {
97 if ( this.options.interactive ) {
98 utils.removeEvent(this.indicator, 'touchstart', this);
99 utils.removeEvent(this.indicator, 'MSPointerDown', this);
100 utils.removeEvent(this.indicator, 'mousedown', this);
101
102 utils.removeEvent(window, 'touchmove', this);
103 utils.removeEvent(window, 'MSPointerMove', this);
104 utils.removeEvent(window, 'mousemove', this);
105
106 utils.removeEvent(window, 'touchend', this);
107 utils.removeEvent(window, 'MSPointerUp', this);
108 utils.removeEvent(window, 'mouseup', this);
109 }
110 },
111
112 _start: function (e) {
113 var point = e.touches ? e.touches[0] : e;
114
115 e.preventDefault();
116 e.stopPropagation();
117
118 this.transitionTime(0);
119
120 this.initiated = true;
121 this.lastPointX = point.pageX;
122 this.lastPointY = point.pageY;
123
124 this.startTime = utils.getTime();
125
126 utils.addEvent(window, 'touchmove', this);
127 utils.addEvent(window, 'MSPointerMove', this);
128 utils.addEvent(window, 'mousemove', this);
129 },
130
131 _move: function (e) {
132 var point = e.touches ? e.touches[0] : e,
133 deltaX, deltaY,
134 newX, newY,
135 timestamp = utils.getTime();
136
137 deltaX = point.pageX - this.lastPointX;
138 this.lastPointX = point.pageX;
139
140 deltaY = point.pageY - this.lastPointY;
141 this.lastPointY = point.pageY;
142
143 newX = this.x + deltaX;
144 newY = this.y + deltaY;
145
146 this._pos(newX, newY);
147
148 e.preventDefault();
149 e.stopPropagation();
150 },
151
152 _end: function (e) {
153 if ( !this.initiated ) {
154 return;
155 }
156
157 this.initiated = false;
158
159 e.preventDefault();
160 e.stopPropagation();
161
162 utils.removeEvent(window, 'touchmove', this);
163 utils.removeEvent(window, 'MSPointerMove', this);
164 utils.removeEvent(window, 'mousemove', this);
165 },
166
167 transitionTime: function (time) {
168 time = time || 0;
169 this.indicatorStyle[utils.style.transitionDuration] = time + 'ms';
170 },
171
172 transitionTimingFunction: function (easing) {
173 this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
174 },
175
176 refresh: function () {
177 this.transitionTime(0);
178
179 if ( this.options.listenX && !this.options.listenY ) {
180 this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
181 } else if ( this.options.listenY && !this.options.listenX ) {
182 this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
183 } else {
184 this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
185 }
186
187 if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
188 utils.addClass(this.wrapper, 'iScrollBothScrollbars');
189 utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
190
191 if ( this.options.defaultScrollbars ) {
192 if ( this.options.listenX ) {
193 this.wrapper.style.right = '8px';
194 } else {
195 this.wrapper.style.bottom = '8px';
196 }
197 }
198 } else {
199 utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
200 utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
201
202 if ( this.options.defaultScrollbars ) {
203 if ( this.options.listenX ) {
204 this.wrapper.style.right = '2px';
205 } else {
206 this.wrapper.style.bottom = '2px';
207 }
208 }
209 }
210
211 var r = this.wrapper.offsetHeight; // force refresh
212
213 if ( this.options.listenX ) {
214 this.wrapperWidth = this.wrapper.clientWidth;
215 if ( this.options.resize ) {
216 this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / this.scroller.scrollerWidth), 8);
217 this.indicatorStyle.width = this.indicatorWidth + 'px';
218 } else {
219 this.indicatorWidth = this.indicator.clientWidth;
220 }
221 this.maxPosX = this.wrapperWidth - this.indicatorWidth;
222 this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
223 }
224
225 if ( this.options.listenY ) {
226 this.wrapperHeight = this.wrapper.clientHeight;
227 if ( this.options.resize ) {
228 this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / this.scroller.scrollerHeight), 8);
229 this.indicatorStyle.height = this.indicatorHeight + 'px';
230 } else {
231 this.indicatorHeight = this.indicator.clientHeight;
232 }
233
234 this.maxPosY = this.wrapperHeight - this.indicatorHeight;
235 this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
236 }
237
238 this.updatePosition();
239 },
240
241 updatePosition: function () {
242 var x = Math.round(this.sizeRatioX * this.scroller.x) || 0,
243 y = Math.round(this.sizeRatioY * this.scroller.y) || 0;
244
245 if ( !this.options.ignoreBoundaries ) {
246 if ( x < 0 ) {
247 x = 0;
248 } else if ( x > this.maxPosX ) {
249 x = this.maxPosX;
250 }
251
252 if ( y < 0 ) {
253 y = 0;
254 } else if ( y > this.maxPosY ) {
255 y = this.maxPosY;
256 }
257 }
258
259 this.x = x;
260 this.y = y;
261
262 if ( this.scroller.options.useTransform ) {
263 this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
264 } else {
265 this.indicatorStyle.left = x + 'px';
266 this.indicatorStyle.top = y + 'px';
267 }
268 },
269
270 _pos: function (x, y) {
271 if ( x < 0 ) {
272 x = 0;
273 } else if ( x > this.maxPosX ) {
274 x = this.maxPosX;
275 }
276
277 if ( y < 0 ) {
278 y = 0;
279 } else if ( y > this.maxPosY ) {
280 y = this.maxPosY;
281 }
282
283 this.scroller.scrollTo(Math.round(x / this.sizeRatioX), Math.round(y / this.sizeRatioY));
284 }
285};