Skip to content

Commit c2445fa

Browse files
author
Chris Ferdinandi
committed
Merge branch 'offset-option'
2 parents c02d1c1 + 1747f24 commit c2445fa

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ You can pass options and callbacks into Smooth Scroll through the `init()` funct
5454

5555
```javascript
5656
smoothScroll.init({
57-
speed: 500, // How fast to complete the scroll in milliseconds
57+
speed: 500, // Integer. How fast to complete the scroll in milliseconds
5858
easing: 'easeInOutCubic', // Easing pattern to use
5959
updateURL: false, // Boolean. Whether or not to update the URL with the anchor hash on scroll
60+
offset: 0, // Integer. How far to offset the scrolling anchor location in pixels
6061
callbackBefore: function ( toggle, anchor ) {}, // Function to run before scrolling
6162
callbackAfter: function ( toggle, anchor ) {} // Function to run after scrolling
6263
});
@@ -107,6 +108,7 @@ Smooth Scroll also lets you override global settings on a link-by-link basis usi
107108
<a data-scroll
108109
data-options="speed: 500;
109110
easing: easeInOutCubic;
111+
offset: 0;
110112
updateURL: false"
111113
>
112114
Anchor Link
@@ -183,6 +185,8 @@ Smooth Scroll is licensed under the [MIT License](http://gomakethings.com/mit/).
183185

184186

185187
## Changelog
188+
* v4.5 - March 20, 2014
189+
* Added `offset` to `options`
186190
* v4.4 - March 15, 2014
187191
* [Fixed iOS scroll-to-top bug](https://github.com/cferdinandi/smooth-scroll/issues/45).
188192
* v4.3 - March 5, 2014

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ <h1 style="text-align: center; font-size: 3em;">Smooth Scroll</h1>
7171
smoothScroll.init({
7272
speed: 500,
7373
easing: 'easeInOutCubic',
74+
offset: 0,
7475
updateURL: false,
7576
callbackBefore: function ( toggle, anchor ) {},
7677
callbackAfter: function ( toggle, anchor ) {}

smooth-scroll.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* =============================================================
22
3-
Smooth Scroll v4.4
3+
Smooth Scroll v4.5
44
Animate scrolling to anchor links, by Chris Ferdinandi.
55
http://gomakethings.com
66
@@ -21,6 +21,7 @@ window.smoothScroll = (function (window, document, undefined) {
2121
var _defaults = {
2222
speed: 500,
2323
easing: 'easeInOutCubic',
24+
offset: 0,
2425
updateURL: false,
2526
callbackBefore: function () {},
2627
callbackAfter: function () {}
@@ -116,15 +117,16 @@ window.smoothScroll = (function (window, document, undefined) {
116117
// Options and overrides
117118
options = _mergeObjects( _defaults, options || {} ); // Merge user options with defaults
118119
var overrides = _getDataOptions( toggle ? toggle.getAttribute('data-options') : null );
119-
var speed = overrides.speed || options.speed;
120+
var speed = parseInt(overrides.speed || options.speed, 10);
120121
var easing = overrides.easing || options.easing;
122+
var offset = parseInt(overrides.offset || options.offset, 10);
121123
var updateURL = overrides.updateURL || options.updateURL;
122124

123125
// Selectors and variables
124126
var fixedHeader = document.querySelector('[data-scroll-header]'); // Get the fixed header
125127
var headerHeight = fixedHeader === null ? 0 : (fixedHeader.offsetHeight + fixedHeader.offsetTop); // Get the height of a fixed header if one exists
126128
var startLocation = window.pageYOffset; // Current location on the page
127-
var endLocation = _getEndLocation( document.querySelector(anchor), headerHeight ); // Scroll to location
129+
var endLocation = _getEndLocation( document.querySelector(anchor), headerHeight + offset ); // Scroll to location
128130
var animationInterval; // interval timer
129131
var distance = endLocation - startLocation; // distance to travel
130132
var timeLapsed = 0;

0 commit comments

Comments
 (0)