UNPKG

scroll-to-element

Version:

Smooth scrolling to an element via selector or node reference

789 lines (671 loc) 21.2 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.scrollToElement = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ // easing functions from "Tween.js" exports.linear = function(n){ return n; }; exports.inQuad = function(n){ return n * n; }; exports.outQuad = function(n){ return n * (2 - n); }; exports.inOutQuad = function(n){ n *= 2; if (n < 1) return 0.5 * n * n; return - 0.5 * (--n * (n - 2) - 1); }; exports.inCube = function(n){ return n * n * n; }; exports.outCube = function(n){ return --n * n * n + 1; }; exports.inOutCube = function(n){ n *= 2; if (n < 1) return 0.5 * n * n * n; return 0.5 * ((n -= 2 ) * n * n + 2); }; exports.inQuart = function(n){ return n * n * n * n; }; exports.outQuart = function(n){ return 1 - (--n * n * n * n); }; exports.inOutQuart = function(n){ n *= 2; if (n < 1) return 0.5 * n * n * n * n; return -0.5 * ((n -= 2) * n * n * n - 2); }; exports.inQuint = function(n){ return n * n * n * n * n; } exports.outQuint = function(n){ return --n * n * n * n * n + 1; } exports.inOutQuint = function(n){ n *= 2; if (n < 1) return 0.5 * n * n * n * n * n; return 0.5 * ((n -= 2) * n * n * n * n + 2); }; exports.inSine = function(n){ return 1 - Math.cos(n * Math.PI / 2 ); }; exports.outSine = function(n){ return Math.sin(n * Math.PI / 2); }; exports.inOutSine = function(n){ return .5 * (1 - Math.cos(Math.PI * n)); }; exports.inExpo = function(n){ return 0 == n ? 0 : Math.pow(1024, n - 1); }; exports.outExpo = function(n){ return 1 == n ? n : 1 - Math.pow(2, -10 * n); }; exports.inOutExpo = function(n){ if (0 == n) return 0; if (1 == n) return 1; if ((n *= 2) < 1) return .5 * Math.pow(1024, n - 1); return .5 * (-Math.pow(2, -10 * (n - 1)) + 2); }; exports.inCirc = function(n){ return 1 - Math.sqrt(1 - n * n); }; exports.outCirc = function(n){ return Math.sqrt(1 - (--n * n)); }; exports.inOutCirc = function(n){ n *= 2 if (n < 1) return -0.5 * (Math.sqrt(1 - n * n) - 1); return 0.5 * (Math.sqrt(1 - (n -= 2) * n) + 1); }; exports.inBack = function(n){ var s = 1.70158; return n * n * (( s + 1 ) * n - s); }; exports.outBack = function(n){ var s = 1.70158; return --n * n * ((s + 1) * n + s) + 1; }; exports.inOutBack = function(n){ var s = 1.70158 * 1.525; if ( ( n *= 2 ) < 1 ) return 0.5 * ( n * n * ( ( s + 1 ) * n - s ) ); return 0.5 * ( ( n -= 2 ) * n * ( ( s + 1 ) * n + s ) + 2 ); }; exports.inBounce = function(n){ return 1 - exports.outBounce(1 - n); }; exports.outBounce = function(n){ if ( n < ( 1 / 2.75 ) ) { return 7.5625 * n * n; } else if ( n < ( 2 / 2.75 ) ) { return 7.5625 * ( n -= ( 1.5 / 2.75 ) ) * n + 0.75; } else if ( n < ( 2.5 / 2.75 ) ) { return 7.5625 * ( n -= ( 2.25 / 2.75 ) ) * n + 0.9375; } else { return 7.5625 * ( n -= ( 2.625 / 2.75 ) ) * n + 0.984375; } }; exports.inOutBounce = function(n){ if (n < .5) return exports.inBounce(n * 2) * .5; return exports.outBounce(n * 2 - 1) * .5 + .5; }; exports.inElastic = function(n){ var s, a = 0.1, p = 0.4; if ( n === 0 ) return 0; if ( n === 1 ) return 1; if ( !a || a < 1 ) { a = 1; s = p / 4; } else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI ); return - ( a * Math.pow( 2, 10 * ( n -= 1 ) ) * Math.sin( ( n - s ) * ( 2 * Math.PI ) / p ) ); }; exports.outElastic = function(n){ var s, a = 0.1, p = 0.4; if ( n === 0 ) return 0; if ( n === 1 ) return 1; if ( !a || a < 1 ) { a = 1; s = p / 4; } else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI ); return ( a * Math.pow( 2, - 10 * n) * Math.sin( ( n - s ) * ( 2 * Math.PI ) / p ) + 1 ); }; exports.inOutElastic = function(n){ var s, a = 0.1, p = 0.4; if ( n === 0 ) return 0; if ( n === 1 ) return 1; if ( !a || a < 1 ) { a = 1; s = p / 4; } else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI ); if ( ( n *= 2 ) < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( n -= 1 ) ) * Math.sin( ( n - s ) * ( 2 * Math.PI ) / p ) ); return a * Math.pow( 2, -10 * ( n -= 1 ) ) * Math.sin( ( n - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1; }; // aliases exports['in-quad'] = exports.inQuad; exports['out-quad'] = exports.outQuad; exports['in-out-quad'] = exports.inOutQuad; exports['in-cube'] = exports.inCube; exports['out-cube'] = exports.outCube; exports['in-out-cube'] = exports.inOutCube; exports['in-quart'] = exports.inQuart; exports['out-quart'] = exports.outQuart; exports['in-out-quart'] = exports.inOutQuart; exports['in-quint'] = exports.inQuint; exports['out-quint'] = exports.outQuint; exports['in-out-quint'] = exports.inOutQuint; exports['in-sine'] = exports.inSine; exports['out-sine'] = exports.outSine; exports['in-out-sine'] = exports.inOutSine; exports['in-expo'] = exports.inExpo; exports['out-expo'] = exports.outExpo; exports['in-out-expo'] = exports.inOutExpo; exports['in-circ'] = exports.inCirc; exports['out-circ'] = exports.outCirc; exports['in-out-circ'] = exports.inOutCirc; exports['in-back'] = exports.inBack; exports['out-back'] = exports.outBack; exports['in-out-back'] = exports.inOutBack; exports['in-bounce'] = exports.inBounce; exports['out-bounce'] = exports.outBounce; exports['in-out-bounce'] = exports.inOutBounce; exports['in-elastic'] = exports.inElastic; exports['out-elastic'] = exports.outElastic; exports['in-out-elastic'] = exports.inOutElastic; },{}],2:[function(require,module,exports){ function Emitter(obj) { if (obj) return mixin(obj); }; function mixin(obj) { for (var key in Emitter.prototype) { obj[key] = Emitter.prototype[key]; } return obj; } Emitter.prototype.on = Emitter.prototype.addEventListener = function(event, fn){ this._callbacks = this._callbacks || {}; (this._callbacks['$' + event] = this._callbacks['$' + event] || []) .push(fn); return this; }; Emitter.prototype.once = function(event, fn){ function on() { this.off(event, on); fn.apply(this, arguments); } on.fn = fn; this.on(event, on); return this; }; Emitter.prototype.off = Emitter.prototype.removeListener = Emitter.prototype.removeAllListeners = Emitter.prototype.removeEventListener = function(event, fn){ this._callbacks = this._callbacks || {}; // all if (0 == arguments.length) { this._callbacks = {}; return this; } // specific event var callbacks = this._callbacks['$' + event]; if (!callbacks) return this; // remove all handlers if (1 == arguments.length) { delete this._callbacks['$' + event]; return this; } // remove specific handler var cb; for (var i = 0; i < callbacks.length; i++) { cb = callbacks[i]; if (cb === fn || cb.fn === fn) { callbacks.splice(i, 1); break; } } // Remove event specific arrays for event types that no // one is subscribed for to avoid memory leak. if (callbacks.length === 0) { delete this._callbacks['$' + event]; } return this; }; Emitter.prototype.emit = function(event){ this._callbacks = this._callbacks || {}; var args = [].slice.call(arguments, 1) , callbacks = this._callbacks['$' + event]; if (callbacks) { callbacks = callbacks.slice(0); for (var i = 0, len = callbacks.length; i < len; ++i) { callbacks[i].apply(this, args); } } return this; }; Emitter.prototype.listeners = function(event){ this._callbacks = this._callbacks || {}; return this._callbacks['$' + event] || []; }; Emitter.prototype.hasListeners = function(event){ return !! this.listeners(event).length; }; if (typeof module !== 'undefined') { module.exports = Emitter; } },{}],3:[function(require,module,exports){ var scroll = require('./scroll-to'); function calculateScrollOffset(elem, additionalOffset, alignment) { var body = document.body, html = document.documentElement; var elemRect = elem.getBoundingClientRect(); var clientHeight = html.clientHeight; var documentHeight = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight ); additionalOffset = additionalOffset || 0; var scrollPosition; if (alignment === 'bottom') { scrollPosition = elemRect.bottom - clientHeight; } else if (alignment === 'middle') { scrollPosition = elemRect.bottom - clientHeight / 2 - elemRect.height / 2; } else { // top and default scrollPosition = elemRect.top; } var maxScrollPosition = documentHeight - clientHeight; return Math.min(scrollPosition + additionalOffset + window.pageYOffset, maxScrollPosition); } module.exports = function (elem, options) { options = options || {}; if (typeof elem === 'string') elem = document.querySelector(elem); if (elem) return scroll(0, calculateScrollOffset(elem, options.offset, options.align), options); }; },{"./scroll-to":7}],4:[function(require,module,exports){ (function (process){ // Generated by CoffeeScript 1.12.2 (function() { var getNanoSeconds, hrtime, loadTime, moduleLoadTime, nodeLoadTime, upTime; if ((typeof performance !== "undefined" && performance !== null) && performance.now) { module.exports = function() { return performance.now(); }; } else if ((typeof process !== "undefined" && process !== null) && process.hrtime) { module.exports = function() { return (getNanoSeconds() - nodeLoadTime) / 1e6; }; hrtime = process.hrtime; getNanoSeconds = function() { var hr; hr = hrtime(); return hr[0] * 1e9 + hr[1]; }; moduleLoadTime = getNanoSeconds(); upTime = process.uptime() * 1e9; nodeLoadTime = moduleLoadTime - upTime; } else if (Date.now) { module.exports = function() { return Date.now() - loadTime; }; loadTime = Date.now(); } else { module.exports = function() { return new Date().getTime() - loadTime; }; loadTime = new Date().getTime(); } }).call(this); }).call(this,require('_process')) },{"_process":5}],5:[function(require,module,exports){ // shim for using process in browser var process = module.exports = {}; // cached from whatever global is present so that test runners that stub it // don't break things. But we need to wrap it in a try catch in case it is // wrapped in strict mode code which doesn't define any globals. It's inside a // function because try/catches deoptimize in certain engines. var cachedSetTimeout; var cachedClearTimeout; function defaultSetTimout() { throw new Error('setTimeout has not been defined'); } function defaultClearTimeout () { throw new Error('clearTimeout has not been defined'); } (function () { try { if (typeof setTimeout === 'function') { cachedSetTimeout = setTimeout; } else { cachedSetTimeout = defaultSetTimout; } } catch (e) { cachedSetTimeout = defaultSetTimout; } try { if (typeof clearTimeout === 'function') { cachedClearTimeout = clearTimeout; } else { cachedClearTimeout = defaultClearTimeout; } } catch (e) { cachedClearTimeout = defaultClearTimeout; } } ()) function runTimeout(fun) { if (cachedSetTimeout === setTimeout) { //normal enviroments in sane situations return setTimeout(fun, 0); } // if setTimeout wasn't available but was latter defined if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { cachedSetTimeout = setTimeout; return setTimeout(fun, 0); } try { // when when somebody has screwed with setTimeout but no I.E. maddness return cachedSetTimeout(fun, 0); } catch(e){ try { // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally return cachedSetTimeout.call(null, fun, 0); } catch(e){ // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error return cachedSetTimeout.call(this, fun, 0); } } } function runClearTimeout(marker) { if (cachedClearTimeout === clearTimeout) { //normal enviroments in sane situations return clearTimeout(marker); } // if clearTimeout wasn't available but was latter defined if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { cachedClearTimeout = clearTimeout; return clearTimeout(marker); } try { // when when somebody has screwed with setTimeout but no I.E. maddness return cachedClearTimeout(marker); } catch (e){ try { // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally return cachedClearTimeout.call(null, marker); } catch (e){ // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. // Some versions of I.E. have different rules for clearTimeout vs setTimeout return cachedClearTimeout.call(this, marker); } } } var queue = []; var draining = false; var currentQueue; var queueIndex = -1; function cleanUpNextTick() { if (!draining || !currentQueue) { return; } draining = false; if (currentQueue.length) { queue = currentQueue.concat(queue); } else { queueIndex = -1; } if (queue.length) { drainQueue(); } } function drainQueue() { if (draining) { return; } var timeout = runTimeout(cleanUpNextTick); draining = true; var len = queue.length; while(len) { currentQueue = queue; queue = []; while (++queueIndex < len) { if (currentQueue) { currentQueue[queueIndex].run(); } } queueIndex = -1; len = queue.length; } currentQueue = null; draining = false; runClearTimeout(timeout); } process.nextTick = function (fun) { var args = new Array(arguments.length - 1); if (arguments.length > 1) { for (var i = 1; i < arguments.length; i++) { args[i - 1] = arguments[i]; } } queue.push(new Item(fun, args)); if (queue.length === 1 && !draining) { runTimeout(drainQueue); } }; // v8 likes predictible objects function Item(fun, array) { this.fun = fun; this.array = array; } Item.prototype.run = function () { this.fun.apply(null, this.array); }; process.title = 'browser'; process.browser = true; process.env = {}; process.argv = []; process.version = ''; // empty string to avoid regexp issues process.versions = {}; function noop() {} process.on = noop; process.addListener = noop; process.once = noop; process.off = noop; process.removeListener = noop; process.removeAllListeners = noop; process.emit = noop; process.prependListener = noop; process.prependOnceListener = noop; process.listeners = function (name) { return [] } process.binding = function (name) { throw new Error('process.binding is not supported'); }; process.cwd = function () { return '/' }; process.chdir = function (dir) { throw new Error('process.chdir is not supported'); }; process.umask = function() { return 0; }; },{}],6:[function(require,module,exports){ (function (global){ var now = require('performance-now') , root = typeof window === 'undefined' ? global : window , vendors = ['moz', 'webkit'] , suffix = 'AnimationFrame' , raf = root['request' + suffix] , caf = root['cancel' + suffix] || root['cancelRequest' + suffix] for(var i = 0; !raf && i < vendors.length; i++) { raf = root[vendors[i] + 'Request' + suffix] caf = root[vendors[i] + 'Cancel' + suffix] || root[vendors[i] + 'CancelRequest' + suffix] } // Some versions of FF have rAF but not cAF if(!raf || !caf) { var last = 0 , id = 0 , queue = [] , frameDuration = 1000 / 60 raf = function(callback) { if(queue.length === 0) { var _now = now() , next = Math.max(0, frameDuration - (_now - last)) last = next + _now setTimeout(function() { var cp = queue.slice(0) // Clear queue here to prevent // callbacks from appending listeners // to the current frame's queue queue.length = 0 for(var i = 0; i < cp.length; i++) { if(!cp[i].cancelled) { try{ cp[i].callback(last) } catch(e) { setTimeout(function() { throw e }, 0) } } } }, Math.round(next)) } queue.push({ handle: ++id, callback: callback, cancelled: false }) return id } caf = function(handle) { for(var i = 0; i < queue.length; i++) { if(queue[i].handle === handle) { queue[i].cancelled = true } } } } module.exports = function(fn) { // Wrap in a new function to prevent // `cancel` potentially being assigned // to the native rAF function return raf.call(root, fn) } module.exports.cancel = function() { caf.apply(root, arguments) } module.exports.polyfill = function(object) { if (!object) { object = root; } object.requestAnimationFrame = raf object.cancelAnimationFrame = caf } }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{"performance-now":4}],7:[function(require,module,exports){ var Tween = require('./tween'); var raf = require('raf'); function scroll() { var y = window.pageYOffset || document.documentElement.scrollTop; var x = window.pageXOffset || document.documentElement.scrollLeft; return { top: y, left: x }; } function scrollTo(x, y, options) { options = options || {}; // start position var start = scroll(); // setup tween var tween = Tween(start) .ease(options.ease || 'out-circ') .to({ top: y, left: x }) .duration(options.duration || 1000); // scroll tween.update(function(o){ window.scrollTo(o.left | 0, o.top | 0); }); // handle end tween.on('end', function(){ animate = function(){}; }); // animate function animate() { raf(animate); tween.update(); } animate(); return tween; } module.exports = scrollTo; },{"./tween":8,"raf":6}],8:[function(require,module,exports){ var ease = require('./ease'); var Emitter = require('./emitter'); function extend(obj, src) { for (var key in src) { if (src.hasOwnProperty(key)) obj[key] = src[key]; } return obj; } function Tween(obj) { if (!(this instanceof Tween)) return new Tween(obj); this._from = obj; this.ease('linear'); this.duration(500); } Emitter(Tween.prototype); Tween.prototype.reset = function(){ this.isArray = Object.prototype.toString.call(this._from) === '[object Array]'; this._curr = extend({}, this._from); this._done = false; this._start = Date.now(); return this; }; Tween.prototype.to = function(obj){ this.reset(); this._to = obj; return this; }; Tween.prototype.duration = function(ms){ this._duration = ms; return this; }; Tween.prototype.ease = function(fn){ fn = 'function' == typeof fn ? fn : ease[fn]; if (!fn) throw new TypeError('invalid easing function'); this._ease = fn; return this; }; Tween.prototype.stop = function(){ this.stopped = true; this._done = true; this.emit('stop'); this.emit('end'); return this; }; Tween.prototype.step = function(){ if (this._done) return; var duration = this._duration; var now = Date.now(); var delta = now - this._start; var done = delta >= duration; if (done) { this._from = this._to; this._update(this._to); this._done = true; this.emit('end'); return this; } var from = this._from; var to = this._to; var curr = this._curr; var fn = this._ease; var p = (now - this._start) / duration; var n = fn(p); if (this.isArray) { for (var i = 0; i < from.length; ++i) { curr[i] = from[i] + (to[i] - from[i]) * n; } this._update(curr); return this; } for (var k in from) { curr[k] = from[k] + (to[k] - from[k]) * n; } this._update(curr); return this; }; Tween.prototype.update = function(fn){ if (0 == arguments.length) return this.step(); this._update = fn; return this; }; module.exports = Tween; },{"./ease":1,"./emitter":2}]},{},[3])(3) });