8000 GitHub - mmmurf/scroll-js: Light scroller that uses native javascript
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mmmurf/scroll-js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Scroll

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.

Why use this over other scroll libraries and plugins?

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.

Benefits

  • 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

Usage

You can manually scroll any element on a page. Just make sure the element you want to scroll has:

  1. A specified height css property.
  2. css overflow property that is set to hidden.
  3. Content that extends beyond the specified height.

Scrolling an element

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!
});

Scroll to an element

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
});

Scroll easing

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!
});

Detect scroll events

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

About

Light scroller that uses native javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.6%
  • HTML 0.4%
0