Animate Scrolling To Anchor Links – scrollToSmooth
Works with sticky navigation bar. Perfect for site navigation on the long web page.
To learn more about Smooth Scroll, check out our 10 Best Smooth Scroll JavaScript Plugins.
1. Install and import the scrollToSmooth as an ES module.
# NPM $ npm i scrolltosmooth
import {
scrollToSmooth,
HorizontalScrollPlugin,
SnapPlugin,
TouchMomentumPlugin,
easeOutCubic
} from 'scrolltosmooth'; 2. Or load the main JavaScript scrolltosmooth.js right before the closing body tag.
<!-- With all easings and plugins --> <script src="/path/to/dist/scrolltosmooth.pkgd.min.js"></script> <!-- With linear easing only --> <script src="/path/to/dist/scrolltosmooth.min.js"></script>
3. Create anchor links pointing to related positions of the webpage.
<nav> <a href="#section-1">Section 1</a> <a href="#section-2">Section 2</a> <a href="#section-3">Section 3</a> <a href="#section-4">Section 4</a> <a href="#section-5">Section 5</a> </nav>
4. Initialize the scrollToSmooth library on the anchor links and done.
let smoothScroll = new scrollToSmooth('a', {
// options here
});
smoothScroll.init(); 5. Set the duration of the animation.
let smoothScroll = new scrollToSmooth('a', {
// duration in ms
duration: 400,
// adjust the scroll animation duration by the amount of pixels to scroll
durationRelative: false,
// minimum amount of milliseconds to perform the animation when using a relative duration
durationMin: null,
// max amount of milliseconds to perform the animation when using a relative duration
durationMax: null,
}); 6. Apply an easing function to smooth scrolling. Available easing functions:
let smoothScroll = new scrollToSmooth('a',{
easing: "linear"
}) 7. Set the offset in pixels or specify an element (typically a fixed header) to calculate the final position of the scrolling animation.
let smoothScroll = new scrollToSmooth('a', {
offset: '#fixed-header'
}); 8. Customize the attribute to determine the target element. Default: ‘href’.
let smoothScroll = new scrollToSmooth('a', {
targetAttributet: '#target'
}); <span data-scrollto="#target">Scroll to Section 1<span>
9. Callback functions.
let smoothScroll = new scrollToSmooth('a',{
onScrollStart: (data) => { },
onScrollUpdate: (data) => { },
onScrollEnd: (data) => { },
}) 10. API methods.
// scroll to a specific element
smoothScroll.scrollTo('.your-selector');
// scroll to a point or a specific element
smoothScroll.scrollTo(50);
// scroll by a fixed amount of pixels
smoothScroll.scrollBy(150);
// cancel the animation
smoothScroll.cancelScroll();
// update options
smoothScroll.update({
// options here
});
// destroy
smoothScroll.destroy(); v4.0.0/1 (03/22/2026)
v3.0.2 (03/06/2026)
v3.0.1 (03/13/2021)
v3.0.0 (03/09/2021)
v2.2.1 (08/01/2020)
v2.2.0 (07/25/2020)
v2.1.5 (05/16/2020)
The post Animate Scrolling To Anchor Links – scrollToSmooth appeared first on CSS Script.
HADLEY — A 75,000-square-foot cap on the size of retail businesses, put in place 20…
AMHERST — Representatives from the union for Amherst Department of Public Works employees say their…
The post Photos: A sweet haul appeared first on Daily Hampshire Gazette.
rangeSlider is a pure Vanilla JavaScript library that converts regular Html5 range inputs into responsive,…
Screamer isn’t subtle. Screamer is neon-soaked, maximum volume arcade racing that requires both the finesse…
This website uses cookies.