Elffuss BraidHub

kikocis/iscroll public

b4fcff1d26dc · 7.4 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, 'MSPointerMove', 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, 'MSPointerMove', 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.lastPointX = point.pageX;
121 this.lastPointY = point.pageY;
122
123 this.startTime = utils.getTime();
124
125 utils.addEvent(window, 'touchmove', this);
126 utils.addEvent(window, 'MSPointerMove', this);
127 utils.addEvent(window, 'mousemove', this);
128 },
129
130 _move: function (e) {
131 var point = e.touches ? e.touches[0] : e,
132 deltaX, deltaY,
133 newX, newY,
134 timestamp = utils.getTime();
135
136 deltaX = point.pageX - this.lastPointX;
137 this.lastPointX = point.pageX;
138
139 deltaY = point.pageY - this.lastPointY;
140 this.lastPointY = point.pageY;
141
142 newX = this.x + deltaX;
143 newY = this.y + deltaY;
144
145 this._pos(newX, newY);
146
147 e.preventDefault();
148 e.stopPropagation();
149 },
150
151 _end: function (e) {
152 e.preventDefault();
153 e.stopPropagation();
154
155 utils.removeEvent(window, 'touchmove', this);
156 utils.removeEvent(window, 'MSPointerMove', this);
157 utils.removeEvent(window, 'mousemove', this);
158 },
159
160 transitionTime: function (time) {
161 time = time || 0;
162 this.indicatorStyle[utils.style.transitionDuration] = time + 'ms';
163 },
164
165 transitionTimingFunction: function (easing) {
166 this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
167 },
168
169 refresh: function () {
170 this.transitionTime(0);
171
172 if ( this.options.listenX && !this.options.listenY ) {
173 this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
174 } else if ( this.options.listenY && !this.options.listenX ) {
175 this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
176 } else {
177 this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
178 }
179
180 if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
181 utils.addClass(this.wrapper, 'iScrollBothScrollbars');
182 utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
183
184 if ( this.options.defaultScrollbars ) {
185 if ( this.options.listenX ) {
186 this.wrapper.style.right = '8px';
187 } else {
188 this.wrapper.style.bottom = '8px';
189 }
190 }
191 } else {
192 utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
193 utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
194
195 if ( this.options.defaultScrollbars ) {
196 if ( this.options.listenX ) {
197 this.wrapper.style.right = '2px';
198 } else {
199 this.wrapper.style.bottom = '2px';
200 }
201 }
202 }
203
204 var r = this.wrapper.offsetHeight; // force refresh
205
206 if ( this.options.listenX ) {
207 this.wrapperWidth = this.wrapper.clientWidth;
208 if ( this.options.resize ) {
209 this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / this.scroller.scrollerWidth), 8);
210 this.indicatorStyle.width = this.indicatorWidth + 'px';
211 } else {
212 this.indicatorWidth = this.indicator.clientWidth;
213 }
214 this.maxPosX = this.wrapperWidth - this.indicatorWidth;
215 this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
216 }
217
218 if ( this.options.listenY ) {
219 this.wrapperHeight = this.wrapper.clientHeight;
220 if ( this.options.resize ) {
221 this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / this.scroller.scrollerHeight), 8);
222 this.indicatorStyle.height = this.indicatorHeight + 'px';
223 } else {
224 this.indicatorHeight = this.indicator.clientHeight;
225 }
226
227 this.maxPosY = this.wrapperHeight - this.indicatorHeight;
228 this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
229 }
230
231 this.updatePosition();
232 },
233
234 updatePosition: function () {
235 var x = Math.round(this.sizeRatioX * this.scroller.x) || 0,
236 y = Math.round(this.sizeRatioY * this.scroller.y) || 0;
237
238 if ( !this.options.ignoreBoundaries ) {
239 if ( x < 0 ) {
240 x = 0;
241 } else if ( x > this.maxPosX ) {
242 x = this.maxPosX;
243 }
244
245 if ( y < 0 ) {
246 y = 0;
247 } else if ( y > this.maxPosY ) {
248 y = this.maxPosY;
249 }
250 }
251
252 this.x = x;
253 this.y = y;
254
255 if ( this.scroller.options.useTransform ) {
256 this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
257 } else {
258 this.indicatorStyle.left = x + 'px';
259 this.indicatorStyle.top = y + 'px';
260 }
261 },
262
263 _pos: function (x, y) {
264 if ( x < 0 ) {
265 x = 0;
266 } else if ( x > this.maxPosX ) {
267 x = this.maxPosX;
268 }
269
270 if ( y < 0 ) {
271 y = 0;
272 } else if ( y > this.maxPosY ) {
273 y = this.maxPosY;
274 }
275
276 this.scroller.scrollTo(Math.round(x / this.sizeRatioX), Math.round(y / this.sizeRatioY));
277 }
278};