A lightweight script to animate scrolling to anchor links. Smooth Scroll works great with Gumshoe.
Download Smooth Scroll / View the demo
In This Documentation
- Getting Started
- Installing with Package Managers
- Working with the Source Files
- Options & Settings
- Browser Compatibility
- Known Issues
- Contributors
- How to Contribute
- License
- Changelog
- Older Docs
Compiled and production-ready code can be found in the dist
directory. The src
directory contains development code. Unit tests are located in the test
directory.
<script src="dist/js/smooth-scroll.js"></script>
<a data-scroll href="#bazinga">Anchor Link</a>
...
<span id="bazinga">Bazinga!</span>
Turn anchor links into Smooth Scroll links by adding the [data-scroll]
data attribute. Give the anchor location an ID just like you normally would.
<script>
smoothScroll.init();
</script>
In the footer of your page, after the content, initialize Smooth Scroll. And that's it, you're done. Nice work!
You can install Smooth Scroll with your favorite package manager.
- NPM:
npm install cferdinandi/smooth-scroll
- Bower:
bower install https://github.com/cferdinandi/smooth-scroll.git
- Component:
component install cferdinandi/smooth-scroll
If you would prefer, you can work with the development code in the src
directory using the included Gulp build system. This compiles, lints, and minifies code, and runs unit tests. It's the same build system that's used by Kraken, so it includes some unnecessary tasks and Sass variables but can be dropped right in to the boilerplate without any configuration.
Make sure these are installed first.
- In bash/terminal/command line,
cd
into your project directory. - Run
npm install
to install required files. - When it's done installing, run one of the task runners to get going:
gulp
manually compiles files.gulp watch
automatically compiles files when changes are made.gulp reload
automatically compiles files and applies changes using LiveReload.
Smooth Scroll includes smart defaults and works right out of the box. But if you want to customize things, it also has a robust API that provides multiple ways for you to adjust the default options and settings.
You can pass options and callbacks into Smooth Scroll through the init()
function:
smoothScroll.init({
speed: 500, // Integer. How fast to complete the scroll in milliseconds
easing: 'easeInOutCubic', // Easing pattern to use
updateURL: true, // Boolean. Whether or not to update the URL with the anchor hash on scroll
offset: 0, // Integer. How far to offset the scrolling anchor location in pixels
callbackBefore: function ( toggle, anchor ) {}, // Function to run before scrolling
callbackAfter: function ( toggle, anchor ) {} // Function to run after scrolling
});
Linear Moves at the same speed from start to finish.
Linear
Ease-In Gradually increases in speed.
easeInQuad
easeInCubic
easeInQuart
easeInQuint
Ease-In-Out Gradually increases in speed, peaks, and then gradually slows down.
easeInOutQuad
easeInOutCubic
easeInOutQuart
easeInOutQuint
Ease-Out Gradually decreases in speed.
easeOutQuad
easeOutCubic
easeOutQuart
easeOutQuint
Learn more about the different easing patterns and what they do at easings.net.
Smooth Scroll also lets you override global settings on a link-by-link basis using the [data-options]
data attribute:
<a data-scroll
data-options='{
"speed": 500,
"easing": "easeInOutCubic",
"offset": 0,
"updateURL": false
}'
>
Anchor Link
</a>
Note: You must use valid JSON in order for the data-options
feature to work.
You can also call Smooth Scroll's scroll animation events in your own scripts.
Animate scrolling to an anchor.
smoothScroll.animateScroll(
toggle, // Node that toggles the animation. ex. document.querySelector('#toggle')
anchor, // ID of the anchor to scroll to. ex. '#bazinga'
options // Classes and callbacks. Same options as those passed into the init() function.
);
Example 1
smoothScroll.animateScroll( null, '#bazinga' );
Example 2
var toggle = document.querySelector('#toggle');
var options = { speed: 1000, easing: 'easeOutCubic' };
smoothScroll.animateScroll( toggle, '#bazinga', options );
Destroy the current smoothScroll.init()
. This is called automatically during the init
function to remove any existing initializations.
smoothScroll.destroy();
Add a [data-scroll-header]
data attribute to fixed headers. Smooth Scroll will automatically offset scroll distances by the header height. If you have multiple fixed headers, add [data-scroll-header]
to the last one in the markup.
<nav data-scroll-header>
...
</nav>
Smooth Scroll works in all modern browsers, and IE 9 and above.
Smooth Scroll is built with modern JavaScript APIs, and uses progressive enhancement. If the JavaScript file fails to load, or if your site is viewed on older and less capable browsers, anchor links will jump the way they normally would. If you need to smooth scrolling for older browsers, download the jQuery version of Smooth Scroll on GitHub.
If the <body>
element has been assigned a height of 100%
, Smooth Scroll is unable to properly calculate page distances and will not scroll to the right location. The <body>
element can have a fixed, non-percentage based height (ex. 500px
), or a height of auto
.
- Easing support contributed by Willem Liu.
- Easing functions forked from Gaëtan Renaudeau.
- URL history support contributed by Robert Pate.
- Fixed header support contributed by Arndt von Lucadou.
- Infinite loop bugs in iOS and Chrome (when zoomed) by Alex Guzman.
- IE10 rounding error fixed by Luke Siedle.
- Enhanced callback functions by Constant Meiring.
- Scroll-to-top bug for links at the bottom of the page by Jonas Havers.
- AMD support and numerous code improvements by Todd Motto.
- Push State bug fix by Yanick Witschi.
- CommonJS module support by Riku Rouvila.
- Query string fix when updating URL by Qu Yatong.
- Scroll to top support by Robbert Broersma.
- Unit tests by Thibaud Colas.
In lieu of a formal style guide, take care to maintain the existing coding style. Don't forget to update the version number, the changelog (in the readme.md
file), and when applicable, the documentation.
Smooth Scroll is licensed under the MIT License.
Smooth Scroll uses semantic versioning.
- v5.3.3 - December 21, 2014
- Adjust how fixed header is set for better accuracy and flexibility.
- v5.3.2 - December 20, 2014
- Added method to get node height more accurately.
- v5.3.1 - December 20, 2014
- Cache header height for better performance.
- v5.3.0 - December 20, 2014
- Now supports scrolling to the top with an empty hash (#).
- v5.2.2 - December 13, 2014
- Updating URL now accounts for query strings.
- v5.2.1 - December 13, 2014
- Added unit tests.
- v5.2.0 - November 21, 2014
- Add focus to scrolled to anchor if focusable.
- v5.1.4 - October 18, 2014
- Removed
.bind
dependency and polyfill. - Updated
gulpfile.js
tasks and namespacing.
- Removed
- v5.1.3 - September 29, 2014
- Fixed CommonJS module bug.
- v5.1.2 - August 31, 2014
- Fixed event listener filter to account for sub elements.
- Removed unused
event
argument fromanimateScroll
- v5.1.1 - August 21, 2014
- Passed in
event
variable toeventHandler
method, fixing Firefox bug.
- Passed in
- v5.1.0 - August 18, 2014
- Added
destroy
method. - Converted to event bubbling approach for better performance.
- Switched to Ruby Sass.
- Added
- v5.0.4 - August 15, 2014
- Added fix for UMD structure.
- v5.0.3 - August 13, 2014
- Replaced character escaping method with
CSS.escape
for more robust character escaping.
- Replaced character escaping method with
- v5.0.2 - August 12, 2014
- Added character escaping when first character in anchor ID is a number.
- v5.0.1 - August 8, 2014
- Added polyfill for
Functions.prototype.bind
. - Removed Sass paths from
gulpfile.js
.
- Added polyfill for
- v5.0.0 - July 21, 2014
- Updated
data-options
functionality to JSON. - Fixed update URL bug.
- Set update URL to
true
by default.
- Updated
- v4.8.2 - June 28, 2014
- Fixed
extend()
method.
- Fixed
- v4.8.1 - June 27, 2014
- Fixed problem with
toggles
containing a URL before the fragment identifier
- Fixed problem with
- v4.8.0 - June 21, 2014
- Converted to gulp.js workflow.
- Added unit testing.
- Added minified versions of files.
- v4.7.2 - June 19, 2014
- Fixed typo that broke scroll.
- v4.7.1 - June 19, 2014
- Fixed factory/root/UMD definition.
- v4.7.0 - June 7, 2014
- Added AMD support.
- Moved public APIs to
exports
variable. - Improved feature test.
- Replaced
Array.prototype.forEach
hack with properforEach
function. - Added a more well supported
trim
function. - General code optimizations for better minification and performance.
- Updated to JSDoc documentation (sort of).
- Updated to three number versioning system.
- Added package manager installation info.
- v4.6 - March 21, 2014
- v4.5 - March 20, 2014
- Added
offset
tooptions
- Added
- v4.4 - March 15, 2014
- v4.3 - March 5, 2014
- Added arguments to callback functions for greater versatility. 44
- v4.2 - February 27, 2014
- Fixed error for null
toggle
argument inanimateScroll
function (43).
- Fixed error for null
- v4.1 - February 27, 2014
- Converted
_defaults
to a literal object
- Converted
- v4.0 - February 21, 2014
- Better public/private method namespacing.
- Require
init()
call to run. - New API exposes additional methods for use in your own scripts.
- Better documentation.
- v3.3 - February 19, 2014
- v3.2 - February 10, 2014
- Fixes iOS infinite loop and Chrome browser zoom bugs.
- v3.1 - February 4, 2014
- Reverted to
Array.protype.foreach
loops.
- Reverted to
- v3.0 - January 28, 2014
- Switched to a data attribute for the toggle selector.
- Added namespacing to IIFE.
- Updated looping method and event listener.
- v2.19 - January 23, 2014
- v2.18 - January 23, 2014
- v2.17 - January 17, 2014
- v2.16 - January 16, 2014
- v2.15 - January 16, 2014
- v2.14 - January 15, 2014
- v2.12 - January 7, 2014
- v2.11 - January 4, 2014
- v2.10 - December 31, 2013
- v2.9 - December 9, 2013
- v2.8 - December 3, 2013
- Fixed false distance reading.
- Added linear easing as fallback when easing pattern not recognized to prevent script from failing.
- v2.7 - November 25, 2013
- Converted naming conventions back to mathmatical roots (ex.
easeInCubic
) to remain consistent with development community language.
- Converted naming conventions back to mathmatical roots (ex.
- v2.6 - November 26, 2013
- Missing character was causing certain easing functions to break.
- v2.5 - November 22, 2013
- Changed the default easing to
easeInOutNormal
.
- Changed the default easing to
- v2.4 - November 21, 2013
- Added easing support with contributions from Willem Liu and code from Gaëtan Renaudeau.
- v2.3 - August 27, 2013
- Added missing semicolons.
- Defined
animationStop
variable once, add values later. - Activated strict mode.
- Wrapped in IIFE.
- v2.2 - August 17, 2013
- Now you can set the animation speed with the
data-speed
attribute. (ex.data-speed="400"
)
- Now you can set the animation speed with the
- v2.1 - August 17, 2013
- Improvement animation function interval for smoother animation.
- Updated to allow for scrolling up the page.
- v2.0 - August 14, 2013
- Converted to vanilla JavaScript.
- Removed dependency on jQuery.
- v1.1 - June 7, 2013
- Switched to MIT license.
- v1.1 - May 18, 2013
- Added jQuery noConflict mode.
- Updated tutorial.
- v1.0 - January 24, 2013
- Initial release.