Skip to content

Commit c58b618

Browse files
author
Chris Ferdinandi
committed
Fixed scroll-to-top bug for links at the bottom of the page
1 parent c7587df commit c58b618

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ Smooth Scroll is licensed under the [MIT License](http://gomakethings.com/mit/).
185185

186186

187187
## Changelog
188+
* v4.6 - March 21, 2014
189+
* [Fixed scroll-to-top bug for links at the bottom of the page](https://github.com/cferdinandi/smooth-scroll/issues/49).
188190
* v4.5 - March 20, 2014
189191
* Added `offset` to `options`
190192
* v4.4 - March 15, 2014

smooth-scroll.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ window.smoothScroll = (function (window, document, undefined) {
7575
}
7676
};
7777

78+
// Determine the document's height
79+
// Private method
80+
// Returns an integer
81+
var _getDocumentHeight = function () {
82+
return Math.max(
83+
document.body.scrollHeight, document.documentElement.scrollHeight,
84+
document.body.offsetHeight, document.documentElement.offsetHeight,
85+
document.body.clientHeight, document.documentElement.clientHeight
86+
);
87+
};
88+
7889
// Convert data-options attribute into an object of key/value pairs
7990
// Private method
8091
// Returns an {object}
@@ -109,17 +120,6 @@ window.smoothScroll = (function (window, document, undefined) {
109120
}
110121
};
111122

112-
// Determine the document's height (cross-browser)
113-
// Fix for issue 49 (https://github.com/cferdinandi/smooth-scroll/issues/49)
114-
// Private method
115-
var _getDocumentHeight = function () {
116-
return Math.max(
117-
document.body.scrollHeight, document.documentElement.scrollHeight,
118-
document.body.offsetHeight, document.documentElement.offsetHeight,
119-
document.body.clientHeight, document.documentElement.clientHeight
120-
);
121-
};
122-
123123
// Start/stop the scrolling animation
124124
// Public method
125125
// Runs functions
@@ -140,6 +140,7 @@ window.smoothScroll = (function (window, document, undefined) {
140140
var endLocation = _getEndLocation( document.querySelector(anchor), headerHeight + offset ); // Scroll to location
141141
var animationInterval; // interval timer
142142
var distance = endLocation - startLocation; // distance to travel
143+
var documentHeight = _getDocumentHeight();
143144
var timeLapsed = 0;
144145
var percentage, position;
145146

@@ -156,7 +157,7 @@ window.smoothScroll = (function (window, document, undefined) {
156157
// Runs functions
157158
var _stopAnimateScroll = function (position, endLocation, animationInterval) {
158159
var currentLocation = window.pageYOffset;
159-
if ( position == endLocation || currentLocation == endLocation || ( (window.innerHeight + currentLocation) >= _getDocumentHeight() ) ) {
160+
if ( position == endLocation || currentLocation == endLocation || ( (window.innerHeight + currentLocation) >= documentHeight ) ) {
160161
clearInterval(animationInterval);
161162
options.callbackAfter( toggle, anchor ); // Run callbacks after animation complete
162163
}

0 commit comments

Comments
 (0)