Skip to content

Commit 69e93a5

Browse files
committed
Pass anchor and toggle into offset()
Addresses... - cferdinandi#410 - cferdinandi#386 - cferdinandi#367
1 parent 1fbe904 commit 69e93a5

11 files changed

+32
-19
lines changed

README.md

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

128128
// Speed & Easing
129129
speed: 500, // Integer. How fast to complete the scroll in milliseconds
130-
offset: 0, // Integer or Function returning an integer. How far to offset the scrolling anchor location in pixels
130+
offset: function (anchor, toggle) {
131+
132+
// Integer or Function returning an integer. How far to offset the scrolling anchor location in pixels
133+
// This example is a function, but you could do something as simple as `offset: 25`
134+
135+
// An example returning different values based on whether the clicked link was in the header nav or not
136+
if (toggle.classList.closest('.my-header-nav')) {
137+
return 25;
138+
} else {
139+
return 50;
140+
}
141+
142+
},
131143
easing: 'easeInOutCubic', // Easing pattern to use
132144
customEasing: function (time) {
133145

@@ -317,6 +329,7 @@ If you have multiple fixed headers, pass in the last one in the markup.
317329
- Custom events let you more easily hook into Smooth Scroll from other scripts.
318330
- The feature test is scoped to a function, allowing for server-side implementations.
319331
- The loss of styling with IDs experienced in earlier versions has been eliminated.
332+
- The `anchor` and `toggle` elements are now passed into the `offset()` function, allowing for more customization [v14.1.0 and up].
320333

321334
### Breaking Changes
322335

dist/smooth-scroll.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v14.0.1: Animate scrolling to anchor links
2+
* smooth-scroll v14.1.0: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -405,7 +405,7 @@
405405
// Get the height of a fixed header if one exists and not already set
406406
headerHeight = getHeaderHeight(fixedHeader);
407407
}
408-
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof animateSettings.offset === 'function' ? animateSettings.offset() : animateSettings.offset), 10)); // Location to scroll to
408+
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof animateSettings.offset === 'function' ? animateSettings.offset(anchor, toggle) : animateSettings.offset), 10)); // Location to scroll to
409409
var distance = endLocation - startLocation; // distance to travel
410410
var documentHeight = getDocumentHeight();
411411
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v14.0.1: Animate scrolling to anchor links
2+
* smooth-scroll v14.1.0: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -475,7 +475,7 @@ if (window.Element && !Element.prototype.closest) {
475475
// Get the height of a fixed header if one exists and not already set
476476
headerHeight = getHeaderHeight(fixedHeader);
477477
}
478-
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof animateSettings.offset === 'function' ? animateSettings.offset() : animateSettings.offset), 10)); // Location to scroll to
478+
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof animateSettings.offset === 'function' ? animateSettings.offset(anchor, toggle) : animateSettings.offset), 10)); // Location to scroll to
479479
var distance = endLocation - startLocation; // distance to travel
480480
var documentHeight = getDocumentHeight();
481481
var timeLapsed = 0;

0 commit comments

Comments
 (0)