Coder Social home page Coder Social logo

acusti / onscrolling Goto Github PK

View Code? Open in Web Editor NEW
29.0 3.0 2.0 291 KB

A better, smoother, more performant window.onscroll event interface using requestAnimationFrame for performance and mobile-compatibility

License: Creative Commons Zero v1.0 Universal

JavaScript 34.66% TypeScript 65.34%

onscrolling's Introduction

Jank-free onscrolling  build workflow

A better, smoother, more performant onscroll event interface based on the concepts from this html5rocks tutorial. It uses requestAnimationFrame plus debouncing for performance and mobile-compatibility (thanks to the touchmove event), giving you a fighting chance to achieve the hallowed 60fps of lore with your scroll-listening UI.

NPM

Usage

The module is ESM-only and exports a single default onscrolling function:

onscrolling( listener )

listener function (payload: { scrollX: number; scrollY: number }) => void

The function to call on a scroll event with a { scrollX: number; scrollY: number } payload object. In this default version, the module will only invoke the listener when the page has been scrolled vertically.

onscrolling( listener, options )

listener function (payload: { scrollX: number; scrollY: number }) => void

The function to call on a scroll event with a { scrollX: number; scrollY: number } payload object. The listener is invoked when the page is scrolled in any of the direction specified in the options object (only once per event).

options object { horizontal?: boolean; vertical?: boolean; x?: boolean; y?: boolean }

The scroll axis or axes to monitor. x is an alias for horizontal, and y is an alias for vertical. If neither horizontal nor vertical are true, vertical is used as the default. To listen for any scroll event in any direction, set both horizontal and vertical to true.

onscrolling return value

onscrolling function (listener: Listener, options?: Options) => () => void

The onscrolling function returns a cleanup function that takes no arguments and is used to remove the passed-in scroll event listener.

Dependencies

None.

Compatibility

Out of the box, onscrolling uses requestAnimationFrame, which is only available in IE10+. For older browsers, your scroll watchers simply won’t run. To add compatibility for those browsers, just include a requestAnimationFrame polyfill.

Tests

Tests use vitest + happy-dom, and can be run with yarn test.

TODO

  • Add optional param to specify an object other than window to monitor for scroll events
  • Expose measure and mutate functions to attach handlers specifically to the measuring (read) or mutating (write) portion of each cycle to minimize layout calculations

Misc

Note: This package was formerly known as jank-free-onscroll

onscrolling's People

Contributors

acusti avatar dependabot[bot] avatar

Stargazers

 avatar Jacob Beltran avatar Kurt Rank avatar Luke Cavanagh avatar Adam Misiorny avatar  avatar  avatar Florent Bourgeois avatar David Authier avatar Luís Rodrigues avatar Denis Radin avatar ● filipe avatar diéssica avatar etpinard avatar Rafael Rinaldi avatar Dmitry Iv. avatar Eric Bower avatar Anatoli Nicolae avatar Simon Kusterer avatar  avatar Matt Mueller avatar Jason Miller avatar Miguel Ángel Durán avatar Ilya Medvedev avatar Lesley avatar AJ avatar Steven Vachon avatar  avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

onscrolling's Issues

Firefox?

It's not working in Firefox (desktop) for me. Please provide a demo for quick testing to rule out the library as the cause of the error.

1.0.0

I doubt this is beta anymore.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.