| 1 | /*! iScroll v5.1.1 ~ (c) 2008-2014 Matteo Spinelli ~ http://cubiq.org/license */ |
| 2 | (function (window, document, Math) { |
| 3 | var rAF = window.requestAnimationFrame || |
| 4 | window.webkitRequestAnimationFrame || |
| 5 | window.mozRequestAnimationFrame || |
| 6 | window.oRequestAnimationFrame || |
| 7 | window.msRequestAnimationFrame || |
| 8 | function (callback) { window.setTimeout(callback, 1000 / 60); }; |
| 9 | |
| 10 | var utils = (function () { |
| 11 | var me = {}; |
| 12 | |
| 13 | var _elementStyle = document.createElement('div').style; |
| 14 | var _vendor = (function () { |
| 15 | var vendors = ['t', 'webkitT', 'MozT', 'msT', 'OT'], |
| 16 | transform, |
| 17 | i = 0, |
| 18 | l = vendors.length; |
| 19 | |
| 20 | for ( ; i < l; i++ ) { |
| 21 | transform = vendors[i] + 'ransform'; |
| 22 | if ( transform in _elementStyle ) return vendors[i].substr(0, vendors[i].length-1); |
| 23 | } |
| 24 | |
| 25 | return false; |
| 26 | })(); |
| 27 | |
| 28 | function _prefixStyle (style) { |
| 29 | if ( _vendor === false ) return false; |
| 30 | if ( _vendor === '' ) return style; |
| 31 | return _vendor + style.charAt(0).toUpperCase() + style.substr(1); |
| 32 | } |
| 33 | |
| 34 | me.getTime = Date.now || function getTime () { return new Date().getTime(); }; |
| 35 | |
| 36 | me.extend = function (target, obj) { |
| 37 | for ( var i in obj ) { |
| 38 | target[i] = obj[i]; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | me.addEvent = function (el, type, fn, capture) { |
| 43 | el.addEventListener(type, fn, !!capture); |
| 44 | }; |
| 45 | |
| 46 | me.removeEvent = function (el, type, fn, capture) { |
| 47 | el.removeEventListener(type, fn, !!capture); |
| 48 | }; |
| 49 | |
| 50 | me.momentum = function (current, start, time, lowerMargin, wrapperSize, deceleration) { |
| 51 | var distance = current - start, |
| 52 | speed = Math.abs(distance) / time, |
| 53 | destination, |
| 54 | duration; |
| 55 | |
| 56 | deceleration = deceleration === undefined ? 0.0006 : deceleration; |
| 57 | |
| 58 | destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 ); |
| 59 | duration = speed / deceleration; |
| 60 | |
| 61 | if ( destination < lowerMargin ) { |
| 62 | destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin; |
| 63 | distance = Math.abs(destination - current); |
| 64 | duration = distance / speed; |
| 65 | } else if ( destination > 0 ) { |
| 66 | destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0; |
| 67 | distance = Math.abs(current) + destination; |
| 68 | duration = distance / speed; |
| 69 | } |
| 70 | |
| 71 | return { |
| 72 | destination: Math.round(destination), |
| 73 | duration: duration |
| 74 | }; |
| 75 | }; |
| 76 | |
| 77 | var _transform = _prefixStyle('transform'); |
| 78 | |
| 79 | me.extend(me, { |
| 80 | hasTransform: _transform !== false, |
| 81 | hasPerspective: _prefixStyle('perspective') in _elementStyle, |
| 82 | hasTouch: 'ontouchstart' in window, |
| 83 | hasPointer: navigator.msPointerEnabled, |
| 84 | hasTransition: _prefixStyle('transition') in _elementStyle |
| 85 | }); |
| 86 | |
| 87 | // This should find all Android browsers lower than build 535.19 (both stock browser and webview) |
| 88 | me.isBadAndroid = /Android /.test(window.navigator.appVersion) && !(/Chrome\/\d/.test(window.navigator.appVersion)); |
| 89 | |
| 90 | me.extend(me.style = {}, { |
| 91 | transform: _transform, |
| 92 | transitionTimingFunction: _prefixStyle('transitionTimingFunction'), |
| 93 | transitionDuration: _prefixStyle('transitionDuration'), |
| 94 | transitionDelay: _prefixStyle('transitionDelay'), |
| 95 | transformOrigin: _prefixStyle('transformOrigin') |
| 96 | }); |
| 97 | |
| 98 | me.hasClass = function (e, c) { |
| 99 | var re = new RegExp("(^|\\s)" + c + "(\\s|$)"); |
| 100 | return re.test(e.className); |
| 101 | }; |
| 102 | |
| 103 | me.addClass = function (e, c) { |
| 104 | if ( me.hasClass(e, c) ) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | var newclass = e.className.split(' '); |
| 109 | newclass.push(c); |
| 110 | e.className = newclass.join(' '); |
| 111 | }; |
| 112 | |
| 113 | me.removeClass = function (e, c) { |
| 114 | if ( !me.hasClass(e, c) ) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | var re = new RegExp("(^|\\s)" + c + "(\\s|$)", 'g'); |
| 119 | e.className = e.className.replace(re, ' '); |
| 120 | }; |
| 121 | |
| 122 | me.offset = function (el) { |
| 123 | var left = -el.offsetLeft, |
| 124 | top = -el.offsetTop; |
| 125 | |
| 126 | // jshint -W084 |
| 127 | while (el = el.offsetParent) { |
| 128 | left -= el.offsetLeft; |
| 129 | top -= el.offsetTop; |
| 130 | } |
| 131 | // jshint +W084 |
| 132 | |
| 133 | return { |
| 134 | left: left, |
| 135 | top: top |
| 136 | }; |
| 137 | }; |
| 138 | |
| 139 | me.preventDefaultException = function (el, exceptions) { |
| 140 | for ( var i in exceptions ) { |
| 141 | if ( exceptions[i].test(el[i]) ) { |
| 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return false; |
| 147 | }; |
| 148 | |
| 149 | me.extend(me.eventType = {}, { |
| 150 | touchstart: 1, |
| 151 | touchmove: 1, |
| 152 | touchend: 1, |
| 153 | |
| 154 | mousedown: 2, |
| 155 | mousemove: 2, |
| 156 | mouseup: 2, |
| 157 | |
| 158 | MSPointerDown: 3, |
| 159 | MSPointerMove: 3, |
| 160 | MSPointerUp: 3 |
| 161 | }); |
| 162 | |
| 163 | me.extend(me.ease = {}, { |
| 164 | quadratic: { |
| 165 | style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)', |
| 166 | fn: function (k) { |
| 167 | return k * ( 2 - k ); |
| 168 | } |
| 169 | }, |
| 170 | circular: { |
| 171 | style: 'cubic-bezier(0.1, 0.57, 0.1, 1)', // Not properly "circular" but this looks better, it should be (0.075, 0.82, 0.165, 1) |
| 172 | fn: function (k) { |
| 173 | return Math.sqrt( 1 - ( --k * k ) ); |
| 174 | } |
| 175 | }, |
| 176 | back: { |
| 177 | style: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)', |
| 178 | fn: function (k) { |
| 179 | var b = 4; |
| 180 | return ( k = k - 1 ) * k * ( ( b + 1 ) * k + b ) + 1; |
| 181 | } |
| 182 | }, |
| 183 | bounce: { |
| 184 | style: '', |
| 185 | fn: function (k) { |
| 186 | if ( ( k /= 1 ) < ( 1 / 2.75 ) ) { |
| 187 | return 7.5625 * k * k; |
| 188 | } else if ( k < ( 2 / 2.75 ) ) { |
| 189 | return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75; |
| 190 | } else if ( k < ( 2.5 / 2.75 ) ) { |
| 191 | return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375; |
| 192 | } else { |
| 193 | return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375; |
| 194 | } |
| 195 | } |
| 196 | }, |
| 197 | elastic: { |
| 198 | style: '', |
| 199 | fn: function (k) { |
| 200 | var f = 0.22, |
| 201 | e = 0.4; |
| 202 | |
| 203 | if ( k === 0 ) { return 0; } |
| 204 | if ( k == 1 ) { return 1; } |
| 205 | |
| 206 | return ( e * Math.pow( 2, - 10 * k ) * Math.sin( ( k - f / 4 ) * ( 2 * Math.PI ) / f ) + 1 ); |
| 207 | } |
| 208 | } |
| 209 | }); |
| 210 | |
| 211 | me.tap = function (e, eventName) { |
| 212 | var ev = document.createEvent('Event'); |
| 213 | ev.initEvent(eventName, true, true); |
| 214 | ev.pageX = e.pageX; |
| 215 | ev.pageY = e.pageY; |
| 216 | e.target.dispatchEvent(ev); |
| 217 | }; |
| 218 | |
| 219 | me.click = function (e) { |
| 220 | var target = e.target, |
| 221 | ev; |
| 222 | |
| 223 | if ( !(/(SELECT|INPUT|TEXTAREA)/i).test(target.tagName) ) { |
| 224 | ev = document.createEvent('MouseEvents'); |
| 225 | ev.initMouseEvent('click', true, true, e.view, 1, |
| 226 | target.screenX, target.screenY, target.clientX, target.clientY, |
| 227 | e.ctrlKey, e.altKey, e.shiftKey, e.metaKey, |
| 228 | 0, null); |
| 229 | |
| 230 | ev._constructed = true; |
| 231 | target.dispatchEvent(ev); |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | return me; |
| 236 | })(); |
| 237 | |
| 238 | function IScroll (el, options) { |
| 239 | this.wrapper = typeof el == 'string' ? document.querySelector(el) : el; |
| 240 | this.scroller = this.wrapper.children[0]; |
| 241 | this.scrollerStyle = this.scroller.style; // cache style for better performance |
| 242 | |
| 243 | this.options = { |
| 244 | |
| 245 | zoomMin: 1, |
| 246 | zoomMax: 4, startZoom: 1, |
| 247 | |
| 248 | resizeScrollbars: true, |
| 249 | |
| 250 | mouseWheelSpeed: 20, |
| 251 | |
| 252 | snapThreshold: 0.334, |
| 253 | |
| 254 | // INSERT POINT: OPTIONS |
| 255 | |
| 256 | startX: 0, |
| 257 | startY: 0, |
| 258 | scrollY: true, |
| 259 | directionLockThreshold: 5, |
| 260 | momentum: true, |
| 261 | |
| 262 | bounce: true, |
| 263 | bounceTime: 600, |
| 264 | bounceEasing: '', |
| 265 | |
| 266 | preventDefault: true, |
| 267 | preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ }, |
| 268 | |
| 269 | HWCompositing: true, |
| 270 | useTransition: true, |
| 271 | useTransform: true |
| 272 | }; |
| 273 | |
| 274 | for ( var i in options ) { |
| 275 | this.options[i] = options[i]; |
| 276 | } |
| 277 | |
| 278 | // Normalize options |
| 279 | this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : ''; |
| 280 | |
| 281 | this.options.useTransition = utils.hasTransition && this.options.useTransition; |
| 282 | this.options.useTransform = utils.hasTransform && this.options.useTransform; |
| 283 | |
| 284 | this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough; |
| 285 | this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault; |
| 286 | |
| 287 | // If you want eventPassthrough I have to lock one of the axes |
| 288 | this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY; |
| 289 | this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX; |
| 290 | |
| 291 | // With eventPassthrough we also need lockDirection mechanism |
| 292 | this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough; |
| 293 | this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold; |
| 294 | |
| 295 | this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing; |
| 296 | |
| 297 | this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling; |
| 298 | |
| 299 | if ( this.options.tap === true ) { |
| 300 | this.options.tap = 'tap'; |
| 301 | } |
| 302 | |
| 303 | if ( this.options.shrinkScrollbars == 'scale' ) { |
| 304 | this.options.useTransition = false; |
| 305 | } |
| 306 | |
| 307 | this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1; |
| 308 | |
| 309 | // INSERT POINT: NORMALIZATION |
| 310 | |
| 311 | // Some defaults |
| 312 | this.x = 0; |
| 313 | this.y = 0; |
| 314 | this.directionX = 0; |
| 315 | this.directionY = 0; |
| 316 | this._events = {}; |
| 317 | |
| 318 | this.scale = Math.min(Math.max(this.options.startZoom, this.options.zoomMin), this.options.zoomMax); |
| 319 | |
| 320 | // INSERT POINT: DEFAULTS |
| 321 | |
| 322 | this._init(); |
| 323 | this.refresh(); |
| 324 | |
| 325 | this.scrollTo(this.options.startX, this.options.startY); |
| 326 | this.enable(); |
| 327 | } |
| 328 | |
| 329 | IScroll.prototype = { |
| 330 | version: '5.1.1', |
| 331 | |
| 332 | _init: function () { |
| 333 | this._initEvents(); |
| 334 | |
| 335 | if ( this.options.zoom ) { |
| 336 | this._initZoom(); |
| 337 | } |
| 338 | |
| 339 | if ( this.options.scrollbars || this.options.indicators ) { |
| 340 | this._initIndicators(); |
| 341 | } |
| 342 | |
| 343 | if ( this.options.mouseWheel ) { |
| 344 | this._initWheel(); |
| 345 | } |
| 346 | |
| 347 | if ( this.options.snap ) { |
| 348 | this._initSnap(); |
| 349 | } |
| 350 | |
| 351 | if ( this.options.keyBindings ) { |
| 352 | this._initKeys(); |
| 353 | } |
| 354 | |
| 355 | // INSERT POINT: _init |
| 356 | |
| 357 | }, |
| 358 | |
| 359 | destroy: function () { |
| 360 | this._initEvents(true); |
| 361 | |
| 362 | this._execEvent('destroy'); |
| 363 | }, |
| 364 | |
| 365 | _transitionEnd: function (e) { |
| 366 | if ( e.target != this.scroller || !this.isInTransition ) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | this._transitionTime(); |
| 371 | if ( !this.resetPosition(this.options.bounceTime) ) { |
| 372 | this.isInTransition = false; |
| 373 | this._execEvent('scrollEnd'); |
| 374 | } |
| 375 | }, |
| 376 | |
| 377 | _start: function (e) { |
| 378 | // React to left mouse button only |
| 379 | if ( utils.eventType[e.type] != 1 ) { |
| 380 | if ( e.button !== 0 ) { |
| 381 | return; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) { |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) { |
| 390 | e.preventDefault(); |
| 391 | } |
| 392 | |
| 393 | var point = e.touches ? e.touches[0] : e, |
| 394 | pos; |
| 395 | |
| 396 | this.initiated = utils.eventType[e.type]; |
| 397 | this.moved = false; |
| 398 | this.distX = 0; |
| 399 | this.distY = 0; |
| 400 | this.directionX = 0; |
| 401 | this.directionY = 0; |
| 402 | this.directionLocked = 0; |
| 403 | |
| 404 | this._transitionTime(); |
| 405 | |
| 406 | this.startTime = utils.getTime(); |
| 407 | |
| 408 | if ( this.options.useTransition && this.isInTransition ) { |
| 409 | this.isInTransition = false; |
| 410 | pos = this.getComputedPosition(); |
| 411 | this._translate(Math.round(pos.x), Math.round(pos.y)); |
| 412 | this._execEvent('scrollEnd'); |
| 413 | } else if ( !this.options.useTransition && this.isAnimating ) { |
| 414 | this.isAnimating = false; |
| 415 | this._execEvent('scrollEnd'); |
| 416 | } |
| 417 | |
| 418 | this.startX = this.x; |
| 419 | this.startY = this.y; |
| 420 | this.absStartX = this.x; |
| 421 | this.absStartY = this.y; |
| 422 | this.pointX = point.pageX; |
| 423 | this.pointY = point.pageY; |
| 424 | |
| 425 | this._execEvent('beforeScrollStart'); |
| 426 | }, |
| 427 | |
| 428 | _move: function (e) { |
| 429 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | if ( this.options.preventDefault ) { // increases performance on Android? TODO: check! |
| 434 | e.preventDefault(); |
| 435 | } |
| 436 | |
| 437 | var point = e.touches ? e.touches[0] : e, |
| 438 | deltaX = point.pageX - this.pointX, |
| 439 | deltaY = point.pageY - this.pointY, |
| 440 | timestamp = utils.getTime(), |
| 441 | newX, newY, |
| 442 | absDistX, absDistY; |
| 443 | |
| 444 | this.pointX = point.pageX; |
| 445 | this.pointY = point.pageY; |
| 446 | |
| 447 | this.distX += deltaX; |
| 448 | this.distY += deltaY; |
| 449 | absDistX = Math.abs(this.distX); |
| 450 | absDistY = Math.abs(this.distY); |
| 451 | |
| 452 | // We need to move at least 10 pixels for the scrolling to initiate |
| 453 | if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) { |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | // If you are scrolling in one direction lock the other |
| 458 | if ( !this.directionLocked && !this.options.freeScroll ) { |
| 459 | if ( absDistX > absDistY + this.options.directionLockThreshold ) { |
| 460 | this.directionLocked = 'h'; // lock horizontally |
| 461 | } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) { |
| 462 | this.directionLocked = 'v'; // lock vertically |
| 463 | } else { |
| 464 | this.directionLocked = 'n'; // no lock |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | if ( this.directionLocked == 'h' ) { |
| 469 | if ( this.options.eventPassthrough == 'vertical' ) { |
| 470 | e.preventDefault(); |
| 471 | } else if ( this.options.eventPassthrough == 'horizontal' ) { |
| 472 | this.initiated = false; |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | deltaY = 0; |
| 477 | } else if ( this.directionLocked == 'v' ) { |
| 478 | if ( this.options.eventPassthrough == 'horizontal' ) { |
| 479 | e.preventDefault(); |
| 480 | } else if ( this.options.eventPassthrough == 'vertical' ) { |
| 481 | this.initiated = false; |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | deltaX = 0; |
| 486 | } |
| 487 | |
| 488 | deltaX = this.hasHorizontalScroll ? deltaX : 0; |
| 489 | deltaY = this.hasVerticalScroll ? deltaY : 0; |
| 490 | |
| 491 | newX = this.x + deltaX; |
| 492 | newY = this.y + deltaY; |
| 493 | |
| 494 | // Slow down if outside of the boundaries |
| 495 | if ( newX > 0 || newX < this.maxScrollX ) { |
| 496 | newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX; |
| 497 | } |
| 498 | if ( newY > 0 || newY < this.maxScrollY ) { |
| 499 | newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY; |
| 500 | } |
| 501 | |
| 502 | this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0; |
| 503 | this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0; |
| 504 | |
| 505 | if ( !this.moved ) { |
| 506 | this._execEvent('scrollStart'); |
| 507 | } |
| 508 | |
| 509 | this.moved = true; |
| 510 | |
| 511 | this._translate(newX, newY); |
| 512 | |
| 513 | /* REPLACE START: _move */ |
| 514 | |
| 515 | if ( timestamp - this.startTime > 300 ) { |
| 516 | this.startTime = timestamp; |
| 517 | this.startX = this.x; |
| 518 | this.startY = this.y; |
| 519 | } |
| 520 | |
| 521 | /* REPLACE END: _move */ |
| 522 | |
| 523 | }, |
| 524 | |
| 525 | _end: function (e) { |
| 526 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) { |
| 531 | e.preventDefault(); |
| 532 | } |
| 533 | |
| 534 | var point = e.changedTouches ? e.changedTouches[0] : e, |
| 535 | momentumX, |
| 536 | momentumY, |
| 537 | duration = utils.getTime() - this.startTime, |
| 538 | newX = Math.round(this.x), |
| 539 | newY = Math.round(this.y), |
| 540 | distanceX = Math.abs(newX - this.startX), |
| 541 | distanceY = Math.abs(newY - this.startY), |
| 542 | time = 0, |
| 543 | easing = ''; |
| 544 | |
| 545 | this.isInTransition = 0; |
| 546 | this.initiated = 0; |
| 547 | this.endTime = utils.getTime(); |
| 548 | |
| 549 | // reset if we are outside of the boundaries |
| 550 | if ( this.resetPosition(this.options.bounceTime) ) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | this.scrollTo(newX, newY); // ensures that the last position is rounded |
| 555 | |
| 556 | // we scrolled less than 10 pixels |
| 557 | if ( !this.moved ) { |
| 558 | if ( this.options.tap ) { |
| 559 | utils.tap(e, this.options.tap); |
| 560 | } |
| 561 | |
| 562 | if ( this.options.click ) { |
| 563 | utils.click(e); |
| 564 | } |
| 565 | |
| 566 | this._execEvent('scrollCancel'); |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) { |
| 571 | this._execEvent('flick'); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | // start momentum animation if needed |
| 576 | if ( this.options.momentum && duration < 300 ) { |
| 577 | momentumX = this.hasHorizontalScroll ? utils.momentum(this.x, this.startX, duration, this.maxScrollX, this.options.bounce ? this.wrapperWidth : 0, this.options.deceleration) : { destination: newX, duration: 0 }; |
| 578 | momentumY = this.hasVerticalScroll ? utils.momentum(this.y, this.startY, duration, this.maxScrollY, this.options.bounce ? this.wrapperHeight : 0, this.options.deceleration) : { destination: newY, duration: 0 }; |
| 579 | newX = momentumX.destination; |
| 580 | newY = momentumY.destination; |
| 581 | time = Math.max(momentumX.duration, momentumY.duration); |
| 582 | this.isInTransition = 1; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | if ( this.options.snap ) { |
| 587 | var snap = this._nearestSnap(newX, newY); |
| 588 | this.currentPage = snap; |
| 589 | time = this.options.snapSpeed || Math.max( |
| 590 | Math.max( |
| 591 | Math.min(Math.abs(newX - snap.x), 1000), |
| 592 | Math.min(Math.abs(newY - snap.y), 1000) |
| 593 | ), 300); |
| 594 | newX = snap.x; |
| 595 | newY = snap.y; |
| 596 | |
| 597 | this.directionX = 0; |
| 598 | this.directionY = 0; |
| 599 | easing = this.options.bounceEasing; |
| 600 | } |
| 601 | |
| 602 | // INSERT POINT: _end |
| 603 | |
| 604 | if ( newX != this.x || newY != this.y ) { |
| 605 | // change easing function when scroller goes out of the boundaries |
| 606 | if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) { |
| 607 | easing = utils.ease.quadratic; |
| 608 | } |
| 609 | |
| 610 | this.scrollTo(newX, newY, time, easing); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | this._execEvent('scrollEnd'); |
| 615 | }, |
| 616 | |
| 617 | _resize: function () { |
| 618 | var that = this; |
| 619 | |
| 620 | clearTimeout(this.resizeTimeout); |
| 621 | |
| 622 | this.resizeTimeout = setTimeout(function () { |
| 623 | that.refresh(); |
| 624 | }, this.options.resizePolling); |
| 625 | }, |
| 626 | |
| 627 | resetPosition: function (time) { |
| 628 | var x = this.x, |
| 629 | y = this.y; |
| 630 | |
| 631 | time = time || 0; |
| 632 | |
| 633 | if ( !this.hasHorizontalScroll || this.x > 0 ) { |
| 634 | x = 0; |
| 635 | } else if ( this.x < this.maxScrollX ) { |
| 636 | x = this.maxScrollX; |
| 637 | } |
| 638 | |
| 639 | if ( !this.hasVerticalScroll || this.y > 0 ) { |
| 640 | y = 0; |
| 641 | } else if ( this.y < this.maxScrollY ) { |
| 642 | y = this.maxScrollY; |
| 643 | } |
| 644 | |
| 645 | if ( x == this.x && y == this.y ) { |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | this.scrollTo(x, y, time, this.options.bounceEasing); |
| 650 | |
| 651 | return true; |
| 652 | }, |
| 653 | |
| 654 | disable: function () { |
| 655 | this.enabled = false; |
| 656 | }, |
| 657 | |
| 658 | enable: function () { |
| 659 | this.enabled = true; |
| 660 | }, |
| 661 | |
| 662 | refresh: function () { |
| 663 | var rf = this.wrapper.offsetHeight; // Force reflow |
| 664 | |
| 665 | this.wrapperWidth = this.wrapper.clientWidth; |
| 666 | this.wrapperHeight = this.wrapper.clientHeight; |
| 667 | |
| 668 | /* REPLACE START: refresh */ |
| 669 | this.scrollerWidth = Math.round(this.scroller.offsetWidth * this.scale); |
| 670 | this.scrollerHeight = Math.round(this.scroller.offsetHeight * this.scale); |
| 671 | |
| 672 | this.maxScrollX = this.wrapperWidth - this.scrollerWidth; |
| 673 | this.maxScrollY = this.wrapperHeight - this.scrollerHeight; |
| 674 | /* REPLACE END: refresh */ |
| 675 | |
| 676 | this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0; |
| 677 | this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0; |
| 678 | |
| 679 | if ( !this.hasHorizontalScroll ) { |
| 680 | this.maxScrollX = 0; |
| 681 | this.scrollerWidth = this.wrapperWidth; |
| 682 | } |
| 683 | |
| 684 | if ( !this.hasVerticalScroll ) { |
| 685 | this.maxScrollY = 0; |
| 686 | this.scrollerHeight = this.wrapperHeight; |
| 687 | } |
| 688 | |
| 689 | this.endTime = 0; |
| 690 | this.directionX = 0; |
| 691 | this.directionY = 0; |
| 692 | |
| 693 | this.wrapperOffset = utils.offset(this.wrapper); |
| 694 | |
| 695 | this._execEvent('refresh'); |
| 696 | |
| 697 | this.resetPosition(); |
| 698 | |
| 699 | // INSERT POINT: _refresh |
| 700 | |
| 701 | }, |
| 702 | |
| 703 | on: function (type, fn) { |
| 704 | if ( !this._events[type] ) { |
| 705 | this._events[type] = []; |
| 706 | } |
| 707 | |
| 708 | this._events[type].push(fn); |
| 709 | }, |
| 710 | |
| 711 | off: function (type, fn) { |
| 712 | if ( !this._events[type] ) { |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | var index = this._events[type].indexOf(fn); |
| 717 | |
| 718 | if ( index > -1 ) { |
| 719 | this._events[type].splice(index, 1); |
| 720 | } |
| 721 | }, |
| 722 | |
| 723 | _execEvent: function (type) { |
| 724 | if ( !this._events[type] ) { |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | var i = 0, |
| 729 | l = this._events[type].length; |
| 730 | |
| 731 | if ( !l ) { |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | for ( ; i < l; i++ ) { |
| 736 | this._events[type][i].apply(this, [].slice.call(arguments, 1)); |
| 737 | } |
| 738 | }, |
| 739 | |
| 740 | scrollBy: function (x, y, time, easing) { |
| 741 | x = this.x + x; |
| 742 | y = this.y + y; |
| 743 | time = time || 0; |
| 744 | |
| 745 | this.scrollTo(x, y, time, easing); |
| 746 | }, |
| 747 | |
| 748 | scrollTo: function (x, y, time, easing) { |
| 749 | easing = easing || utils.ease.circular; |
| 750 | |
| 751 | this.isInTransition = this.options.useTransition && time > 0; |
| 752 | |
| 753 | if ( !time || (this.options.useTransition && easing.style) ) { |
| 754 | this._transitionTimingFunction(easing.style); |
| 755 | this._transitionTime(time); |
| 756 | this._translate(x, y); |
| 757 | } else { |
| 758 | this._animate(x, y, time, easing.fn); |
| 759 | } |
| 760 | }, |
| 761 | |
| 762 | scrollToElement: function (el, time, offsetX, offsetY, easing) { |
| 763 | el = el.nodeType ? el : this.scroller.querySelector(el); |
| 764 | |
| 765 | if ( !el ) { |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | var pos = utils.offset(el); |
| 770 | |
| 771 | pos.left -= this.wrapperOffset.left; |
| 772 | pos.top -= this.wrapperOffset.top; |
| 773 | |
| 774 | // if offsetX/Y are true we center the element to the screen |
| 775 | if ( offsetX === true ) { |
| 776 | offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2); |
| 777 | } |
| 778 | if ( offsetY === true ) { |
| 779 | offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2); |
| 780 | } |
| 781 | |
| 782 | pos.left -= offsetX || 0; |
| 783 | pos.top -= offsetY || 0; |
| 784 | |
| 785 | pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left; |
| 786 | pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top; |
| 787 | |
| 788 | time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time; |
| 789 | |
| 790 | this.scrollTo(pos.left, pos.top, time, easing); |
| 791 | }, |
| 792 | |
| 793 | _transitionTime: function (time) { |
| 794 | time = time || 0; |
| 795 | |
| 796 | this.scrollerStyle[utils.style.transitionDuration] = time + 'ms'; |
| 797 | |
| 798 | if ( !time && utils.isBadAndroid ) { |
| 799 | this.scrollerStyle[utils.style.transitionDuration] = '0.001s'; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | if ( this.indicators ) { |
| 804 | for ( var i = this.indicators.length; i--; ) { |
| 805 | this.indicators[i].transitionTime(time); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | |
| 810 | // INSERT POINT: _transitionTime |
| 811 | |
| 812 | }, |
| 813 | |
| 814 | _transitionTimingFunction: function (easing) { |
| 815 | this.scrollerStyle[utils.style.transitionTimingFunction] = easing; |
| 816 | |
| 817 | |
| 818 | if ( this.indicators ) { |
| 819 | for ( var i = this.indicators.length; i--; ) { |
| 820 | this.indicators[i].transitionTimingFunction(easing); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | |
| 825 | // INSERT POINT: _transitionTimingFunction |
| 826 | |
| 827 | }, |
| 828 | |
| 829 | _translate: function (x, y) { |
| 830 | if ( this.options.useTransform ) { |
| 831 | |
| 832 | /* REPLACE START: _translate */ this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px) scale(' + this.scale + ') ' + this.translateZ;/* REPLACE END: _translate */ |
| 833 | |
| 834 | } else { |
| 835 | x = Math.round(x); |
| 836 | y = Math.round(y); |
| 837 | this.scrollerStyle.left = x + 'px'; |
| 838 | this.scrollerStyle.top = y + 'px'; |
| 839 | } |
| 840 | |
| 841 | this.x = x; |
| 842 | this.y = y; |
| 843 | |
| 844 | |
| 845 | if ( this.indicators ) { |
| 846 | for ( var i = this.indicators.length; i--; ) { |
| 847 | this.indicators[i].updatePosition(); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | |
| 852 | // INSERT POINT: _translate |
| 853 | |
| 854 | }, |
| 855 | |
| 856 | _initEvents: function (remove) { |
| 857 | var eventType = remove ? utils.removeEvent : utils.addEvent, |
| 858 | target = this.options.bindToWrapper ? this.wrapper : window; |
| 859 | |
| 860 | eventType(window, 'orientationchange', this); |
| 861 | eventType(window, 'resize', this); |
| 862 | |
| 863 | if ( this.options.click ) { |
| 864 | eventType(this.wrapper, 'click', this, true); |
| 865 | } |
| 866 | |
| 867 | if ( !this.options.disableMouse ) { |
| 868 | eventType(this.wrapper, 'mousedown', this); |
| 869 | eventType(target, 'mousemove', this); |
| 870 | eventType(target, 'mousecancel', this); |
| 871 | eventType(target, 'mouseup', this); |
| 872 | } |
| 873 | |
| 874 | if ( utils.hasPointer && !this.options.disablePointer ) { |
| 875 | eventType(this.wrapper, 'MSPointerDown', this); |
| 876 | eventType(target, 'MSPointerMove', this); |
| 877 | eventType(target, 'MSPointerCancel', this); |
| 878 | eventType(target, 'MSPointerUp', this); |
| 879 | } |
| 880 | |
| 881 | if ( utils.hasTouch && !this.options.disableTouch ) { |
| 882 | eventType(this.wrapper, 'touchstart', this); |
| 883 | eventType(target, 'touchmove', this); |
| 884 | eventType(target, 'touchcancel', this); |
| 885 | eventType(target, 'touchend', this); |
| 886 | } |
| 887 | |
| 888 | eventType(this.scroller, 'transitionend', this); |
| 889 | eventType(this.scroller, 'webkitTransitionEnd', this); |
| 890 | eventType(this.scroller, 'oTransitionEnd', this); |
| 891 | eventType(this.scroller, 'MSTransitionEnd', this); |
| 892 | }, |
| 893 | |
| 894 | getComputedPosition: function () { |
| 895 | var matrix = window.getComputedStyle(this.scroller, null), |
| 896 | x, y; |
| 897 | |
| 898 | if ( this.options.useTransform ) { |
| 899 | matrix = matrix[utils.style.transform].split(')')[0].split(', '); |
| 900 | x = +(matrix[12] || matrix[4]); |
| 901 | y = +(matrix[13] || matrix[5]); |
| 902 | } else { |
| 903 | x = +matrix.left.replace(/[^-\d.]/g, ''); |
| 904 | y = +matrix.top.replace(/[^-\d.]/g, ''); |
| 905 | } |
| 906 | |
| 907 | return { x: x, y: y }; |
| 908 | }, |
| 909 | |
| 910 | _initIndicators: function () { |
| 911 | var interactive = this.options.interactiveScrollbars, |
| 912 | customStyle = typeof this.options.scrollbars != 'string', |
| 913 | indicators = [], |
| 914 | indicator; |
| 915 | |
| 916 | var that = this; |
| 917 | |
| 918 | this.indicators = []; |
| 919 | |
| 920 | if ( this.options.scrollbars ) { |
| 921 | // Vertical scrollbar |
| 922 | if ( this.options.scrollY ) { |
| 923 | indicator = { |
| 924 | el: createDefaultScrollbar('v', interactive, this.options.scrollbars), |
| 925 | interactive: interactive, |
| 926 | defaultScrollbars: true, |
| 927 | customStyle: customStyle, |
| 928 | resize: this.options.resizeScrollbars, |
| 929 | shrink: this.options.shrinkScrollbars, |
| 930 | fade: this.options.fadeScrollbars, |
| 931 | listenX: false |
| 932 | }; |
| 933 | |
| 934 | this.wrapper.appendChild(indicator.el); |
| 935 | indicators.push(indicator); |
| 936 | } |
| 937 | |
| 938 | // Horizontal scrollbar |
| 939 | if ( this.options.scrollX ) { |
| 940 | indicator = { |
| 941 | el: createDefaultScrollbar('h', interactive, this.options.scrollbars), |
| 942 | interactive: interactive, |
| 943 | defaultScrollbars: true, |
| 944 | customStyle: customStyle, |
| 945 | resize: this.options.resizeScrollbars, |
| 946 | shrink: this.options.shrinkScrollbars, |
| 947 | fade: this.options.fadeScrollbars, |
| 948 | listenY: false |
| 949 | }; |
| 950 | |
| 951 | this.wrapper.appendChild(indicator.el); |
| 952 | indicators.push(indicator); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | if ( this.options.indicators ) { |
| 957 | // TODO: check concat compatibility |
| 958 | indicators = indicators.concat(this.options.indicators); |
| 959 | } |
| 960 | |
| 961 | for ( var i = indicators.length; i--; ) { |
| 962 | this.indicators.push( new Indicator(this, indicators[i]) ); |
| 963 | } |
| 964 | |
| 965 | // TODO: check if we can use array.map (wide compatibility and performance issues) |
| 966 | function _indicatorsMap (fn) { |
| 967 | for ( var i = that.indicators.length; i--; ) { |
| 968 | fn.call(that.indicators[i]); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | if ( this.options.fadeScrollbars ) { |
| 973 | this.on('scrollEnd', function () { |
| 974 | _indicatorsMap(function () { |
| 975 | this.fade(); |
| 976 | }); |
| 977 | }); |
| 978 | |
| 979 | this.on('scrollCancel', function () { |
| 980 | _indicatorsMap(function () { |
| 981 | this.fade(); |
| 982 | }); |
| 983 | }); |
| 984 | |
| 985 | this.on('scrollStart', function () { |
| 986 | _indicatorsMap(function () { |
| 987 | this.fade(1); |
| 988 | }); |
| 989 | }); |
| 990 | |
| 991 | this.on('beforeScrollStart', function () { |
| 992 | _indicatorsMap(function () { |
| 993 | this.fade(1, true); |
| 994 | }); |
| 995 | }); |
| 996 | } |
| 997 | |
| 998 | |
| 999 | this.on('refresh', function () { |
| 1000 | _indicatorsMap(function () { |
| 1001 | this.refresh(); |
| 1002 | }); |
| 1003 | }); |
| 1004 | |
| 1005 | this.on('destroy', function () { |
| 1006 | _indicatorsMap(function () { |
| 1007 | this.destroy(); |
| 1008 | }); |
| 1009 | |
| 1010 | delete this.indicators; |
| 1011 | }); |
| 1012 | }, |
| 1013 | |
| 1014 | _initZoom: function () { |
| 1015 | this.scrollerStyle[utils.style.transformOrigin] = '0 0'; |
| 1016 | }, |
| 1017 | |
| 1018 | _zoomStart: function (e) { |
| 1019 | var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ), |
| 1020 | c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY ); |
| 1021 | |
| 1022 | this.touchesDistanceStart = Math.sqrt(c1 * c1 + c2 * c2); |
| 1023 | this.startScale = this.scale; |
| 1024 | |
| 1025 | this.originX = Math.abs(e.touches[0].pageX + e.touches[1].pageX) / 2 + this.wrapperOffset.left - this.x; |
| 1026 | this.originY = Math.abs(e.touches[0].pageY + e.touches[1].pageY) / 2 + this.wrapperOffset.top - this.y; |
| 1027 | |
| 1028 | this._execEvent('zoomStart'); |
| 1029 | }, |
| 1030 | |
| 1031 | _zoom: function (e) { |
| 1032 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | if ( this.options.preventDefault ) { |
| 1037 | e.preventDefault(); |
| 1038 | } |
| 1039 | |
| 1040 | var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ), |
| 1041 | c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY ), |
| 1042 | distance = Math.sqrt( c1 * c1 + c2 * c2 ), |
| 1043 | scale = 1 / this.touchesDistanceStart * distance * this.startScale, |
| 1044 | lastScale, |
| 1045 | x, y; |
| 1046 | |
| 1047 | this.scaled = true; |
| 1048 | |
| 1049 | if ( scale < this.options.zoomMin ) { |
| 1050 | scale = 0.5 * this.options.zoomMin * Math.pow(2.0, scale / this.options.zoomMin); |
| 1051 | } else if ( scale > this.options.zoomMax ) { |
| 1052 | scale = 2.0 * this.options.zoomMax * Math.pow(0.5, this.options.zoomMax / scale); |
| 1053 | } |
| 1054 | |
| 1055 | lastScale = scale / this.startScale; |
| 1056 | x = this.originX - this.originX * lastScale + this.startX; |
| 1057 | y = this.originY - this.originY * lastScale + this.startY; |
| 1058 | |
| 1059 | this.scale = scale; |
| 1060 | |
| 1061 | this.scrollTo(x, y, 0); |
| 1062 | }, |
| 1063 | |
| 1064 | _zoomEnd: function (e) { |
| 1065 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | if ( this.options.preventDefault ) { |
| 1070 | e.preventDefault(); |
| 1071 | } |
| 1072 | |
| 1073 | var newX, newY, |
| 1074 | lastScale; |
| 1075 | |
| 1076 | this.isInTransition = 0; |
| 1077 | this.initiated = 0; |
| 1078 | |
| 1079 | if ( this.scale > this.options.zoomMax ) { |
| 1080 | this.scale = this.options.zoomMax; |
| 1081 | } else if ( this.scale < this.options.zoomMin ) { |
| 1082 | this.scale = this.options.zoomMin; |
| 1083 | } |
| 1084 | |
| 1085 | // Update boundaries |
| 1086 | this.refresh(); |
| 1087 | |
| 1088 | lastScale = this.scale / this.startScale; |
| 1089 | |
| 1090 | newX = this.originX - this.originX * lastScale + this.startX; |
| 1091 | newY = this.originY - this.originY * lastScale + this.startY; |
| 1092 | |
| 1093 | if ( newX > 0 ) { |
| 1094 | newX = 0; |
| 1095 | } else if ( newX < this.maxScrollX ) { |
| 1096 | newX = this.maxScrollX; |
| 1097 | } |
| 1098 | |
| 1099 | if ( newY > 0 ) { |
| 1100 | newY = 0; |
| 1101 | } else if ( newY < this.maxScrollY ) { |
| 1102 | newY = this.maxScrollY; |
| 1103 | } |
| 1104 | |
| 1105 | if ( this.x != newX || this.y != newY ) { |
| 1106 | this.scrollTo(newX, newY, this.options.bounceTime); |
| 1107 | } |
| 1108 | |
| 1109 | this.scaled = false; |
| 1110 | |
| 1111 | this._execEvent('zoomEnd'); |
| 1112 | }, |
| 1113 | |
| 1114 | zoom: function (scale, x, y, time) { |
| 1115 | if ( scale < this.options.zoomMin ) { |
| 1116 | scale = this.options.zoomMin; |
| 1117 | } else if ( scale > this.options.zoomMax ) { |
| 1118 | scale = this.options.zoomMax; |
| 1119 | } |
| 1120 | |
| 1121 | if ( scale == this.scale ) { |
| 1122 | return; |
| 1123 | } |
| 1124 | |
| 1125 | var relScale = scale / this.scale; |
| 1126 | |
| 1127 | x = x === undefined ? this.wrapperWidth / 2 : x; |
| 1128 | y = y === undefined ? this.wrapperHeight / 2 : y; |
| 1129 | time = time === undefined ? 300 : time; |
| 1130 | |
| 1131 | x = x + this.wrapperOffset.left - this.x; |
| 1132 | y = y + this.wrapperOffset.top - this.y; |
| 1133 | |
| 1134 | x = x - x * relScale + this.x; |
| 1135 | y = y - y * relScale + this.y; |
| 1136 | |
| 1137 | this.scale = scale; |
| 1138 | |
| 1139 | this.refresh(); // update boundaries |
| 1140 | |
| 1141 | if ( x > 0 ) { |
| 1142 | x = 0; |
| 1143 | } else if ( x < this.maxScrollX ) { |
| 1144 | x = this.maxScrollX; |
| 1145 | } |
| 1146 | |
| 1147 | if ( y > 0 ) { |
| 1148 | y = 0; |
| 1149 | } else if ( y < this.maxScrollY ) { |
| 1150 | y = this.maxScrollY; |
| 1151 | } |
| 1152 | |
| 1153 | this.scrollTo(x, y, time); |
| 1154 | }, |
| 1155 | |
| 1156 | _wheelZoom: function (e) { |
| 1157 | var wheelDeltaY, |
| 1158 | deltaScale, |
| 1159 | that = this; |
| 1160 | |
| 1161 | // Execute the zoomEnd event after 400ms the wheel stopped scrolling |
| 1162 | clearTimeout(this.wheelTimeout); |
| 1163 | this.wheelTimeout = setTimeout(function () { |
| 1164 | that._execEvent('zoomEnd'); |
| 1165 | }, 400); |
| 1166 | |
| 1167 | if ( 'deltaX' in e ) { |
| 1168 | wheelDeltaY = -e.deltaY / Math.abs(e.deltaY); |
| 1169 | } else if ('wheelDeltaX' in e) { |
| 1170 | wheelDeltaY = e.wheelDeltaY / Math.abs(e.wheelDeltaY); |
| 1171 | } else if('wheelDelta' in e) { |
| 1172 | wheelDeltaY = e.wheelDelta / Math.abs(e.wheelDelta); |
| 1173 | } else if ('detail' in e) { |
| 1174 | wheelDeltaY = -e.detail / Math.abs(e.wheelDelta); |
| 1175 | } else { |
| 1176 | return; |
| 1177 | } |
| 1178 | |
| 1179 | deltaScale = this.scale + wheelDeltaY / 5; |
| 1180 | |
| 1181 | this.zoom(deltaScale, e.pageX, e.pageY, 0); |
| 1182 | }, |
| 1183 | |
| 1184 | _initWheel: function () { |
| 1185 | utils.addEvent(this.wrapper, 'wheel', this); |
| 1186 | utils.addEvent(this.wrapper, 'mousewheel', this); |
| 1187 | utils.addEvent(this.wrapper, 'DOMMouseScroll', this); |
| 1188 | |
| 1189 | this.on('destroy', function () { |
| 1190 | utils.removeEvent(this.wrapper, 'wheel', this); |
| 1191 | utils.removeEvent(this.wrapper, 'mousewheel', this); |
| 1192 | utils.removeEvent(this.wrapper, 'DOMMouseScroll', this); |
| 1193 | }); |
| 1194 | }, |
| 1195 | |
| 1196 | _wheel: function (e) { |
| 1197 | if ( !this.enabled ) { |
| 1198 | return; |
| 1199 | } |
| 1200 | |
| 1201 | e.preventDefault(); |
| 1202 | e.stopPropagation(); |
| 1203 | |
| 1204 | var wheelDeltaX, wheelDeltaY, |
| 1205 | newX, newY, |
| 1206 | that = this; |
| 1207 | |
| 1208 | if ( this.wheelTimeout === undefined ) { |
| 1209 | that._execEvent('scrollStart'); |
| 1210 | } |
| 1211 | |
| 1212 | // Execute the scrollEnd event after 400ms the wheel stopped scrolling |
| 1213 | clearTimeout(this.wheelTimeout); |
| 1214 | this.wheelTimeout = setTimeout(function () { |
| 1215 | that._execEvent('scrollEnd'); |
| 1216 | that.wheelTimeout = undefined; |
| 1217 | }, 400); |
| 1218 | |
| 1219 | if ( 'deltaX' in e ) { |
| 1220 | wheelDeltaX = -e.deltaX; |
| 1221 | wheelDeltaY = -e.deltaY; |
| 1222 | } else if ( 'wheelDeltaX' in e ) { |
| 1223 | wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed; |
| 1224 | wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed; |
| 1225 | } else if ( 'wheelDelta' in e ) { |
| 1226 | wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed; |
| 1227 | } else if ( 'detail' in e ) { |
| 1228 | wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed; |
| 1229 | } else { |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | wheelDeltaX *= this.options.invertWheelDirection; |
| 1234 | wheelDeltaY *= this.options.invertWheelDirection; |
| 1235 | |
| 1236 | if ( !this.hasVerticalScroll ) { |
| 1237 | wheelDeltaX = wheelDeltaY; |
| 1238 | wheelDeltaY = 0; |
| 1239 | } |
| 1240 | |
| 1241 | if ( this.options.snap ) { |
| 1242 | newX = this.currentPage.pageX; |
| 1243 | newY = this.currentPage.pageY; |
| 1244 | |
| 1245 | if ( wheelDeltaX > 0 ) { |
| 1246 | newX--; |
| 1247 | } else if ( wheelDeltaX < 0 ) { |
| 1248 | newX++; |
| 1249 | } |
| 1250 | |
| 1251 | if ( wheelDeltaY > 0 ) { |
| 1252 | newY--; |
| 1253 | } else if ( wheelDeltaY < 0 ) { |
| 1254 | newY++; |
| 1255 | } |
| 1256 | |
| 1257 | this.goToPage(newX, newY); |
| 1258 | |
| 1259 | return; |
| 1260 | } |
| 1261 | |
| 1262 | newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0); |
| 1263 | newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0); |
| 1264 | |
| 1265 | if ( newX > 0 ) { |
| 1266 | newX = 0; |
| 1267 | } else if ( newX < this.maxScrollX ) { |
| 1268 | newX = this.maxScrollX; |
| 1269 | } |
| 1270 | |
| 1271 | if ( newY > 0 ) { |
| 1272 | newY = 0; |
| 1273 | } else if ( newY < this.maxScrollY ) { |
| 1274 | newY = this.maxScrollY; |
| 1275 | } |
| 1276 | |
| 1277 | this.scrollTo(newX, newY, 0); |
| 1278 | |
| 1279 | // INSERT POINT: _wheel |
| 1280 | }, |
| 1281 | |
| 1282 | _initSnap: function () { |
| 1283 | this.currentPage = {}; |
| 1284 | |
| 1285 | if ( typeof this.options.snap == 'string' ) { |
| 1286 | this.options.snap = this.scroller.querySelectorAll(this.options.snap); |
| 1287 | } |
| 1288 | |
| 1289 | this.on('refresh', function () { |
| 1290 | var i = 0, l, |
| 1291 | m = 0, n, |
| 1292 | cx, cy, |
| 1293 | x = 0, y, |
| 1294 | stepX = this.options.snapStepX || this.wrapperWidth, |
| 1295 | stepY = this.options.snapStepY || this.wrapperHeight, |
| 1296 | el; |
| 1297 | |
| 1298 | this.pages = []; |
| 1299 | |
| 1300 | if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) { |
| 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | if ( this.options.snap === true ) { |
| 1305 | cx = Math.round( stepX / 2 ); |
| 1306 | cy = Math.round( stepY / 2 ); |
| 1307 | |
| 1308 | while ( x > -this.scrollerWidth ) { |
| 1309 | this.pages[i] = []; |
| 1310 | l = 0; |
| 1311 | y = 0; |
| 1312 | |
| 1313 | while ( y > -this.scrollerHeight ) { |
| 1314 | this.pages[i][l] = { |
| 1315 | x: Math.max(x, this.maxScrollX), |
| 1316 | y: Math.max(y, this.maxScrollY), |
| 1317 | width: stepX, |
| 1318 | height: stepY, |
| 1319 | cx: x - cx, |
| 1320 | cy: y - cy |
| 1321 | }; |
| 1322 | |
| 1323 | y -= stepY; |
| 1324 | l++; |
| 1325 | } |
| 1326 | |
| 1327 | x -= stepX; |
| 1328 | i++; |
| 1329 | } |
| 1330 | } else { |
| 1331 | el = this.options.snap; |
| 1332 | l = el.length; |
| 1333 | n = -1; |
| 1334 | |
| 1335 | for ( ; i < l; i++ ) { |
| 1336 | if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) { |
| 1337 | m = 0; |
| 1338 | n++; |
| 1339 | } |
| 1340 | |
| 1341 | if ( !this.pages[m] ) { |
| 1342 | this.pages[m] = []; |
| 1343 | } |
| 1344 | |
| 1345 | x = Math.max(-el[i].offsetLeft, this.maxScrollX); |
| 1346 | y = Math.max(-el[i].offsetTop, this.maxScrollY); |
| 1347 | cx = x - Math.round(el[i].offsetWidth / 2); |
| 1348 | cy = y - Math.round(el[i].offsetHeight / 2); |
| 1349 | |
| 1350 | this.pages[m][n] = { |
| 1351 | x: x, |
| 1352 | y: y, |
| 1353 | width: el[i].offsetWidth, |
| 1354 | height: el[i].offsetHeight, |
| 1355 | cx: cx, |
| 1356 | cy: cy |
| 1357 | }; |
| 1358 | |
| 1359 | if ( x > this.maxScrollX ) { |
| 1360 | m++; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0); |
| 1366 | |
| 1367 | // Update snap threshold if needed |
| 1368 | if ( this.options.snapThreshold % 1 === 0 ) { |
| 1369 | this.snapThresholdX = this.options.snapThreshold; |
| 1370 | this.snapThresholdY = this.options.snapThreshold; |
| 1371 | } else { |
| 1372 | this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold); |
| 1373 | this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold); |
| 1374 | } |
| 1375 | }); |
| 1376 | |
| 1377 | this.on('flick', function () { |
| 1378 | var time = this.options.snapSpeed || Math.max( |
| 1379 | Math.max( |
| 1380 | Math.min(Math.abs(this.x - this.startX), 1000), |
| 1381 | Math.min(Math.abs(this.y - this.startY), 1000) |
| 1382 | ), 300); |
| 1383 | |
| 1384 | this.goToPage( |
| 1385 | this.currentPage.pageX + this.directionX, |
| 1386 | this.currentPage.pageY + this.directionY, |
| 1387 | time |
| 1388 | ); |
| 1389 | }); |
| 1390 | }, |
| 1391 | |
| 1392 | _nearestSnap: function (x, y) { |
| 1393 | if ( !this.pages.length ) { |
| 1394 | return { x: 0, y: 0, pageX: 0, pageY: 0 }; |
| 1395 | } |
| 1396 | |
| 1397 | var i = 0, |
| 1398 | l = this.pages.length, |
| 1399 | m = 0; |
| 1400 | |
| 1401 | // Check if we exceeded the snap threshold |
| 1402 | if ( Math.abs(x - this.absStartX) < this.snapThresholdX && |
| 1403 | Math.abs(y - this.absStartY) < this.snapThresholdY ) { |
| 1404 | return this.currentPage; |
| 1405 | } |
| 1406 | |
| 1407 | if ( x > 0 ) { |
| 1408 | x = 0; |
| 1409 | } else if ( x < this.maxScrollX ) { |
| 1410 | x = this.maxScrollX; |
| 1411 | } |
| 1412 | |
| 1413 | if ( y > 0 ) { |
| 1414 | y = 0; |
| 1415 | } else if ( y < this.maxScrollY ) { |
| 1416 | y = this.maxScrollY; |
| 1417 | } |
| 1418 | |
| 1419 | for ( ; i < l; i++ ) { |
| 1420 | if ( x >= this.pages[i][0].cx ) { |
| 1421 | x = this.pages[i][0].x; |
| 1422 | break; |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | l = this.pages[i].length; |
| 1427 | |
| 1428 | for ( ; m < l; m++ ) { |
| 1429 | if ( y >= this.pages[0][m].cy ) { |
| 1430 | y = this.pages[0][m].y; |
| 1431 | break; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | if ( i == this.currentPage.pageX ) { |
| 1436 | i += this.directionX; |
| 1437 | |
| 1438 | if ( i < 0 ) { |
| 1439 | i = 0; |
| 1440 | } else if ( i >= this.pages.length ) { |
| 1441 | i = this.pages.length - 1; |
| 1442 | } |
| 1443 | |
| 1444 | x = this.pages[i][0].x; |
| 1445 | } |
| 1446 | |
| 1447 | if ( m == this.currentPage.pageY ) { |
| 1448 | m += this.directionY; |
| 1449 | |
| 1450 | if ( m < 0 ) { |
| 1451 | m = 0; |
| 1452 | } else if ( m >= this.pages[0].length ) { |
| 1453 | m = this.pages[0].length - 1; |
| 1454 | } |
| 1455 | |
| 1456 | y = this.pages[0][m].y; |
| 1457 | } |
| 1458 | |
| 1459 | return { |
| 1460 | x: x, |
| 1461 | y: y, |
| 1462 | pageX: i, |
| 1463 | pageY: m |
| 1464 | }; |
| 1465 | }, |
| 1466 | |
| 1467 | goToPage: function (x, y, time, easing) { |
| 1468 | easing = easing || this.options.bounceEasing; |
| 1469 | |
| 1470 | if ( x >= this.pages.length ) { |
| 1471 | x = this.pages.length - 1; |
| 1472 | } else if ( x < 0 ) { |
| 1473 | x = 0; |
| 1474 | } |
| 1475 | |
| 1476 | if ( y >= this.pages[x].length ) { |
| 1477 | y = this.pages[x].length - 1; |
| 1478 | } else if ( y < 0 ) { |
| 1479 | y = 0; |
| 1480 | } |
| 1481 | |
| 1482 | var posX = this.pages[x][y].x, |
| 1483 | posY = this.pages[x][y].y; |
| 1484 | |
| 1485 | time = time === undefined ? this.options.snapSpeed || Math.max( |
| 1486 | Math.max( |
| 1487 | Math.min(Math.abs(posX - this.x), 1000), |
| 1488 | Math.min(Math.abs(posY - this.y), 1000) |
| 1489 | ), 300) : time; |
| 1490 | |
| 1491 | this.currentPage = { |
| 1492 | x: posX, |
| 1493 | y: posY, |
| 1494 | pageX: x, |
| 1495 | pageY: y |
| 1496 | }; |
| 1497 | |
| 1498 | this.scrollTo(posX, posY, time, easing); |
| 1499 | }, |
| 1500 | |
| 1501 | next: function (time, easing) { |
| 1502 | var x = this.currentPage.pageX, |
| 1503 | y = this.currentPage.pageY; |
| 1504 | |
| 1505 | x++; |
| 1506 | |
| 1507 | if ( x >= this.pages.length && this.hasVerticalScroll ) { |
| 1508 | x = 0; |
| 1509 | y++; |
| 1510 | } |
| 1511 | |
| 1512 | this.goToPage(x, y, time, easing); |
| 1513 | }, |
| 1514 | |
| 1515 | prev: function (time, easing) { |
| 1516 | var x = this.currentPage.pageX, |
| 1517 | y = this.currentPage.pageY; |
| 1518 | |
| 1519 | x--; |
| 1520 | |
| 1521 | if ( x < 0 && this.hasVerticalScroll ) { |
| 1522 | x = 0; |
| 1523 | y--; |
| 1524 | } |
| 1525 | |
| 1526 | this.goToPage(x, y, time, easing); |
| 1527 | }, |
| 1528 | |
| 1529 | _initKeys: function (e) { |
| 1530 | // default key bindings |
| 1531 | var keys = { |
| 1532 | pageUp: 33, |
| 1533 | pageDown: 34, |
| 1534 | end: 35, |
| 1535 | home: 36, |
| 1536 | left: 37, |
| 1537 | up: 38, |
| 1538 | right: 39, |
| 1539 | down: 40 |
| 1540 | }; |
| 1541 | var i; |
| 1542 | |
| 1543 | // if you give me characters I give you keycode |
| 1544 | if ( typeof this.options.keyBindings == 'object' ) { |
| 1545 | for ( i in this.options.keyBindings ) { |
| 1546 | if ( typeof this.options.keyBindings[i] == 'string' ) { |
| 1547 | this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0); |
| 1548 | } |
| 1549 | } |
| 1550 | } else { |
| 1551 | this.options.keyBindings = {}; |
| 1552 | } |
| 1553 | |
| 1554 | for ( i in keys ) { |
| 1555 | this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i]; |
| 1556 | } |
| 1557 | |
| 1558 | utils.addEvent(window, 'keydown', this); |
| 1559 | |
| 1560 | this.on('destroy', function () { |
| 1561 | utils.removeEvent(window, 'keydown', this); |
| 1562 | }); |
| 1563 | }, |
| 1564 | |
| 1565 | _key: function (e) { |
| 1566 | if ( !this.enabled ) { |
| 1567 | return; |
| 1568 | } |
| 1569 | |
| 1570 | var snap = this.options.snap, // we are using this alot, better to cache it |
| 1571 | newX = snap ? this.currentPage.pageX : this.x, |
| 1572 | newY = snap ? this.currentPage.pageY : this.y, |
| 1573 | now = utils.getTime(), |
| 1574 | prevTime = this.keyTime || 0, |
| 1575 | acceleration = 0.250, |
| 1576 | pos; |
| 1577 | |
| 1578 | if ( this.options.useTransition && this.isInTransition ) { |
| 1579 | pos = this.getComputedPosition(); |
| 1580 | |
| 1581 | this._translate(Math.round(pos.x), Math.round(pos.y)); |
| 1582 | this.isInTransition = false; |
| 1583 | } |
| 1584 | |
| 1585 | this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0; |
| 1586 | |
| 1587 | switch ( e.keyCode ) { |
| 1588 | case this.options.keyBindings.pageUp: |
| 1589 | if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) { |
| 1590 | newX += snap ? 1 : this.wrapperWidth; |
| 1591 | } else { |
| 1592 | newY += snap ? 1 : this.wrapperHeight; |
| 1593 | } |
| 1594 | break; |
| 1595 | case this.options.keyBindings.pageDown: |
| 1596 | if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) { |
| 1597 | newX -= snap ? 1 : this.wrapperWidth; |
| 1598 | } else { |
| 1599 | newY -= snap ? 1 : this.wrapperHeight; |
| 1600 | } |
| 1601 | break; |
| 1602 | case this.options.keyBindings.end: |
| 1603 | newX = snap ? this.pages.length-1 : this.maxScrollX; |
| 1604 | newY = snap ? this.pages[0].length-1 : this.maxScrollY; |
| 1605 | break; |
| 1606 | case this.options.keyBindings.home: |
| 1607 | newX = 0; |
| 1608 | newY = 0; |
| 1609 | break; |
| 1610 | case this.options.keyBindings.left: |
| 1611 | newX += snap ? -1 : 5 + this.keyAcceleration>>0; |
| 1612 | break; |
| 1613 | case this.options.keyBindings.up: |
| 1614 | newY += snap ? 1 : 5 + this.keyAcceleration>>0; |
| 1615 | break; |
| 1616 | case this.options.keyBindings.right: |
| 1617 | newX -= snap ? -1 : 5 + this.keyAcceleration>>0; |
| 1618 | break; |
| 1619 | case this.options.keyBindings.down: |
| 1620 | newY -= snap ? 1 : 5 + this.keyAcceleration>>0; |
| 1621 | break; |
| 1622 | default: |
| 1623 | return; |
| 1624 | } |
| 1625 | |
| 1626 | if ( snap ) { |
| 1627 | this.goToPage(newX, newY); |
| 1628 | return; |
| 1629 | } |
| 1630 | |
| 1631 | if ( newX > 0 ) { |
| 1632 | newX = 0; |
| 1633 | this.keyAcceleration = 0; |
| 1634 | } else if ( newX < this.maxScrollX ) { |
| 1635 | newX = this.maxScrollX; |
| 1636 | this.keyAcceleration = 0; |
| 1637 | } |
| 1638 | |
| 1639 | if ( newY > 0 ) { |
| 1640 | newY = 0; |
| 1641 | this.keyAcceleration = 0; |
| 1642 | } else if ( newY < this.maxScrollY ) { |
| 1643 | newY = this.maxScrollY; |
| 1644 | this.keyAcceleration = 0; |
| 1645 | } |
| 1646 | |
| 1647 | this.scrollTo(newX, newY, 0); |
| 1648 | |
| 1649 | this.keyTime = now; |
| 1650 | }, |
| 1651 | |
| 1652 | _animate: function (destX, destY, duration, easingFn) { |
| 1653 | var that = this, |
| 1654 | startX = this.x, |
| 1655 | startY = this.y, |
| 1656 | startTime = utils.getTime(), |
| 1657 | destTime = startTime + duration; |
| 1658 | |
| 1659 | function step () { |
| 1660 | var now = utils.getTime(), |
| 1661 | newX, newY, |
| 1662 | easing; |
| 1663 | |
| 1664 | if ( now >= destTime ) { |
| 1665 | that.isAnimating = false; |
| 1666 | that._translate(destX, destY); |
| 1667 | |
| 1668 | if ( !that.resetPosition(that.options.bounceTime) ) { |
| 1669 | that._execEvent('scrollEnd'); |
| 1670 | } |
| 1671 | |
| 1672 | return; |
| 1673 | } |
| 1674 | |
| 1675 | now = ( now - startTime ) / duration; |
| 1676 | easing = easingFn(now); |
| 1677 | newX = ( destX - startX ) * easing + startX; |
| 1678 | newY = ( destY - startY ) * easing + startY; |
| 1679 | that._translate(newX, newY); |
| 1680 | |
| 1681 | if ( that.isAnimating ) { |
| 1682 | rAF(step); |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | this.isAnimating = true; |
| 1687 | step(); |
| 1688 | }, |
| 1689 | handleEvent: function (e) { |
| 1690 | switch ( e.type ) { |
| 1691 | case 'touchstart': |
| 1692 | case 'MSPointerDown': |
| 1693 | case 'mousedown': |
| 1694 | this._start(e); |
| 1695 | |
| 1696 | if ( this.options.zoom && e.touches && e.touches.length > 1 ) { |
| 1697 | this._zoomStart(e); |
| 1698 | } |
| 1699 | break; |
| 1700 | case 'touchmove': |
| 1701 | case 'MSPointerMove': |
| 1702 | case 'mousemove': |
| 1703 | if ( this.options.zoom && e.touches && e.touches[1] ) { |
| 1704 | this._zoom(e); |
| 1705 | return; |
| 1706 | } |
| 1707 | this._move(e); |
| 1708 | break; |
| 1709 | case 'touchend': |
| 1710 | case 'MSPointerUp': |
| 1711 | case 'mouseup': |
| 1712 | case 'touchcancel': |
| 1713 | case 'MSPointerCancel': |
| 1714 | case 'mousecancel': |
| 1715 | if ( this.scaled ) { |
| 1716 | this._zoomEnd(e); |
| 1717 | return; |
| 1718 | } |
| 1719 | this._end(e); |
| 1720 | break; |
| 1721 | case 'orientationchange': |
| 1722 | case 'resize': |
| 1723 | this._resize(); |
| 1724 | break; |
| 1725 | case 'transitionend': |
| 1726 | case 'webkitTransitionEnd': |
| 1727 | case 'oTransitionEnd': |
| 1728 | case 'MSTransitionEnd': |
| 1729 | this._transitionEnd(e); |
| 1730 | break; |
| 1731 | case 'wheel': |
| 1732 | case 'DOMMouseScroll': |
| 1733 | case 'mousewheel': |
| 1734 | if ( this.options.wheelAction == 'zoom' ) { |
| 1735 | this._wheelZoom(e); |
| 1736 | return; |
| 1737 | } |
| 1738 | this._wheel(e); |
| 1739 | break; |
| 1740 | case 'keydown': |
| 1741 | this._key(e); |
| 1742 | break; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | }; |
| 1747 | function createDefaultScrollbar (direction, interactive, type) { |
| 1748 | var scrollbar = document.createElement('div'), |
| 1749 | indicator = document.createElement('div'); |
| 1750 | |
| 1751 | if ( type === true ) { |
| 1752 | scrollbar.style.cssText = 'position:absolute;z-index:9999'; |
| 1753 | 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'; |
| 1754 | } |
| 1755 | |
| 1756 | indicator.className = 'iScrollIndicator'; |
| 1757 | |
| 1758 | if ( direction == 'h' ) { |
| 1759 | if ( type === true ) { |
| 1760 | scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0'; |
| 1761 | indicator.style.height = '100%'; |
| 1762 | } |
| 1763 | scrollbar.className = 'iScrollHorizontalScrollbar'; |
| 1764 | } else { |
| 1765 | if ( type === true ) { |
| 1766 | scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px'; |
| 1767 | indicator.style.width = '100%'; |
| 1768 | } |
| 1769 | scrollbar.className = 'iScrollVerticalScrollbar'; |
| 1770 | } |
| 1771 | |
| 1772 | scrollbar.style.cssText += ';overflow:hidden'; |
| 1773 | |
| 1774 | if ( !interactive ) { |
| 1775 | scrollbar.style.pointerEvents = 'none'; |
| 1776 | } |
| 1777 | |
| 1778 | scrollbar.appendChild(indicator); |
| 1779 | |
| 1780 | return scrollbar; |
| 1781 | } |
| 1782 | |
| 1783 | function Indicator (scroller, options) { |
| 1784 | this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el; |
| 1785 | this.wrapperStyle = this.wrapper.style; |
| 1786 | this.indicator = this.wrapper.children[0]; |
| 1787 | this.indicatorStyle = this.indicator.style; |
| 1788 | this.scroller = scroller; |
| 1789 | |
| 1790 | this.options = { |
| 1791 | listenX: true, |
| 1792 | listenY: true, |
| 1793 | interactive: false, |
| 1794 | resize: true, |
| 1795 | defaultScrollbars: false, |
| 1796 | shrink: false, |
| 1797 | fade: false, |
| 1798 | speedRatioX: 0, |
| 1799 | speedRatioY: 0 |
| 1800 | }; |
| 1801 | |
| 1802 | for ( var i in options ) { |
| 1803 | this.options[i] = options[i]; |
| 1804 | } |
| 1805 | |
| 1806 | this.sizeRatioX = 1; |
| 1807 | this.sizeRatioY = 1; |
| 1808 | this.maxPosX = 0; |
| 1809 | this.maxPosY = 0; |
| 1810 | |
| 1811 | if ( this.options.interactive ) { |
| 1812 | if ( !this.options.disableTouch ) { |
| 1813 | utils.addEvent(this.indicator, 'touchstart', this); |
| 1814 | utils.addEvent(window, 'touchend', this); |
| 1815 | } |
| 1816 | if ( !this.options.disablePointer ) { |
| 1817 | utils.addEvent(this.indicator, 'MSPointerDown', this); |
| 1818 | utils.addEvent(window, 'MSPointerUp', this); |
| 1819 | } |
| 1820 | if ( !this.options.disableMouse ) { |
| 1821 | utils.addEvent(this.indicator, 'mousedown', this); |
| 1822 | utils.addEvent(window, 'mouseup', this); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | if ( this.options.fade ) { |
| 1827 | this.wrapperStyle[utils.style.transform] = this.scroller.translateZ; |
| 1828 | this.wrapperStyle[utils.style.transitionDuration] = utils.isBadAndroid ? '0.001s' : '0ms'; |
| 1829 | this.wrapperStyle.opacity = '0'; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | Indicator.prototype = { |
| 1834 | handleEvent: function (e) { |
| 1835 | switch ( e.type ) { |
| 1836 | case 'touchstart': |
| 1837 | case 'MSPointerDown': |
| 1838 | case 'mousedown': |
| 1839 | this._start(e); |
| 1840 | break; |
| 1841 | case 'touchmove': |
| 1842 | case 'MSPointerMove': |
| 1843 | case 'mousemove': |
| 1844 | this._move(e); |
| 1845 | break; |
| 1846 | case 'touchend': |
| 1847 | case 'MSPointerUp': |
| 1848 | case 'mouseup': |
| 1849 | case 'touchcancel': |
| 1850 | case 'MSPointerCancel': |
| 1851 | case 'mousecancel': |
| 1852 | this._end(e); |
| 1853 | break; |
| 1854 | } |
| 1855 | }, |
| 1856 | |
| 1857 | destroy: function () { |
| 1858 | if ( this.options.interactive ) { |
| 1859 | utils.removeEvent(this.indicator, 'touchstart', this); |
| 1860 | utils.removeEvent(this.indicator, 'MSPointerDown', this); |
| 1861 | utils.removeEvent(this.indicator, 'mousedown', this); |
| 1862 | |
| 1863 | utils.removeEvent(window, 'touchmove', this); |
| 1864 | utils.removeEvent(window, 'MSPointerMove', this); |
| 1865 | utils.removeEvent(window, 'mousemove', this); |
| 1866 | |
| 1867 | utils.removeEvent(window, 'touchend', this); |
| 1868 | utils.removeEvent(window, 'MSPointerUp', this); |
| 1869 | utils.removeEvent(window, 'mouseup', this); |
| 1870 | } |
| 1871 | |
| 1872 | if ( this.options.defaultScrollbars ) { |
| 1873 | this.wrapper.parentNode.removeChild(this.wrapper); |
| 1874 | } |
| 1875 | }, |
| 1876 | |
| 1877 | _start: function (e) { |
| 1878 | var point = e.touches ? e.touches[0] : e; |
| 1879 | |
| 1880 | e.preventDefault(); |
| 1881 | e.stopPropagation(); |
| 1882 | |
| 1883 | this.transitionTime(); |
| 1884 | |
| 1885 | this.initiated = true; |
| 1886 | this.moved = false; |
| 1887 | this.lastPointX = point.pageX; |
| 1888 | this.lastPointY = point.pageY; |
| 1889 | |
| 1890 | this.startTime = utils.getTime(); |
| 1891 | |
| 1892 | if ( !this.options.disableTouch ) { |
| 1893 | utils.addEvent(window, 'touchmove', this); |
| 1894 | } |
| 1895 | if ( !this.options.disablePointer ) { |
| 1896 | utils.addEvent(window, 'MSPointerMove', this); |
| 1897 | } |
| 1898 | if ( !this.options.disableMouse ) { |
| 1899 | utils.addEvent(window, 'mousemove', this); |
| 1900 | } |
| 1901 | |
| 1902 | this.scroller._execEvent('beforeScrollStart'); |
| 1903 | }, |
| 1904 | |
| 1905 | _move: function (e) { |
| 1906 | var point = e.touches ? e.touches[0] : e, |
| 1907 | deltaX, deltaY, |
| 1908 | newX, newY, |
| 1909 | timestamp = utils.getTime(); |
| 1910 | |
| 1911 | if ( !this.moved ) { |
| 1912 | this.scroller._execEvent('scrollStart'); |
| 1913 | } |
| 1914 | |
| 1915 | this.moved = true; |
| 1916 | |
| 1917 | deltaX = point.pageX - this.lastPointX; |
| 1918 | this.lastPointX = point.pageX; |
| 1919 | |
| 1920 | deltaY = point.pageY - this.lastPointY; |
| 1921 | this.lastPointY = point.pageY; |
| 1922 | |
| 1923 | newX = this.x + deltaX; |
| 1924 | newY = this.y + deltaY; |
| 1925 | |
| 1926 | this._pos(newX, newY); |
| 1927 | |
| 1928 | // INSERT POINT: indicator._move |
| 1929 | |
| 1930 | e.preventDefault(); |
| 1931 | e.stopPropagation(); |
| 1932 | }, |
| 1933 | |
| 1934 | _end: function (e) { |
| 1935 | if ( !this.initiated ) { |
| 1936 | return; |
| 1937 | } |
| 1938 | |
| 1939 | this.initiated = false; |
| 1940 | |
| 1941 | e.preventDefault(); |
| 1942 | e.stopPropagation(); |
| 1943 | |
| 1944 | utils.removeEvent(window, 'touchmove', this); |
| 1945 | utils.removeEvent(window, 'MSPointerMove', this); |
| 1946 | utils.removeEvent(window, 'mousemove', this); |
| 1947 | |
| 1948 | if ( this.scroller.options.snap ) { |
| 1949 | var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y); |
| 1950 | |
| 1951 | var time = this.options.snapSpeed || Math.max( |
| 1952 | Math.max( |
| 1953 | Math.min(Math.abs(this.scroller.x - snap.x), 1000), |
| 1954 | Math.min(Math.abs(this.scroller.y - snap.y), 1000) |
| 1955 | ), 300); |
| 1956 | |
| 1957 | if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) { |
| 1958 | this.scroller.directionX = 0; |
| 1959 | this.scroller.directionY = 0; |
| 1960 | this.scroller.currentPage = snap; |
| 1961 | this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing); |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | if ( this.moved ) { |
| 1966 | this.scroller._execEvent('scrollEnd'); |
| 1967 | } |
| 1968 | }, |
| 1969 | |
| 1970 | transitionTime: function (time) { |
| 1971 | time = time || 0; |
| 1972 | this.indicatorStyle[utils.style.transitionDuration] = time + 'ms'; |
| 1973 | |
| 1974 | if ( !time && utils.isBadAndroid ) { |
| 1975 | this.indicatorStyle[utils.style.transitionDuration] = '0.001s'; |
| 1976 | } |
| 1977 | }, |
| 1978 | |
| 1979 | transitionTimingFunction: function (easing) { |
| 1980 | this.indicatorStyle[utils.style.transitionTimingFunction] = easing; |
| 1981 | }, |
| 1982 | |
| 1983 | refresh: function () { |
| 1984 | this.transitionTime(); |
| 1985 | |
| 1986 | if ( this.options.listenX && !this.options.listenY ) { |
| 1987 | this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none'; |
| 1988 | } else if ( this.options.listenY && !this.options.listenX ) { |
| 1989 | this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none'; |
| 1990 | } else { |
| 1991 | this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none'; |
| 1992 | } |
| 1993 | |
| 1994 | if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) { |
| 1995 | utils.addClass(this.wrapper, 'iScrollBothScrollbars'); |
| 1996 | utils.removeClass(this.wrapper, 'iScrollLoneScrollbar'); |
| 1997 | |
| 1998 | if ( this.options.defaultScrollbars && this.options.customStyle ) { |
| 1999 | if ( this.options.listenX ) { |
| 2000 | this.wrapper.style.right = '8px'; |
| 2001 | } else { |
| 2002 | this.wrapper.style.bottom = '8px'; |
| 2003 | } |
| 2004 | } |
| 2005 | } else { |
| 2006 | utils.removeClass(this.wrapper, 'iScrollBothScrollbars'); |
| 2007 | utils.addClass(this.wrapper, 'iScrollLoneScrollbar'); |
| 2008 | |
| 2009 | if ( this.options.defaultScrollbars && this.options.customStyle ) { |
| 2010 | if ( this.options.listenX ) { |
| 2011 | this.wrapper.style.right = '2px'; |
| 2012 | } else { |
| 2013 | this.wrapper.style.bottom = '2px'; |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | var r = this.wrapper.offsetHeight; // force refresh |
| 2019 | |
| 2020 | if ( this.options.listenX ) { |
| 2021 | this.wrapperWidth = this.wrapper.clientWidth; |
| 2022 | if ( this.options.resize ) { |
| 2023 | this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8); |
| 2024 | this.indicatorStyle.width = this.indicatorWidth + 'px'; |
| 2025 | } else { |
| 2026 | this.indicatorWidth = this.indicator.clientWidth; |
| 2027 | } |
| 2028 | |
| 2029 | this.maxPosX = this.wrapperWidth - this.indicatorWidth; |
| 2030 | |
| 2031 | if ( this.options.shrink == 'clip' ) { |
| 2032 | this.minBoundaryX = -this.indicatorWidth + 8; |
| 2033 | this.maxBoundaryX = this.wrapperWidth - 8; |
| 2034 | } else { |
| 2035 | this.minBoundaryX = 0; |
| 2036 | this.maxBoundaryX = this.maxPosX; |
| 2037 | } |
| 2038 | |
| 2039 | this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX)); |
| 2040 | } |
| 2041 | |
| 2042 | if ( this.options.listenY ) { |
| 2043 | this.wrapperHeight = this.wrapper.clientHeight; |
| 2044 | if ( this.options.resize ) { |
| 2045 | this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8); |
| 2046 | this.indicatorStyle.height = this.indicatorHeight + 'px'; |
| 2047 | } else { |
| 2048 | this.indicatorHeight = this.indicator.clientHeight; |
| 2049 | } |
| 2050 | |
| 2051 | this.maxPosY = this.wrapperHeight - this.indicatorHeight; |
| 2052 | |
| 2053 | if ( this.options.shrink == 'clip' ) { |
| 2054 | this.minBoundaryY = -this.indicatorHeight + 8; |
| 2055 | this.maxBoundaryY = this.wrapperHeight - 8; |
| 2056 | } else { |
| 2057 | this.minBoundaryY = 0; |
| 2058 | this.maxBoundaryY = this.maxPosY; |
| 2059 | } |
| 2060 | |
| 2061 | this.maxPosY = this.wrapperHeight - this.indicatorHeight; |
| 2062 | this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY)); |
| 2063 | } |
| 2064 | |
| 2065 | this.updatePosition(); |
| 2066 | }, |
| 2067 | |
| 2068 | updatePosition: function () { |
| 2069 | var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0, |
| 2070 | y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0; |
| 2071 | |
| 2072 | if ( !this.options.ignoreBoundaries ) { |
| 2073 | if ( x < this.minBoundaryX ) { |
| 2074 | if ( this.options.shrink == 'scale' ) { |
| 2075 | this.width = Math.max(this.indicatorWidth + x, 8); |
| 2076 | this.indicatorStyle.width = this.width + 'px'; |
| 2077 | } |
| 2078 | x = this.minBoundaryX; |
| 2079 | } else if ( x > this.maxBoundaryX ) { |
| 2080 | if ( this.options.shrink == 'scale' ) { |
| 2081 | this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8); |
| 2082 | this.indicatorStyle.width = this.width + 'px'; |
| 2083 | x = this.maxPosX + this.indicatorWidth - this.width; |
| 2084 | } else { |
| 2085 | x = this.maxBoundaryX; |
| 2086 | } |
| 2087 | } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) { |
| 2088 | this.width = this.indicatorWidth; |
| 2089 | this.indicatorStyle.width = this.width + 'px'; |
| 2090 | } |
| 2091 | |
| 2092 | if ( y < this.minBoundaryY ) { |
| 2093 | if ( this.options.shrink == 'scale' ) { |
| 2094 | this.height = Math.max(this.indicatorHeight + y * 3, 8); |
| 2095 | this.indicatorStyle.height = this.height + 'px'; |
| 2096 | } |
| 2097 | y = this.minBoundaryY; |
| 2098 | } else if ( y > this.maxBoundaryY ) { |
| 2099 | if ( this.options.shrink == 'scale' ) { |
| 2100 | this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8); |
| 2101 | this.indicatorStyle.height = this.height + 'px'; |
| 2102 | y = this.maxPosY + this.indicatorHeight - this.height; |
| 2103 | } else { |
| 2104 | y = this.maxBoundaryY; |
| 2105 | } |
| 2106 | } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) { |
| 2107 | this.height = this.indicatorHeight; |
| 2108 | this.indicatorStyle.height = this.height + 'px'; |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | this.x = x; |
| 2113 | this.y = y; |
| 2114 | |
| 2115 | if ( this.scroller.options.useTransform ) { |
| 2116 | this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ; |
| 2117 | } else { |
| 2118 | this.indicatorStyle.left = x + 'px'; |
| 2119 | this.indicatorStyle.top = y + 'px'; |
| 2120 | } |
| 2121 | }, |
| 2122 | |
| 2123 | _pos: function (x, y) { |
| 2124 | if ( x < 0 ) { |
| 2125 | x = 0; |
| 2126 | } else if ( x > this.maxPosX ) { |
| 2127 | x = this.maxPosX; |
| 2128 | } |
| 2129 | |
| 2130 | if ( y < 0 ) { |
| 2131 | y = 0; |
| 2132 | } else if ( y > this.maxPosY ) { |
| 2133 | y = this.maxPosY; |
| 2134 | } |
| 2135 | |
| 2136 | x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x; |
| 2137 | y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y; |
| 2138 | |
| 2139 | this.scroller.scrollTo(x, y); |
| 2140 | }, |
| 2141 | |
| 2142 | fade: function (val, hold) { |
| 2143 | if ( hold && !this.visible ) { |
| 2144 | return; |
| 2145 | } |
| 2146 | |
| 2147 | clearTimeout(this.fadeTimeout); |
| 2148 | this.fadeTimeout = null; |
| 2149 | |
| 2150 | var time = val ? 250 : 500, |
| 2151 | delay = val ? 0 : 300; |
| 2152 | |
| 2153 | val = val ? '1' : '0'; |
| 2154 | |
| 2155 | this.wrapperStyle[utils.style.transitionDuration] = time + 'ms'; |
| 2156 | |
| 2157 | this.fadeTimeout = setTimeout((function (val) { |
| 2158 | this.wrapperStyle.opacity = val; |
| 2159 | this.visible = +val; |
| 2160 | }).bind(this, val), delay); |
| 2161 | } |
| 2162 | }; |
| 2163 | |
| 2164 | IScroll.utils = utils; |
| 2165 | |
| 2166 | if ( typeof module != 'undefined' && module.exports ) { |
| 2167 | module.exports = IScroll; |
| 2168 | } else { |
| 2169 | window.IScroll = IScroll; |
| 2170 | } |
| 2171 | |
| 2172 | })(window, document, Math); |