| 1 | |
| 2 | function 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 | scrollbar.style.cssText += ';overflow:hidden'; |
| 28 | |
| 29 | if ( !interactive ) { |
| 30 | scrollbar.style.pointerEvents = 'none'; |
| 31 | } |
| 32 | |
| 33 | scrollbar.appendChild(indicator); |
| 34 | |
| 35 | return scrollbar; |
| 36 | } |
| 37 | |
| 38 | function Indicator (scroller, options) { |
| 39 | this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el; |
| 40 | this.wrapperStyle = this.wrapper.style; |
| 41 | this.indicator = this.wrapper.children[0]; |
| 42 | this.indicatorStyle = this.indicator.style; |
| 43 | this.scroller = scroller; |
| 44 | |
| 45 | this.options = { |
| 46 | listenX: true, |
| 47 | listenY: true, |
| 48 | interactive: false, |
| 49 | resize: true, |
| 50 | defaultScrollbars: false, |
| 51 | shrink: false, |
| 52 | fade: false, |
| 53 | speedRatioX: 0, |
| 54 | speedRatioY: 0 |
| 55 | }; |
| 56 | |
| 57 | for ( var i in options ) { |
| 58 | this.options[i] = options[i]; |
| 59 | } |
| 60 | |
| 61 | this.sizeRatioX = 1; |
| 62 | this.sizeRatioY = 1; |
| 63 | this.maxPosX = 0; |
| 64 | this.maxPosY = 0; |
| 65 | |
| 66 | if ( this.options.interactive ) { |
| 67 | if ( !this.options.disableTouch ) { |
| 68 | utils.addEvent(this.indicator, 'touchstart', this); |
| 69 | utils.addEvent(window, 'touchend', this); |
| 70 | } |
| 71 | if ( !this.options.disablePointer ) { |
| 72 | utils.addEvent(this.indicator, 'MSPointerDown', this); |
| 73 | utils.addEvent(window, 'MSPointerUp', this); |
| 74 | } |
| 75 | if ( !this.options.disableMouse ) { |
| 76 | utils.addEvent(this.indicator, 'mousedown', this); |
| 77 | utils.addEvent(window, 'mouseup', this); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if ( this.options.fade ) { |
| 82 | this.wrapperStyle[utils.style.transform] = this.scroller.translateZ; |
| 83 | this.wrapperStyle[utils.style.transitionDuration] = utils.isBadAndroid ? '0.001s' : '0ms'; |
| 84 | this.wrapperStyle.opacity = '0'; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | Indicator.prototype = { |
| 89 | handleEvent: function (e) { |
| 90 | switch ( e.type ) { |
| 91 | case 'touchstart': |
| 92 | case 'MSPointerDown': |
| 93 | case 'mousedown': |
| 94 | this._start(e); |
| 95 | break; |
| 96 | case 'touchmove': |
| 97 | case 'MSPointerMove': |
| 98 | case 'mousemove': |
| 99 | this._move(e); |
| 100 | break; |
| 101 | case 'touchend': |
| 102 | case 'MSPointerUp': |
| 103 | case 'mouseup': |
| 104 | case 'touchcancel': |
| 105 | case 'MSPointerCancel': |
| 106 | case 'mousecancel': |
| 107 | this._end(e); |
| 108 | break; |
| 109 | } |
| 110 | }, |
| 111 | |
| 112 | destroy: function () { |
| 113 | if ( this.options.interactive ) { |
| 114 | utils.removeEvent(this.indicator, 'touchstart', this); |
| 115 | utils.removeEvent(this.indicator, 'MSPointerDown', this); |
| 116 | utils.removeEvent(this.indicator, 'mousedown', this); |
| 117 | |
| 118 | utils.removeEvent(window, 'touchmove', this); |
| 119 | utils.removeEvent(window, 'MSPointerMove', this); |
| 120 | utils.removeEvent(window, 'mousemove', this); |
| 121 | |
| 122 | utils.removeEvent(window, 'touchend', this); |
| 123 | utils.removeEvent(window, 'MSPointerUp', this); |
| 124 | utils.removeEvent(window, 'mouseup', this); |
| 125 | } |
| 126 | |
| 127 | if ( this.options.defaultScrollbars ) { |
| 128 | this.wrapper.parentNode.removeChild(this.wrapper); |
| 129 | } |
| 130 | }, |
| 131 | |
| 132 | _start: function (e) { |
| 133 | var point = e.touches ? e.touches[0] : e; |
| 134 | |
| 135 | e.preventDefault(); |
| 136 | e.stopPropagation(); |
| 137 | |
| 138 | this.transitionTime(); |
| 139 | |
| 140 | this.initiated = true; |
| 141 | this.moved = false; |
| 142 | this.lastPointX = point.pageX; |
| 143 | this.lastPointY = point.pageY; |
| 144 | |
| 145 | this.startTime = utils.getTime(); |
| 146 | |
| 147 | if ( !this.options.disableTouch ) { |
| 148 | utils.addEvent(window, 'touchmove', this); |
| 149 | } |
| 150 | if ( !this.options.disablePointer ) { |
| 151 | utils.addEvent(window, 'MSPointerMove', this); |
| 152 | } |
| 153 | if ( !this.options.disableMouse ) { |
| 154 | utils.addEvent(window, 'mousemove', this); |
| 155 | } |
| 156 | |
| 157 | this.scroller._execEvent('beforeScrollStart'); |
| 158 | }, |
| 159 | |
| 160 | _move: function (e) { |
| 161 | var point = e.touches ? e.touches[0] : e, |
| 162 | deltaX, deltaY, |
| 163 | newX, newY, |
| 164 | timestamp = utils.getTime(); |
| 165 | |
| 166 | if ( !this.moved ) { |
| 167 | this.scroller._execEvent('scrollStart'); |
| 168 | } |
| 169 | |
| 170 | this.moved = true; |
| 171 | |
| 172 | deltaX = point.pageX - this.lastPointX; |
| 173 | this.lastPointX = point.pageX; |
| 174 | |
| 175 | deltaY = point.pageY - this.lastPointY; |
| 176 | this.lastPointY = point.pageY; |
| 177 | |
| 178 | newX = this.x + deltaX; |
| 179 | newY = this.y + deltaY; |
| 180 | |
| 181 | this._pos(newX, newY); |
| 182 | |
| 183 | // INSERT POINT: indicator._move |
| 184 | |
| 185 | e.preventDefault(); |
| 186 | e.stopPropagation(); |
| 187 | }, |
| 188 | |
| 189 | _end: function (e) { |
| 190 | if ( !this.initiated ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | this.initiated = false; |
| 195 | |
| 196 | e.preventDefault(); |
| 197 | e.stopPropagation(); |
| 198 | |
| 199 | utils.removeEvent(window, 'touchmove', this); |
| 200 | utils.removeEvent(window, 'MSPointerMove', this); |
| 201 | utils.removeEvent(window, 'mousemove', this); |
| 202 | |
| 203 | if ( this.scroller.options.snap ) { |
| 204 | var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y); |
| 205 | |
| 206 | var time = this.options.snapSpeed || Math.max( |
| 207 | Math.max( |
| 208 | Math.min(Math.abs(this.scroller.x - snap.x), 1000), |
| 209 | Math.min(Math.abs(this.scroller.y - snap.y), 1000) |
| 210 | ), 300); |
| 211 | |
| 212 | if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) { |
| 213 | this.scroller.directionX = 0; |
| 214 | this.scroller.directionY = 0; |
| 215 | this.scroller.currentPage = snap; |
| 216 | this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if ( this.moved ) { |
| 221 | this.scroller._execEvent('scrollEnd'); |
| 222 | } |
| 223 | }, |
| 224 | |
| 225 | transitionTime: function (time) { |
| 226 | time = time || 0; |
| 227 | this.indicatorStyle[utils.style.transitionDuration] = time + 'ms'; |
| 228 | |
| 229 | if ( !time && utils.isBadAndroid ) { |
| 230 | this.indicatorStyle[utils.style.transitionDuration] = '0.001s'; |
| 231 | } |
| 232 | }, |
| 233 | |
| 234 | transitionTimingFunction: function (easing) { |
| 235 | this.indicatorStyle[utils.style.transitionTimingFunction] = easing; |
| 236 | }, |
| 237 | |
| 238 | refresh: function () { |
| 239 | this.transitionTime(); |
| 240 | |
| 241 | if ( this.options.listenX && !this.options.listenY ) { |
| 242 | this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none'; |
| 243 | } else if ( this.options.listenY && !this.options.listenX ) { |
| 244 | this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none'; |
| 245 | } else { |
| 246 | this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none'; |
| 247 | } |
| 248 | |
| 249 | if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) { |
| 250 | utils.addClass(this.wrapper, 'iScrollBothScrollbars'); |
| 251 | utils.removeClass(this.wrapper, 'iScrollLoneScrollbar'); |
| 252 | |
| 253 | if ( this.options.defaultScrollbars && this.options.customStyle ) { |
| 254 | if ( this.options.listenX ) { |
| 255 | this.wrapper.style.right = '8px'; |
| 256 | } else { |
| 257 | this.wrapper.style.bottom = '8px'; |
| 258 | } |
| 259 | } |
| 260 | } else { |
| 261 | utils.removeClass(this.wrapper, 'iScrollBothScrollbars'); |
| 262 | utils.addClass(this.wrapper, 'iScrollLoneScrollbar'); |
| 263 | |
| 264 | if ( this.options.defaultScrollbars && this.options.customStyle ) { |
| 265 | if ( this.options.listenX ) { |
| 266 | this.wrapper.style.right = '2px'; |
| 267 | } else { |
| 268 | this.wrapper.style.bottom = '2px'; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | var r = this.wrapper.offsetHeight; // force refresh |
| 274 | |
| 275 | if ( this.options.listenX ) { |
| 276 | this.wrapperWidth = this.wrapper.clientWidth; |
| 277 | if ( this.options.resize ) { |
| 278 | this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8); |
| 279 | this.indicatorStyle.width = this.indicatorWidth + 'px'; |
| 280 | } else { |
| 281 | this.indicatorWidth = this.indicator.clientWidth; |
| 282 | } |
| 283 | |
| 284 | this.maxPosX = this.wrapperWidth - this.indicatorWidth; |
| 285 | |
| 286 | if ( this.options.shrink == 'clip' ) { |
| 287 | this.minBoundaryX = -this.indicatorWidth + 8; |
| 288 | this.maxBoundaryX = this.wrapperWidth - 8; |
| 289 | } else { |
| 290 | this.minBoundaryX = 0; |
| 291 | this.maxBoundaryX = this.maxPosX; |
| 292 | } |
| 293 | |
| 294 | this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX)); |
| 295 | } |
| 296 | |
| 297 | if ( this.options.listenY ) { |
| 298 | this.wrapperHeight = this.wrapper.clientHeight; |
| 299 | if ( this.options.resize ) { |
| 300 | this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8); |
| 301 | this.indicatorStyle.height = this.indicatorHeight + 'px'; |
| 302 | } else { |
| 303 | this.indicatorHeight = this.indicator.clientHeight; |
| 304 | } |
| 305 | |
| 306 | this.maxPosY = this.wrapperHeight - this.indicatorHeight; |
| 307 | |
| 308 | if ( this.options.shrink == 'clip' ) { |
| 309 | this.minBoundaryY = -this.indicatorHeight + 8; |
| 310 | this.maxBoundaryY = this.wrapperHeight - 8; |
| 311 | } else { |
| 312 | this.minBoundaryY = 0; |
| 313 | this.maxBoundaryY = this.maxPosY; |
| 314 | } |
| 315 | |
| 316 | this.maxPosY = this.wrapperHeight - this.indicatorHeight; |
| 317 | this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY)); |
| 318 | } |
| 319 | |
| 320 | this.updatePosition(); |
| 321 | }, |
| 322 | |
| 323 | updatePosition: function () { |
| 324 | var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0, |
| 325 | y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0; |
| 326 | |
| 327 | if ( !this.options.ignoreBoundaries ) { |
| 328 | if ( x < this.minBoundaryX ) { |
| 329 | if ( this.options.shrink == 'scale' ) { |
| 330 | this.width = Math.max(this.indicatorWidth + x, 8); |
| 331 | this.indicatorStyle.width = this.width + 'px'; |
| 332 | } |
| 333 | x = this.minBoundaryX; |
| 334 | } else if ( x > this.maxBoundaryX ) { |
| 335 | if ( this.options.shrink == 'scale' ) { |
| 336 | this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8); |
| 337 | this.indicatorStyle.width = this.width + 'px'; |
| 338 | x = this.maxPosX + this.indicatorWidth - this.width; |
| 339 | } else { |
| 340 | x = this.maxBoundaryX; |
| 341 | } |
| 342 | } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) { |
| 343 | this.width = this.indicatorWidth; |
| 344 | this.indicatorStyle.width = this.width + 'px'; |
| 345 | } |
| 346 | |
| 347 | if ( y < this.minBoundaryY ) { |
| 348 | if ( this.options.shrink == 'scale' ) { |
| 349 | this.height = Math.max(this.indicatorHeight + y * 3, 8); |
| 350 | this.indicatorStyle.height = this.height + 'px'; |
| 351 | } |
| 352 | y = this.minBoundaryY; |
| 353 | } else if ( y > this.maxBoundaryY ) { |
| 354 | if ( this.options.shrink == 'scale' ) { |
| 355 | this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8); |
| 356 | this.indicatorStyle.height = this.height + 'px'; |
| 357 | y = this.maxPosY + this.indicatorHeight - this.height; |
| 358 | } else { |
| 359 | y = this.maxBoundaryY; |
| 360 | } |
| 361 | } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) { |
| 362 | this.height = this.indicatorHeight; |
| 363 | this.indicatorStyle.height = this.height + 'px'; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | this.x = x; |
| 368 | this.y = y; |
| 369 | |
| 370 | if ( this.scroller.options.useTransform ) { |
| 371 | this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ; |
| 372 | } else { |
| 373 | this.indicatorStyle.left = x + 'px'; |
| 374 | this.indicatorStyle.top = y + 'px'; |
| 375 | } |
| 376 | }, |
| 377 | |
| 378 | _pos: function (x, y) { |
| 379 | if ( x < 0 ) { |
| 380 | x = 0; |
| 381 | } else if ( x > this.maxPosX ) { |
| 382 | x = this.maxPosX; |
| 383 | } |
| 384 | |
| 385 | if ( y < 0 ) { |
| 386 | y = 0; |
| 387 | } else if ( y > this.maxPosY ) { |
| 388 | y = this.maxPosY; |
| 389 | } |
| 390 | |
| 391 | x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x; |
| 392 | y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y; |
| 393 | |
| 394 | this.scroller.scrollTo(x, y); |
| 395 | }, |
| 396 | |
| 397 | fade: function (val, hold) { |
| 398 | if ( hold && !this.visible ) { |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | clearTimeout(this.fadeTimeout); |
| 403 | this.fadeTimeout = null; |
| 404 | |
| 405 | var time = val ? 250 : 500, |
| 406 | delay = val ? 0 : 300; |
| 407 | |
| 408 | val = val ? '1' : '0'; |
| 409 | |
| 410 | this.wrapperStyle[utils.style.transitionDuration] = time + 'ms'; |
| 411 | |
| 412 | this.fadeTimeout = setTimeout((function (val) { |
| 413 | this.wrapperStyle.opacity = val; |
| 414 | this.visible = +val; |
| 415 | }).bind(this, val), delay); |
| 416 | } |
| 417 | }; |