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