diff --git a/elevator.js b/elevator.js index 3634e41..90b784f 100644 --- a/elevator.js +++ b/elevator.js @@ -10,8 +10,7 @@ *********************************************/ var Elevator = function(options) { - - 'use strict'; + "use strict"; // Elements var body = null; @@ -39,17 +38,18 @@ var Elevator = function(options) { */ // Thanks Mr Penner - http://robertpenner.com/easing/ - function easeInOutQuad( t, b, c, d ) { + function easeInOutQuad(t, b, c, d) { t /= d / 2; - if ( t < 1 ) return c / 2 * t * t + b; + if (t < 1) return c / 2 * t * t + b; t--; - return -c / 2 * ( t * ( t -2 ) - 1 ) + b; + return -c / 2 * (t * (t - 2) - 1) + b; } - function extendParameters(options, defaults){ - for( var option in defaults ){ - var t = options[option] === undefined && typeof option !== "function"; - if(t){ + function extendParameters(options, defaults) { + for (var option in defaults) { + var t = + options[option] === undefined && typeof option !== "function"; + if (t) { options[option] = defaults[option]; } } @@ -58,13 +58,13 @@ var Elevator = function(options) { function getVerticalOffset(element) { var verticalOffset = 0; - while( element ){ + while (element) { verticalOffset += element.offsetTop || 0; element = element.offsetParent; } - if ( verticalPadding ) { - verticalOffset = verticalOffset - verticalPadding; + if (verticalPadding) { + verticalOffset = verticalOffset - verticalPadding; } return verticalOffset; @@ -75,65 +75,73 @@ var Elevator = function(options) { */ // Time is passed through requestAnimationFrame, what a world! - function animateLoop( time ) { - if ( !startTime ) { + function animateLoop(time) { + if (!startTime) { startTime = time; } var timeSoFar = time - startTime; - var easedPosition = easeInOutQuad(timeSoFar, startPosition, endPosition - startPosition, duration); + var easedPosition = easeInOutQuad( + timeSoFar, + startPosition, + endPosition - startPosition, + duration + ); window.scrollTo(0, easedPosition); - if( timeSoFar < duration ) { + if (timeSoFar < duration) { animation = requestAnimationFrame(animateLoop); } else { animationFinished(); } } -// ELEVATE! -// / -// ____ -// .' '=====<0 -// |======| -// |======| -// [IIIIII[\--() -// |_______| -// C O O O D -// C O O O D -// C O O O D -// C__O__O__O__D -// [_____________] + // ELEVATE! + // / + // ____ + // .' '=====<0 + // |======| + // |======| + // [IIIIII[\--() + // |_______| + // C O O O D + // C O O O D + // C O O O D + // C__O__O__O__D + // [_____________] this.elevate = function() { - - if( elevating ) { + if (elevating) { return; } elevating = true; - startPosition = (document.documentElement.scrollTop || body.scrollTop); + startPosition = document.documentElement.scrollTop || body.scrollTop; updateEndPosition(); // No custom duration set, so we travel at pixels per millisecond. (0.75px per ms) - if( !customDuration ) { - duration = ( Math.abs(endPosition - startPosition) * 1.5); + if (!customDuration) { + duration = Math.abs(endPosition - startPosition) * 1.5; } - requestAnimationFrame( animateLoop ); + requestAnimationFrame(animateLoop); // Start music! - if( mainAudio ) { + if (mainAudio) { mainAudio.play(); } - if( startCallback ) { + if (startCallback) { startCallback(); } }; function browserMeetsRequirements() { - return window.requestAnimationFrame && window.Audio && window.addEventListener; + return ( + window.requestAnimationFrame && + window.Audio && + window.addEventListener + ); } function resetPositions() { @@ -143,39 +151,36 @@ var Elevator = function(options) { } function updateEndPosition() { - if(targetElement){ + if (targetElement) { endPosition = getVerticalOffset(targetElement); } } function animationFinished() { - resetPositions(); // Stop music! - if( mainAudio ) { + if (mainAudio) { mainAudio.pause(); mainAudio.currentTime = 0; } - if( endAudio ) { + if (endAudio) { endAudio.play(); } - if( endCallback ) { + if (endCallback) { endCallback(); } } function onWindowBlur() { - // If animating, go straight to the top. And play no more music. - if( elevating ) { - - cancelAnimationFrame( animation ); + if (elevating) { + cancelAnimationFrame(animation); resetPositions(); - if( mainAudio ) { + if (mainAudio) { mainAudio.pause(); mainAudio.currentTime = 0; } @@ -185,12 +190,12 @@ var Elevator = function(options) { } } - function bindElevateToElement( element ) { - if( element.addEventListener ) { - element.addEventListener('click', that.elevate, false); + function bindElevateToElement(element) { + if (element.addEventListener) { + element.addEventListener("click", that.elevate, false); } else { // Older browsers - element.attachEvent('onclick', function() { + element.attachEvent("onclick", function() { updateEndPosition(); document.documentElement.scrollTop = endPosition; document.body.scrollTop = endPosition; @@ -199,7 +204,7 @@ var Elevator = function(options) { } } - function init( _options ) { + function init(_options) { // Bind to element click event, if need be. body = document.body; @@ -215,46 +220,46 @@ var Elevator = function(options) { _options = extendParameters(_options, defaults); - if( _options.element ) { - bindElevateToElement( _options.element ); + if (_options.element) { + bindElevateToElement(_options.element); } // Take the stairs instead - if( !browserMeetsRequirements() ) { + if (!browserMeetsRequirements()) { return; } - if( _options.duration ) { + if (_options.duration) { customDuration = true; duration = _options.duration; } - if( _options.targetElement ) { + if (_options.targetElement) { targetElement = _options.targetElement; } - if( _options.verticalPadding ) { + if (_options.verticalPadding) { verticalPadding = _options.verticalPadding; } - window.addEventListener('blur', onWindowBlur, false); + window.addEventListener("blur", onWindowBlur, false); - if( _options.mainAudio ) { - mainAudio = new Audio( _options.mainAudio ); - mainAudio.setAttribute( 'preload', _options.preloadAudio ); - mainAudio.setAttribute( 'loop', _options.loopAudio ); + if (_options.mainAudio) { + mainAudio = new Audio(_options.mainAudio); + mainAudio.setAttribute("preload", _options.preloadAudio); + mainAudio.setAttribute("loop", _options.loopAudio); } - if( _options.endAudio ) { - endAudio = new Audio( _options.endAudio ); - endAudio.setAttribute( 'preload', 'true' ); + if (_options.endAudio) { + endAudio = new Audio(_options.endAudio); + endAudio.setAttribute("preload", "true"); } - if( _options.endCallback ) { + if (_options.endCallback) { endCallback = _options.endCallback; } - if( _options.startCallback ) { + if (_options.startCallback) { startCallback = _options.startCallback; } } @@ -262,4 +267,6 @@ var Elevator = function(options) { init(options); }; -module.exports = Elevator; +if (typeof module !== "undefined" && module.exports) { + module.exports = Elevator; +} diff --git a/elevator.min.js b/elevator.min.js index 1cbeac3..f30692a 100644 --- a/elevator.min.js +++ b/elevator.min.js @@ -1 +1 @@ -var Elevator=function(options){"use strict";var body=null;var animation=null;var duration=null;var customDuration=false;var startTime=null;var startPosition=null;var endPosition=0;var targetElement=null;var verticalPadding=null;var elevating=false;var startCallback;var mainAudio;var endAudio;var endCallback;var that=this;function easeInOutQuad(t,b,c,d){t/=d/2;if(t<1)return c/2*t*t+b;t--;return-c/2*(t*(t-2)-1)+b}function extendParameters(options,defaults){for(var option in defaults){var t=options[option]===undefined&&typeof option!=="function";if(t){options[option]=defaults[option]}}return options}function getVerticalOffset(element){var verticalOffset=0;while(element){verticalOffset+=element.offsetTop||0;element=element.offsetParent}if(verticalPadding){verticalOffset=verticalOffset-verticalPadding}return verticalOffset}function animateLoop(time){if(!startTime){startTime=time}var timeSoFar=time-startTime;var easedPosition=easeInOutQuad(timeSoFar,startPosition,endPosition-startPosition,duration);window.scrollTo(0,easedPosition);if(timeSoFarn?t/2*n*n+e:(n--,-t/2*(n*(n-2)-1)+e)}function t(n,e){for(var t in e){var o=void 0===n[t]&&"function"!=typeof t;o&&(n[t]=e[t])}return n}function o(n){for(var e=0;n;)e+=n.offsetTop||0,n=n.offsetParent;return g&&(e-=g),e}function l(n){T||(T=n);var t=n-T,o=e(t,k,y-k,b);window.scrollTo(0,o),b>t?w=requestAnimationFrame(l):r()}function i(){return window.requestAnimationFrame&&window.Audio&&window.addEventListener}function u(){T=null,k=null,h=!1}function a(){C&&(y=o(C))}function r(){u(),f&&(f.pause(),f.currentTime=0),p&&p.play(),v&&v()}function d(){h&&(cancelAnimationFrame(w),u(),f&&(f.pause(),f.currentTime=0),a(),window.scrollTo(0,y))}function c(n){n.addEventListener?n.addEventListener("click",F.elevate,!1):n.attachEvent("onclick",function(){a(),document.documentElement.scrollTop=y,document.body.scrollTop=y,window.scroll(0,y)})}function s(n){A=document.body;var e={duration:void 0,mainAudio:!1,endAudio:!1,preloadAudio:!0,loopAudio:!0,startCallback:null,endCallback:null};n=t(n,e),n.element&&c(n.element),i()&&(n.duration&&(E=!0,b=n.duration),n.targetElement&&(C=n.targetElement),n.verticalPadding&&(g=n.verticalPadding),window.addEventListener("blur",d,!1),n.mainAudio&&(f=new Audio(n.mainAudio),f.setAttribute("preload",n.preloadAudio),f.setAttribute("loop",n.loopAudio)),n.endAudio&&(p=new Audio(n.endAudio),p.setAttribute("preload","true")),n.endCallback&&(v=n.endCallback),n.startCallback&&(m=n.startCallback))}var m,f,p,v,A=null,w=null,b=null,E=!1,T=null,k=null,y=0,C=null,g=null,h=!1,F=this;this.elevate=function(){h||(h=!0,k=document.documentElement.scrollTop||A.scrollTop,a(),E||(b=1.5*Math.abs(y-k)),requestAnimationFrame(l),f&&f.play(),m&&m())},s(n)};"undefined"!=typeof module&&module.exports&&(module.exports=Elevator); \ No newline at end of file diff --git a/package.json b/package.json index bb79669..ca550aa 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,9 @@ { "name": "elevator.js", - "version": "1.0.0", - "description": "Elevator.js fixes those awkward \"scroll to top\" moments the old fashioned way.", - "keywords": [ - "scroll", - "top", - "elevator" - ], + "version": "1.0.1", + "description": + "Elevator.js fixes those awkward \"scroll to top\" moments the old fashioned way.", + "keywords": ["scroll", "top", "elevator"], "homepage": "http://tholman.com/elevator.js", "bugs": { "url": "https://github.com/tholman/elevator.js/issues"