A light-weight scroll manager that uses only native javascript. Manipulates native scroll properties so that native events fire appropriately and uses browser's animation frames for fast and smooth rendering.
Many other scroller libraries use absolutely positioning, css animations, transitions and other types of workarounds directly on
the window.document
, <html>
, <body>
and other elements to "fake" a scrolling effect in order to get the scroller to behave.
While this is clever, desktop and mobile devices (mobile mainly), heavily depend on the natural scroll events of these elements to do helpful things for the user. Like hiding the location url bar as you scroll down the window of the document (on mobile browsers), for instance. Or pausing heavy processes, until the user is done performing a task as to not interrupt them, or adding inertia or natural momentum when scrolling. So it's increasingly important that the scroll logic added to these elements is done in a way that lends nicely to these use cases, which is what this Scroll class does.
- pure, native javascript
- no css transitions, animations or absolute positioning hacks
- manually scroll to any portion of a page and detect when done
- safe to use on the
document.body
element - supports easing functions when scrolling
- battery-friendly -- uses minimal amount of CPU power (no processing on inactive tabs, and hidden elements!)
- fast and smooth rendering (no choppiness)
- does not hijack native browser functionality (i.e. inertia scrolling, momentum defaults)
- native "onscroll" events can still be used (window.onscroll and Element.onscroll)
- Both non-AMD and AMD compatible
You can manually scroll any element on a page. Just make sure the element you want to scroll has:
- A specified
height
css property. - css
overflow
property that is set tohidden
. - Content that extends beyond the specified height.
You can manually scroll to any portion of an element and detect when done. This examples scrolls the window (document body).
var scroll = new Scroll({
el: document.body
});
scroll.to(0, 500).then(function () {
//scrolling down 500 pixels has completed!
});
var myElement = document.body.getElementsByClassName('my-element')[0];
var scroll = new Scroll({
el: document.body
});
scroll.toElement(myElement).then(function () {
// done scrolling to the element
});
Easing is also supported simply by passing an options object with easing.
var scroll = new Scroll({
el: document.body
});
scroll.to(0, 200, {easing: 'easeInOutCubic', duration: 500}, function () {
// scrolled down 200 pixels using the easeInOutCubic easing effect in 500 milliseconds!
});
Listen in on native scroll events the same way you would if a user was scrolling with a mouse or touch event.
var scroll = new Scroll({
el: document.body
});
window.onscroll = function () {
// scrolling!
}
scroll.to(0, 300); // scroll to trigger event