Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
Fixed bug that caused animation to stop several pixels short.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Ferdinandi committed Jan 16, 2014
1 parent 0f4f527 commit 1707d87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ A simple script to animate scrolling to anchor links. Easing support contributed
Getting started with Smooth Scroll is really easy. [View the online tutorial](http://cferdinandi.github.com/smooth-scroll/) or dig through the `index.html` file.

## Changelog
* v2.14 (January 15, 2014)
* [Fixed bug that caused animation to stop several pixels short.](https://github.com/cferdinandi/smooth-scroll/pull/15#issuecomment-32380770)
* v2.12 (January 7, 2014)
* [Added fixed header support.](https://github.com/cferdinandi/smooth-scroll/pull/20#issuecomment-31773547)
* v2.11 (January 4, 2014)
Expand Down
59 changes: 25 additions & 34 deletions smooth-scroll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* =============================================================
Smooth Scroll 2.12
Smooth Scroll 2.14
Animate scrolling to anchor links, by Chris Ferdinandi.
http://gomakethings.com
Expand Down Expand Up @@ -55,12 +55,13 @@
}
};

// Calculate how far and how fast to scroll
// http://www.quirksmode.org/blog/archives/2008/01/using_the_assig.html
var startLocation = window.pageYOffset;
var scrollHeader = document.querySelector( '.scroll-header' );
// Get the height of a fixed header if one exists
var scrollHeader = document.querySelector('.scroll-header');
var headerHeight = scrollHeader === null ? 0 : scrollHeader.offsetHeight;
var endLocation = function (anchor) {

// Calculate how far to scroll
var startLocation = window.pageYOffset;
var getEndLocation = function (anchor) {
var distance = 0;
if (anchor.offsetParent) {
do {
Expand All @@ -70,42 +71,33 @@
}
return distance - headerHeight;
};
var distance = endLocation(anchor) - startLocation;
var increments = distance / (duration / 16);
var endLocation = getEndLocation(anchor);
var distance = endLocation - startLocation;

// Function to stop the scrolling animation
var stopAnimation = function () {
var currentLocation = window.pageYOffset;
// var there = endLocation(anchor);
if ( currentLocation == endLocation || ( (window.innerHeight + currentLocation) >= document.body.scrollHeight ) ) {
clearInterval(runAnimation);
updateURL(url, anchor);
}
};

// Set the animation variables to 0/undefined.
var timeLapsed = 0;
var percentage, position, stopAnimation;
var percentage, position;

// Scroll the page by an increment, and check if it's time to stop
var animateScroll = function () {
timeLapsed += 16;
percentage = ( timeLapsed / duration );
percentage = ( percentage > 1 ) ? 1 : percentage;
position = startLocation + ( distance * easingPattern(easing, percentage) );
window.scrollTo(0, position);
window.scrollTo( 0, position );
stopAnimation();
};

// Stop the animation
if ( increments >= 0 ) { // If scrolling down
// Stop animation when you reach the anchor OR the bottom of the page
stopAnimation = function () {
var travelled = window.pageYOffset;
if ( (travelled >= (endLocation(anchor) - increments)) || ((window.innerHeight + travelled) >= document.body.scrollHeight) ) {
clearInterval(runAnimation);
updateURL(url, anchor);
}
};
} else { // If scrolling up
// Stop animation when you reach the anchor OR the top of the page
stopAnimation = function () {
var travelled = window.pageYOffset;
if ( travelled <= endLocation(anchor) || travelled <= 0 ) {
clearInterval(runAnimation);
updateURL(url, anchor);
}
};
}

// Loop the animation function
var runAnimation = setInterval(animateScroll, 16);

Expand All @@ -128,10 +120,9 @@
var dataEasing = toggle.getAttribute('data-easing');
var dataURL = toggle.getAttribute('data-url');

// If the anchor exists
// If the anchor exists, scroll to it
if (dataTarget) {
// Scroll to the anchor
smoothScroll(dataTarget, dataSpeed || 500, dataEasing || 'easeInOutCubic', dataURL || 'false');
smoothScroll( dataTarget, dataSpeed || 500, dataEasing || 'easeInOutCubic', dataURL || 'false' );
}

}, false);
Expand Down

0 comments on commit 1707d87

Please sign in to comment.