Skip to content

Commit 8ccb056

Browse files
authored
Merge pull request cferdinandi#436 from cferdinandi/development
Added clipped scrolling support
2 parents 07e17c7 + 85c395c commit 8ccb056

11 files changed

+74
-43
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ var scroll = new SmoothScroll('a[href*="#"]', {
127127

128128
// Speed & Easing
129129
speed: 500, // Integer. How fast to complete the scroll in milliseconds
130+
clip: true, // If true, adjust scroll distance to prevent abrupt stops near the bottom of the page
130131
offset: function (anchor, toggle) {
131132

132133
// Integer or Function returning an integer. How far to offset the scrolling anchor location in pixels

dist/smooth-scroll.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v14.1.1: Animate scrolling to anchor links
2+
* smooth-scroll v14.2.0: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -31,6 +31,7 @@
3131

3232
// Speed & Easing
3333
speed: 500,
34+
clip: true,
3435
offset: 0,
3536
easing: 'easeInOutCubic',
3637
customEasing: null,
@@ -254,12 +255,14 @@
254255

255256
/**
256257
* Calculate how far to scroll
257-
* @param {Element} anchor The anchor element to scroll to
258-
* @param {Number} headerHeight Height of a fixed header, if any
259-
* @param {Number} offset Number of pixels by which to offset scroll
258+
* Clip support added by robjtede - https://github.com/cferdinandi/smooth-scroll/issues/405
259+
* @param {Element} anchor The anchor element to scroll to
260+
* @param {Number} headerHeight Height of a fixed header, if any
261+
* @param {Number} offset Number of pixels by which to offset scroll
262+
* @param {Boolean} clip If true, adjust scroll distance to prevent abrupt stops near the bottom of the page
260263
* @returns {Number}
261264
*/
262-
var getEndLocation = function (anchor, headerHeight, offset) {
265+
var getEndLocation = function (anchor, headerHeight, offset, clip) {
263266
var location = 0;
264267
if (anchor.offsetParent) {
265268
do {
@@ -268,7 +271,10 @@
268271
} while (anchor);
269272
}
270273
location = Math.max(location - headerHeight - offset, 0);
271-
return location;
274+
if (clip) {
275+
location = Math.min(location, getDocumentHeight() - window.innerHeight);
276+
}
277+
return location;
272278
};
273279

274280
/**
@@ -405,7 +411,7 @@
405411
// Get the height of a fixed header if one exists and not already set
406412
headerHeight = getHeaderHeight(fixedHeader);
407413
}
408-
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof animateSettings.offset === 'function' ? animateSettings.offset(anchor, toggle) : animateSettings.offset), 10)); // Location to scroll to
414+
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof animateSettings.offset === 'function' ? animateSettings.offset(anchor, toggle) : animateSettings.offset), 10), animateSettings.clip); // Location to scroll to
409415
var distance = endLocation - startLocation; // distance to travel
410416
var documentHeight = getDocumentHeight();
411417
var timeLapsed = 0;

dist/smooth-scroll.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/smooth-scroll.polyfills.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v14.1.1: Animate scrolling to anchor links
2+
* smooth-scroll v14.2.0: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -101,6 +101,7 @@ if (window.Element && !Element.prototype.closest) {
101101

102102
// Speed & Easing
103103
speed: 500,
104+
clip: true,
104105
offset: 0,
105106
easing: 'easeInOutCubic',
106107
customEasing: null,
@@ -324,12 +325,14 @@ if (window.Element && !Element.prototype.closest) {
324325

325326
/**
326327
* Calculate how far to scroll
327-
* @param {Element} anchor The anchor element to scroll to
328-
* @param {Number} headerHeight Height of a fixed header, if any
329-
* @param {Number} offset Number of pixels by which to offset scroll
328+
* Clip support added by robjtede - https://github.com/cferdinandi/smooth-scroll/issues/405
329+
* @param {Element} anchor The anchor element to scroll to
330+
* @param {Number} headerHeight Height of a fixed header, if any
331+
* @param {Number} offset Number of pixels by which to offset scroll
332+
* @param {Boolean} clip If true, adjust scroll distance to prevent abrupt stops near the bottom of the page
330333
* @returns {Number}
331334
*/
332-
var getEndLocation = function (anchor, headerHeight, offset) {
335+
var getEndLocation = function (anchor, headerHeight, offset, clip) {
333336
var location = 0;
334337
if (anchor.offsetParent) {
335338
do {
@@ -338,7 +341,10 @@ if (window.Element && !Element.prototype.closest) {
338341
} while (anchor);
339342
}
340343
location = Math.max(location - headerHeight - offset, 0);
341-
return location;
344+
if (clip) {
345+
location = Math.min(location, getDocumentHeight() - window.innerHeight);
346+
}
347+
return location;
342348
};
343349

344350
/**
@@ -475,7 +481,7 @@ if (window.Element && !Element.prototype.closest) {
475481
// Get the height of a fixed header if one exists and not already set
476482
headerHeight = getHeaderHeight(fixedHeader);
477483
}
478-
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof animateSettings.offset === 'function' ? animateSettings.offset(anchor, toggle) : animateSettings.offset), 10)); // Location to scroll to
484+
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof animateSettings.offset === 'function' ? animateSettings.offset(anchor, toggle) : animateSettings.offset), 10), animateSettings.clip); // Location to scroll to
479485
var distance = endLocation - startLocation; // distance to travel
480486
var documentHeight = getDocumentHeight();
481487
var timeLapsed = 0;

0 commit comments

Comments
 (0)