
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
svelte-scrollto-element
Advanced tools
Svelte action that listens for click events and scrolls to elements with animation. Inspired by rigor789/vue-scrollto.
Animating vertical and horizontal scrolling (for Svelte)
- TypeScript / JavaScript
- Useful options
- Compatible with
SvelteKit,SSRandadapter-static
npm install svelte-scrollto-element --save
# yarn add svelte-scrollto-element
<script>
import { animateScroll } from 'svelte-scrollto-element';
</script>
<a on:click={() => animateScroll.scrollToBottom()}> Scroll to bottom </a>
<a on:click={() => animateScroll.scrollToTop()}> Scroll to top </a>
<a on:click={() => animateScroll.scrollTo({element: '#my-anchor'})}> Scroll to element with id="my-anchor" </a>
<a on:click={() => animateScroll.scrollTo({element: '.my-element-class', offset: 200})}> Scroll to below element 200px </a>
<a on:click={() => animateScroll.scrollTo({element: 'footer', duration: 2000})}> Scroll to footer element over 2000ms </a>
<script>
import { scrollto } from 'svelte-scrollto-element';
</script>
<!-- Parameter is element selector or options -->
<a use:scrollto={'#scroll-element'}> Scroll to element </a>
| Option | Required | Default value | Description |
|---|---|---|---|
container | âś” | "body" | Scroll container |
duration | âś” | 500 | The duration (in milliseconds) of the scrolling animation. |
delay | 0 | ||
offset | 0 | The offset that should be applied when scrolling. This option accepts a callback function | |
easing | cubicInOut | The easing function to be used when animating. Can pass any easing from svelte/easing or pass custom easing function. | |
onStart | noop | A callback function that should be called when scrolling has started. Receives the target element and page offset as a parameter. | |
onDone | noop | A callback function that should be called when scrolling has ended. Receives the target element and page offset as a parameter. | |
onAborting | noop | A callback function that should be called when scrolling has been aborted by the user (user scrolled, clicked etc.). Receives the target element and page offset as parameters. | |
scrollX | false | Whether or not we want scrolling on the x axis | |
scrollY | true | Whether or not we want scrolling on the y axis |
Override global options:
import { animateScroll } from 'svelte-scrollto-element';
animateScroll.setGlobalOptions({
offset: 200,
onStart: (element, offset) => {
if (element) {
console.log('Start scrolling to element:', element);
} else if (offset) {
console.log('Start scrolling to page offset: (${offset.x}, ${offset.y})');
}
}
});
scrollTo(options)Accepts all global options and:
element: The element you want scroll tox: The offset we want to scrolling on the x axisy: The offset we want to scrolling on the y axisscrollToTop(options)Shortcut of use scrollTo with x equal to 0.
scrollToBottom(options)Shortcut of use scrollTo with x equal to containerHeight
Svelte action that listens for click (touchstart) events and scrolls to elements with animation.
scrollto
scrolltotop
scrolltobottom
If you want to use Lazy and want to scroll to elements, you need to give your lazy components (images, v.v..) fixed dimensions, so the browser known the size of the not loaded elements before scrolling.
Initially forked from langbamit/svelte-scrollto which is archived now. Uses some of its code and functionality!
I DO NOT STORE ANY DATA. PERIOD.
I physically can't. I have nowhere to store it. I don't even have a server database to store it. So even if Justin Bieber asked nicely to see your data, I wouldn't have anything to show him.
That's why, with this library, what happens on your device stays on your device till disappear.
Initially forked from langbamit/svelte-scrollto which is archived now.
Proudly powered by nature 🗻, wind 💨, tea 🍵 and beer 🍺 ;)
All contents are licensed under the MIT license.
FAQs
Svelte action that listens for click events and scrolls to elements with animation. Inspired by rigor789/vue-scrollto.
We found that svelte-scrollto-element demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.