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

Commit

Permalink
Updated variables for more accurate easing math when scrolling to top…
Browse files Browse the repository at this point in the history
… of page.

#25 (comment)
29
  • Loading branch information
Chris Ferdinandi committed Jan 17, 2014
1 parent b43ffe2 commit 05101b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 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.16 (Junuary 16, 2014)
* [Updated variables for more accurate easing math when scrolling to top of page.](https://github.com/cferdinandi/smooth-scroll/pull/25#issuecomment-32566729)
* v2.15 (January 16, 2014)
* [Fixed bug that caused "scroll-to-top" animation to create endless loop.](https://github.com/cferdinandi/smooth-scroll/issues/24)
* v2.14 (January 15, 2014)
Expand Down
26 changes: 12 additions & 14 deletions smooth-scroll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* =============================================================
Smooth Scroll 2.15
Smooth Scroll 2.16
Animate scrolling to anchor links, by Chris Ferdinandi.
http://gomakethings.com
Expand Down Expand Up @@ -62,31 +62,29 @@
// Calculate how far to scroll
var startLocation = window.pageYOffset;
var getEndLocation = function (anchor) {
var distance = 0;
var location = 0;
if (anchor.offsetParent) {
do {
distance += anchor.offsetTop;
location += anchor.offsetTop;
anchor = anchor.offsetParent;
} while (anchor);
}
return distance - headerHeight;
location = location - headerHeight;
if ( location >= 0 ) {
return location;
} else {
return 0;
}
};
var endLocation = getEndLocation(anchor);
var distance = endLocation - startLocation;

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

Expand Down

0 comments on commit 05101b1

Please sign in to comment.