Table of Contents
Features
- Smooth scroll inside any element in any direction
- Center elements
- Extremely precise
- Handle multiple scroll animation at the time
- High performance
- Detect onScroll events and differentiate between user and utility scroll
- React to elements position changes
- Customize easing function used to animate the scroll
- Typescript support
Installation
It can be installed from npm,
$ npm install --save scroll-utility
or from a cdn at jsdelivr
<script src="https://cdn.jsdelivr.net/npm/scroll-utility@4"></script>
when downloading from a cdn the package will be globally exported as ScrollUtility
Usage
Basic Scroll
import { Scroll } from "scroll-utility"
const scrollManager = new Scroll()
scrollManager.scrollTo(50)
scrollManager.scrollTo(50, 1000)
scrollManager.scrollTo.element("#some-element")
scrollManager.scrollTo.element("#some-element", 0.5)
scrollManager.scrollTo.element("#some-element", 1, 1000)
scrollManager.scrollTo.scrollSize(0)
scrollManager.scrollTo.scrollSize(1)
scrollManager.scrollTo.scrollSize(0.5, 1000)
scrollManager.scrollTo.size(0)
scrollManager.scrollTo.size(1)
scrollManager.scrollTo.size(2, 999)
scrollManager.scrollBy(50)
scrollManager.scrollBy(50, 1000)
scrollManager.scrollBy.element("#some-element")
scrollManager.scrollBy.element("#some-element", 0.5)
scrollManager.scrollBy.element("#some-element", 1, 1000)
scrollManager.scrollBy.element("#some-element", 0)
scrollManager.scrollBy.scrollSize(0.1)
scrollManager.scrollBy.size(1)
scrollManager.scrollBy.size(-1)
scrollManager.scrollBy.size(0.5, 1000)
That's just a quick reference cheat sheet of how to use scroll-utility, if you want to learn more keep reading :)
Creating a scroll manager
import { Scroll } from "scroll-utility"
const scrollManager = new Scroll({
element: window,
horizontal: false,
onScroll: null,
duration: 0,
easing: defaultEasingFunction,
})
const scrollManagerWithOutOptions = new Scroll()
scroll wrapper
The element option when creating a 'scrollManager' indicates the element in which the scroll will take place, by default the window
const documentElement = document.querySelector("html")!
let scrollManager = new Scroll()
scrollManager = new Scroll({ element: window })
scrollManager = new Scroll({ element: "html" })
scrollManager = new Scroll({ element: documentElement })
scrollManager = new Scroll({ element: document.documentElement })
scrollManager = new Scroll({ element: "#some-element" })
scrollManager = new Scroll({ element: document.getElementById("some-element") })
direction
The horizontal option indicates the direction when scrolling, by default vertical
let scrollManager = new Scroll()
scrollManager = new Scroll({ horizontal: false })
scrollManager = new Scroll({ horizontal: true })
onScroll callback
let scrollManager = new Scroll()
scrollManager = new Scroll({
onScroll: external => {
console.log("scrolled!")
if (external) {
}
},
})
scrollManager.onScroll = () => console.log("new onScroll callback")
scrollManager.onScroll = null
default duration
The duration option indicates the default duration of the scroll animations in milliseconds, by default 0
let scrollManager = new Scroll()
scrollManager = new Scroll({ duration: 350 })
scrollManager.duration = 1000
easing
The easing option indicates the default animation of the scroll, which is by default inOutQuad
import { Scroll, defaultEasingFunction } from "scroll-utility"
let scrollManager = new Scroll()
scrollManager = new Scroll({ easing: defaultEasingFunction })
scrollManager.easing = (currentStep, offsetValue, distance, totalSteps) => {
return distance * (currentStep / totalSteps) + offsetValue
}
Here are some more easing functions
Scrolling
scrollBy
scrollBy will accept a value (the number of px to scroll down), a duration (to override the default duration), and a easing function (to override the default one).
If the value in negative it will scroll up
scrollManager.scrollBy(50)
scrollManager.scrollBy(-50)
scrollManager.scrollBy(50, 1000)
scrollManager.scrollBy(50, 1000, customEasingFunction)
scrollBy element
The 1st parameter of scrollBy.element is the element whose size will be used to scroll, the rest of parameters same as plane scrollBy
scrollManager.scrollBy.element("#some-element")
scrollManager.scrollBy.element("#some-element", 0.5)
scrollManager.scrollBy.element("#some-element", -1, 1000)
scrollManager.scrollBy.element("#some-element", 1, 1000, customEasingFunction)
scrollBy size
Here the size is the size of the scroll container, and the value passed is a modifier, been 1 the full size, 0.5 half, and a negative value will mean the scroll will be up instead of down (or left instead of right)
scrollManager.scrollBy.size(1)
scrollManager.scrollBy.size(-1)
scrollManager.scrollBy.size(0.5, 1000)
See size
scrollBy scrollSize
scrollManager.scrollBy.scrollSize(0.1)
See scrollSize
scrollTo
scrollTo will accept a value (the position to scroll to), a duration (to override the default duration), and a easing function (to override the default one).
scrollManager.scrollTo(50)
scrollManager.scrollTo(50, 1000)
scrollTo element
The 1st parameter of scrollTo.element is the element whose position will be used to scroll, the rest of parameters same as plane scrollTo
For the value used to center the element, it matches the same criteria used in getRelativeElementPosition
scrollManager.scrollTo.element("#some-element")
scrollManager.scrollTo.element("#some-element", 0.5)
scrollManager.scrollTo.element("#some-element", 1, 1000)
scrollTo size
Pretty much the same as scrollBy.size, except it scrolls to instead of by.
scrollManager.scrollTo.size(0)
scrollManager.scrollTo.size(1)
scrollManager.scrollTo.size(2, 999)
See size
scrollTo scrollSize
Same as scrollBy.scrollSize, except it scrolls to instead of by.
scrollManager.scrollTo.scrollSize(0)
scrollManager.scrollTo.scrollSize(1)
scrollManager.scrollTo.scrollSize(0.5, 1000)
See scrollSize
Computed values
scrollPosition
const scrollManager = new Scroll()
scrollManager.scrollPosition
size
const scrollManager = new Scroll()
scrollManager.size
scrollSize
const scrollManager = new Scroll()
scrollManager.scrollSize
relativeElementPosition
const relativePosition = new Scroll().getRelativeElementPosition("#some-elemet")
if (relativePosition < -1) {
}
if (relativePosition > -1 && relativePosition < 0) {
}
if (relativePosition > 0 && relativePosition < 1) {
if (relativePosition === 0.5) {
}
}
if (relativePosition > 1 && relativePosition < 2) {
}
if (relativePosition > 2) {
}
Browser Compatibility
Test are made using automate testing with Browserstack for open source.
Why?
There are a lot of packages about smooth scrolling, so, what's the difference?
Well, the main idea is to be able to stack multiple scroll animations together, and with high precision. That is not an extra feature, that's what this package does, you can trigger multiple animations to several places, and it will be as precise as it can be.
License
MIT
Support
This project is free and open-source, so if you think this project can help you or anyone else, you should star it in github
Also feel free to open an issue if you have any idea, question, or you've found a bug. Any feedback is good support