| 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 | // INSERT POINT: OPTIONS |
| 246 | |
| 247 | startX: 0, |
| 248 | startY: 0, |
| 249 | scrollY: true, |
| 250 | directionLockThreshold: 5, |
| 251 | momentum: true, |
| 252 | |
| 253 | bounce: true, |
| 254 | bounceTime: 600, |
| 255 | bounceEasing: '', |
| 256 | |
| 257 | preventDefault: true, |
| 258 | preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ }, |
| 259 | |
| 260 | HWCompositing: true, |
| 261 | useTransition: true, |
| 262 | useTransform: true |
| 263 | }; |
| 264 | |
| 265 | for ( var i in options ) { |
| 266 | this.options[i] = options[i]; |
| 267 | } |
| 268 | |
| 269 | // Normalize options |
| 270 | this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : ''; |
| 271 | |
| 272 | this.options.useTransition = utils.hasTransition && this.options.useTransition; |
| 273 | this.options.useTransform = utils.hasTransform && this.options.useTransform; |
| 274 | |
| 275 | this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough; |
| 276 | this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault; |
| 277 | |
| 278 | // If you want eventPassthrough I have to lock one of the axes |
| 279 | this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY; |
| 280 | this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX; |
| 281 | |
| 282 | // With eventPassthrough we also need lockDirection mechanism |
| 283 | this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough; |
| 284 | this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold; |
| 285 | |
| 286 | this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing; |
| 287 | |
| 288 | this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling; |
| 289 | |
| 290 | if ( this.options.tap === true ) { |
| 291 | this.options.tap = 'tap'; |
| 292 | } |
| 293 | |
| 294 | // INSERT POINT: NORMALIZATION |
| 295 | |
| 296 | // Some defaults |
| 297 | this.x = 0; |
| 298 | this.y = 0; |
| 299 | this.directionX = 0; |
| 300 | this.directionY = 0; |
| 301 | this._events = {}; |
| 302 | |
| 303 | // INSERT POINT: DEFAULTS |
| 304 | |
| 305 | this._init(); |
| 306 | this.refresh(); |
| 307 | |
| 308 | this.scrollTo(this.options.startX, this.options.startY); |
| 309 | this.enable(); |
| 310 | } |
| 311 | |
| 312 | IScroll.prototype = { |
| 313 | version: '5.1.1', |
| 314 | |
| 315 | _init: function () { |
| 316 | this._initEvents(); |
| 317 | |
| 318 | // INSERT POINT: _init |
| 319 | |
| 320 | }, |
| 321 | |
| 322 | destroy: function () { |
| 323 | this._initEvents(true); |
| 324 | |
| 325 | this._execEvent('destroy'); |
| 326 | }, |
| 327 | |
| 328 | _transitionEnd: function (e) { |
| 329 | if ( e.target != this.scroller || !this.isInTransition ) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | this._transitionTime(); |
| 334 | if ( !this.resetPosition(this.options.bounceTime) ) { |
| 335 | this.isInTransition = false; |
| 336 | this._execEvent('scrollEnd'); |
| 337 | } |
| 338 | }, |
| 339 | |
| 340 | _start: function (e) { |
| 341 | // React to left mouse button only |
| 342 | if ( utils.eventType[e.type] != 1 ) { |
| 343 | if ( e.button !== 0 ) { |
| 344 | return; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) { |
| 353 | e.preventDefault(); |
| 354 | } |
| 355 | |
| 356 | var point = e.touches ? e.touches[0] : e, |
| 357 | pos; |
| 358 | |
| 359 | this.initiated = utils.eventType[e.type]; |
| 360 | this.moved = false; |
| 361 | this.distX = 0; |
| 362 | this.distY = 0; |
| 363 | this.directionX = 0; |
| 364 | this.directionY = 0; |
| 365 | this.directionLocked = 0; |
| 366 | |
| 367 | this._transitionTime(); |
| 368 | |
| 369 | this.startTime = utils.getTime(); |
| 370 | |
| 371 | if ( this.options.useTransition && this.isInTransition ) { |
| 372 | this.isInTransition = false; |
| 373 | pos = this.getComputedPosition(); |
| 374 | this._translate(Math.round(pos.x), Math.round(pos.y)); |
| 375 | this._execEvent('scrollEnd'); |
| 376 | } else if ( !this.options.useTransition && this.isAnimating ) { |
| 377 | this.isAnimating = false; |
| 378 | this._execEvent('scrollEnd'); |
| 379 | } |
| 380 | |
| 381 | this.startX = this.x; |
| 382 | this.startY = this.y; |
| 383 | this.absStartX = this.x; |
| 384 | this.absStartY = this.y; |
| 385 | this.pointX = point.pageX; |
| 386 | this.pointY = point.pageY; |
| 387 | |
| 388 | this._execEvent('beforeScrollStart'); |
| 389 | }, |
| 390 | |
| 391 | _move: function (e) { |
| 392 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | if ( this.options.preventDefault ) { // increases performance on Android? TODO: check! |
| 397 | e.preventDefault(); |
| 398 | } |
| 399 | |
| 400 | var point = e.touches ? e.touches[0] : e, |
| 401 | deltaX = point.pageX - this.pointX, |
| 402 | deltaY = point.pageY - this.pointY, |
| 403 | timestamp = utils.getTime(), |
| 404 | newX, newY, |
| 405 | absDistX, absDistY; |
| 406 | |
| 407 | this.pointX = point.pageX; |
| 408 | this.pointY = point.pageY; |
| 409 | |
| 410 | this.distX += deltaX; |
| 411 | this.distY += deltaY; |
| 412 | absDistX = Math.abs(this.distX); |
| 413 | absDistY = Math.abs(this.distY); |
| 414 | |
| 415 | // We need to move at least 10 pixels for the scrolling to initiate |
| 416 | if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | // If you are scrolling in one direction lock the other |
| 421 | if ( !this.directionLocked && !this.options.freeScroll ) { |
| 422 | if ( absDistX > absDistY + this.options.directionLockThreshold ) { |
| 423 | this.directionLocked = 'h'; // lock horizontally |
| 424 | } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) { |
| 425 | this.directionLocked = 'v'; // lock vertically |
| 426 | } else { |
| 427 | this.directionLocked = 'n'; // no lock |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if ( this.directionLocked == 'h' ) { |
| 432 | if ( this.options.eventPassthrough == 'vertical' ) { |
| 433 | e.preventDefault(); |
| 434 | } else if ( this.options.eventPassthrough == 'horizontal' ) { |
| 435 | this.initiated = false; |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | deltaY = 0; |
| 440 | } else if ( this.directionLocked == 'v' ) { |
| 441 | if ( this.options.eventPassthrough == 'horizontal' ) { |
| 442 | e.preventDefault(); |
| 443 | } else if ( this.options.eventPassthrough == 'vertical' ) { |
| 444 | this.initiated = false; |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | deltaX = 0; |
| 449 | } |
| 450 | |
| 451 | deltaX = this.hasHorizontalScroll ? deltaX : 0; |
| 452 | deltaY = this.hasVerticalScroll ? deltaY : 0; |
| 453 | |
| 454 | newX = this.x + deltaX; |
| 455 | newY = this.y + deltaY; |
| 456 | |
| 457 | // Slow down if outside of the boundaries |
| 458 | if ( newX > 0 || newX < this.maxScrollX ) { |
| 459 | newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX; |
| 460 | } |
| 461 | if ( newY > 0 || newY < this.maxScrollY ) { |
| 462 | newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY; |
| 463 | } |
| 464 | |
| 465 | this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0; |
| 466 | this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0; |
| 467 | |
| 468 | if ( !this.moved ) { |
| 469 | this._execEvent('scrollStart'); |
| 470 | } |
| 471 | |
| 472 | this.moved = true; |
| 473 | |
| 474 | this._translate(newX, newY); |
| 475 | |
| 476 | /* REPLACE START: _move */ |
| 477 | |
| 478 | if ( timestamp - this.startTime > 300 ) { |
| 479 | this.startTime = timestamp; |
| 480 | this.startX = this.x; |
| 481 | this.startY = this.y; |
| 482 | } |
| 483 | |
| 484 | /* REPLACE END: _move */ |
| 485 | |
| 486 | }, |
| 487 | |
| 488 | _end: function (e) { |
| 489 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) { |
| 494 | e.preventDefault(); |
| 495 | } |
| 496 | |
| 497 | var point = e.changedTouches ? e.changedTouches[0] : e, |
| 498 | momentumX, |
| 499 | momentumY, |
| 500 | duration = utils.getTime() - this.startTime, |
| 501 | newX = Math.round(this.x), |
| 502 | newY = Math.round(this.y), |
| 503 | distanceX = Math.abs(newX - this.startX), |
| 504 | distanceY = Math.abs(newY - this.startY), |
| 505 | time = 0, |
| 506 | easing = ''; |
| 507 | |
| 508 | this.isInTransition = 0; |
| 509 | this.initiated = 0; |
| 510 | this.endTime = utils.getTime(); |
| 511 | |
| 512 | // reset if we are outside of the boundaries |
| 513 | if ( this.resetPosition(this.options.bounceTime) ) { |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | this.scrollTo(newX, newY); // ensures that the last position is rounded |
| 518 | |
| 519 | // we scrolled less than 10 pixels |
| 520 | if ( !this.moved ) { |
| 521 | if ( this.options.tap ) { |
| 522 | utils.tap(e, this.options.tap); |
| 523 | } |
| 524 | |
| 525 | if ( this.options.click ) { |
| 526 | utils.click(e); |
| 527 | } |
| 528 | |
| 529 | this._execEvent('scrollCancel'); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) { |
| 534 | this._execEvent('flick'); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | // start momentum animation if needed |
| 539 | if ( this.options.momentum && duration < 300 ) { |
| 540 | 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 }; |
| 541 | 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 }; |
| 542 | newX = momentumX.destination; |
| 543 | newY = momentumY.destination; |
| 544 | time = Math.max(momentumX.duration, momentumY.duration); |
| 545 | this.isInTransition = 1; |
| 546 | } |
| 547 | |
| 548 | // INSERT POINT: _end |
| 549 | |
| 550 | if ( newX != this.x || newY != this.y ) { |
| 551 | // change easing function when scroller goes out of the boundaries |
| 552 | if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) { |
| 553 | easing = utils.ease.quadratic; |
| 554 | } |
| 555 | |
| 556 | this.scrollTo(newX, newY, time, easing); |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | this._execEvent('scrollEnd'); |
| 561 | }, |
| 562 | |
| 563 | _resize: function () { |
| 564 | var that = this; |
| 565 | |
| 566 | clearTimeout(this.resizeTimeout); |
| 567 | |
| 568 | this.resizeTimeout = setTimeout(function () { |
| 569 | that.refresh(); |
| 570 | }, this.options.resizePolling); |
| 571 | }, |
| 572 | |
| 573 | resetPosition: function (time) { |
| 574 | var x = this.x, |
| 575 | y = this.y; |
| 576 | |
| 577 | time = time || 0; |
| 578 | |
| 579 | if ( !this.hasHorizontalScroll || this.x > 0 ) { |
| 580 | x = 0; |
| 581 | } else if ( this.x < this.maxScrollX ) { |
| 582 | x = this.maxScrollX; |
| 583 | } |
| 584 | |
| 585 | if ( !this.hasVerticalScroll || this.y > 0 ) { |
| 586 | y = 0; |
| 587 | } else if ( this.y < this.maxScrollY ) { |
| 588 | y = this.maxScrollY; |
| 589 | } |
| 590 | |
| 591 | if ( x == this.x && y == this.y ) { |
| 592 | return false; |
| 593 | } |
| 594 | |
| 595 | this.scrollTo(x, y, time, this.options.bounceEasing); |
| 596 | |
| 597 | return true; |
| 598 | }, |
| 599 | |
| 600 | disable: function () { |
| 601 | this.enabled = false; |
| 602 | }, |
| 603 | |
| 604 | enable: function () { |
| 605 | this.enabled = true; |
| 606 | }, |
| 607 | |
| 608 | refresh: function () { |
| 609 | var rf = this.wrapper.offsetHeight; // Force reflow |
| 610 | |
| 611 | this.wrapperWidth = this.wrapper.clientWidth; |
| 612 | this.wrapperHeight = this.wrapper.clientHeight; |
| 613 | |
| 614 | /* REPLACE START: refresh */ |
| 615 | |
| 616 | this.scrollerWidth = this.scroller.offsetWidth; |
| 617 | this.scrollerHeight = this.scroller.offsetHeight; |
| 618 | |
| 619 | this.maxScrollX = this.wrapperWidth - this.scrollerWidth; |
| 620 | this.maxScrollY = this.wrapperHeight - this.scrollerHeight; |
| 621 | |
| 622 | /* REPLACE END: refresh */ |
| 623 | |
| 624 | this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0; |
| 625 | this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0; |
| 626 | |
| 627 | if ( !this.hasHorizontalScroll ) { |
| 628 | this.maxScrollX = 0; |
| 629 | this.scrollerWidth = this.wrapperWidth; |
| 630 | } |
| 631 | |
| 632 | if ( !this.hasVerticalScroll ) { |
| 633 | this.maxScrollY = 0; |
| 634 | this.scrollerHeight = this.wrapperHeight; |
| 635 | } |
| 636 | |
| 637 | this.endTime = 0; |
| 638 | this.directionX = 0; |
| 639 | this.directionY = 0; |
| 640 | |
| 641 | this.wrapperOffset = utils.offset(this.wrapper); |
| 642 | |
| 643 | this._execEvent('refresh'); |
| 644 | |
| 645 | this.resetPosition(); |
| 646 | |
| 647 | // INSERT POINT: _refresh |
| 648 | |
| 649 | }, |
| 650 | |
| 651 | on: function (type, fn) { |
| 652 | if ( !this._events[type] ) { |
| 653 | this._events[type] = []; |
| 654 | } |
| 655 | |
| 656 | this._events[type].push(fn); |
| 657 | }, |
| 658 | |
| 659 | off: function (type, fn) { |
| 660 | if ( !this._events[type] ) { |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | var index = this._events[type].indexOf(fn); |
| 665 | |
| 666 | if ( index > -1 ) { |
| 667 | this._events[type].splice(index, 1); |
| 668 | } |
| 669 | }, |
| 670 | |
| 671 | _execEvent: function (type) { |
| 672 | if ( !this._events[type] ) { |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | var i = 0, |
| 677 | l = this._events[type].length; |
| 678 | |
| 679 | if ( !l ) { |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | for ( ; i < l; i++ ) { |
| 684 | this._events[type][i].apply(this, [].slice.call(arguments, 1)); |
| 685 | } |
| 686 | }, |
| 687 | |
| 688 | scrollBy: function (x, y, time, easing) { |
| 689 | x = this.x + x; |
| 690 | y = this.y + y; |
| 691 | time = time || 0; |
| 692 | |
| 693 | this.scrollTo(x, y, time, easing); |
| 694 | }, |
| 695 | |
| 696 | scrollTo: function (x, y, time, easing) { |
| 697 | easing = easing || utils.ease.circular; |
| 698 | |
| 699 | this.isInTransition = this.options.useTransition && time > 0; |
| 700 | |
| 701 | if ( !time || (this.options.useTransition && easing.style) ) { |
| 702 | this._transitionTimingFunction(easing.style); |
| 703 | this._transitionTime(time); |
| 704 | this._translate(x, y); |
| 705 | } else { |
| 706 | this._animate(x, y, time, easing.fn); |
| 707 | } |
| 708 | }, |
| 709 | |
| 710 | scrollToElement: function (el, time, offsetX, offsetY, easing) { |
| 711 | el = el.nodeType ? el : this.scroller.querySelector(el); |
| 712 | |
| 713 | if ( !el ) { |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | var pos = utils.offset(el); |
| 718 | |
| 719 | pos.left -= this.wrapperOffset.left; |
| 720 | pos.top -= this.wrapperOffset.top; |
| 721 | |
| 722 | // if offsetX/Y are true we center the element to the screen |
| 723 | if ( offsetX === true ) { |
| 724 | offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2); |
| 725 | } |
| 726 | if ( offsetY === true ) { |
| 727 | offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2); |
| 728 | } |
| 729 | |
| 730 | pos.left -= offsetX || 0; |
| 731 | pos.top -= offsetY || 0; |
| 732 | |
| 733 | pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left; |
| 734 | pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top; |
| 735 | |
| 736 | time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time; |
| 737 | |
| 738 | this.scrollTo(pos.left, pos.top, time, easing); |
| 739 | }, |
| 740 | |
| 741 | _transitionTime: function (time) { |
| 742 | time = time || 0; |
| 743 | |
| 744 | this.scrollerStyle[utils.style.transitionDuration] = time + 'ms'; |
| 745 | |
| 746 | if ( !time && utils.isBadAndroid ) { |
| 747 | this.scrollerStyle[utils.style.transitionDuration] = '0.001s'; |
| 748 | } |
| 749 | |
| 750 | // INSERT POINT: _transitionTime |
| 751 | |
| 752 | }, |
| 753 | |
| 754 | _transitionTimingFunction: function (easing) { |
| 755 | this.scrollerStyle[utils.style.transitionTimingFunction] = easing; |
| 756 | |
| 757 | // INSERT POINT: _transitionTimingFunction |
| 758 | |
| 759 | }, |
| 760 | |
| 761 | _translate: function (x, y) { |
| 762 | if ( this.options.useTransform ) { |
| 763 | |
| 764 | /* REPLACE START: _translate */ |
| 765 | |
| 766 | this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ; |
| 767 | |
| 768 | /* REPLACE END: _translate */ |
| 769 | |
| 770 | } else { |
| 771 | x = Math.round(x); |
| 772 | y = Math.round(y); |
| 773 | this.scrollerStyle.left = x + 'px'; |
| 774 | this.scrollerStyle.top = y + 'px'; |
| 775 | } |
| 776 | |
| 777 | this.x = x; |
| 778 | this.y = y; |
| 779 | |
| 780 | // INSERT POINT: _translate |
| 781 | |
| 782 | }, |
| 783 | |
| 784 | _initEvents: function (remove) { |
| 785 | var eventType = remove ? utils.removeEvent : utils.addEvent, |
| 786 | target = this.options.bindToWrapper ? this.wrapper : window; |
| 787 | |
| 788 | eventType(window, 'orientationchange', this); |
| 789 | eventType(window, 'resize', this); |
| 790 | |
| 791 | if ( this.options.click ) { |
| 792 | eventType(this.wrapper, 'click', this, true); |
| 793 | } |
| 794 | |
| 795 | if ( !this.options.disableMouse ) { |
| 796 | eventType(this.wrapper, 'mousedown', this); |
| 797 | eventType(target, 'mousemove', this); |
| 798 | eventType(target, 'mousecancel', this); |
| 799 | eventType(target, 'mouseup', this); |
| 800 | } |
| 801 | |
| 802 | if ( utils.hasPointer && !this.options.disablePointer ) { |
| 803 | eventType(this.wrapper, 'MSPointerDown', this); |
| 804 | eventType(target, 'MSPointerMove', this); |
| 805 | eventType(target, 'MSPointerCancel', this); |
| 806 | eventType(target, 'MSPointerUp', this); |
| 807 | } |
| 808 | |
| 809 | if ( utils.hasTouch && !this.options.disableTouch ) { |
| 810 | eventType(this.wrapper, 'touchstart', this); |
| 811 | eventType(target, 'touchmove', this); |
| 812 | eventType(target, 'touchcancel', this); |
| 813 | eventType(target, 'touchend', this); |
| 814 | } |
| 815 | |
| 816 | eventType(this.scroller, 'transitionend', this); |
| 817 | eventType(this.scroller, 'webkitTransitionEnd', this); |
| 818 | eventType(this.scroller, 'oTransitionEnd', this); |
| 819 | eventType(this.scroller, 'MSTransitionEnd', this); |
| 820 | }, |
| 821 | |
| 822 | getComputedPosition: function () { |
| 823 | var matrix = window.getComputedStyle(this.scroller, null), |
| 824 | x, y; |
| 825 | |
| 826 | if ( this.options.useTransform ) { |
| 827 | matrix = matrix[utils.style.transform].split(')')[0].split(', '); |
| 828 | x = +(matrix[12] || matrix[4]); |
| 829 | y = +(matrix[13] || matrix[5]); |
| 830 | } else { |
| 831 | x = +matrix.left.replace(/[^-\d.]/g, ''); |
| 832 | y = +matrix.top.replace(/[^-\d.]/g, ''); |
| 833 | } |
| 834 | |
| 835 | return { x: x, y: y }; |
| 836 | }, |
| 837 | |
| 838 | _animate: function (destX, destY, duration, easingFn) { |
| 839 | var that = this, |
| 840 | startX = this.x, |
| 841 | startY = this.y, |
| 842 | startTime = utils.getTime(), |
| 843 | destTime = startTime + duration; |
| 844 | |
| 845 | function step () { |
| 846 | var now = utils.getTime(), |
| 847 | newX, newY, |
| 848 | easing; |
| 849 | |
| 850 | if ( now >= destTime ) { |
| 851 | that.isAnimating = false; |
| 852 | that._translate(destX, destY); |
| 853 | |
| 854 | if ( !that.resetPosition(that.options.bounceTime) ) { |
| 855 | that._execEvent('scrollEnd'); |
| 856 | } |
| 857 | |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | now = ( now - startTime ) / duration; |
| 862 | easing = easingFn(now); |
| 863 | newX = ( destX - startX ) * easing + startX; |
| 864 | newY = ( destY - startY ) * easing + startY; |
| 865 | that._translate(newX, newY); |
| 866 | |
| 867 | if ( that.isAnimating ) { |
| 868 | rAF(step); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | this.isAnimating = true; |
| 873 | step(); |
| 874 | }, |
| 875 | handleEvent: function (e) { |
| 876 | switch ( e.type ) { |
| 877 | case 'touchstart': |
| 878 | case 'MSPointerDown': |
| 879 | case 'mousedown': |
| 880 | this._start(e); |
| 881 | break; |
| 882 | case 'touchmove': |
| 883 | case 'MSPointerMove': |
| 884 | case 'mousemove': |
| 885 | this._move(e); |
| 886 | break; |
| 887 | case 'touchend': |
| 888 | case 'MSPointerUp': |
| 889 | case 'mouseup': |
| 890 | case 'touchcancel': |
| 891 | case 'MSPointerCancel': |
| 892 | case 'mousecancel': |
| 893 | this._end(e); |
| 894 | break; |
| 895 | case 'orientationchange': |
| 896 | case 'resize': |
| 897 | this._resize(); |
| 898 | break; |
| 899 | case 'transitionend': |
| 900 | case 'webkitTransitionEnd': |
| 901 | case 'oTransitionEnd': |
| 902 | case 'MSTransitionEnd': |
| 903 | this._transitionEnd(e); |
| 904 | break; |
| 905 | case 'wheel': |
| 906 | case 'DOMMouseScroll': |
| 907 | case 'mousewheel': |
| 908 | this._wheel(e); |
| 909 | break; |
| 910 | case 'keydown': |
| 911 | this._key(e); |
| 912 | break; |
| 913 | case 'click': |
| 914 | if ( !e._constructed ) { |
| 915 | e.preventDefault(); |
| 916 | e.stopPropagation(); |
| 917 | } |
| 918 | break; |
| 919 | } |
| 920 | } |
| 921 | }; |
| 922 | IScroll.utils = utils; |
| 923 | |
| 924 | if ( typeof module != 'undefined' && module.exports ) { |
| 925 | module.exports = IScroll; |
| 926 | } else { |
| 927 | window.IScroll = IScroll; |
| 928 | } |
| 929 | |
| 930 | })(window, document, Math); |