1.1.1 • Published 1 year ago

af-scroll v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

AFScroll (Animation Frame Scroll)

The script aligns page scroll update with animation frame.
It allows to perform js based DOM manipulation related to page scroll just in time, not too late.

Demo

Menu

Quick start

Install

# install af-scroll npm package
npm install --save af-scroll

Initialization

// import create function
import createAFScroll from 'af-scroll';

// call to initialize
createAFScroll();

Imports

The library build is delivered in two formats: esmodule and commonjs

// default import (esmodule)
import createAFScroll from 'af-scroll';

// explicit import (esmodule)
import createAFScroll from 'af-scroll/dist/esmodule';

// explicit import (commonjs)
const createAFScroll = require('af-scroll/dist/commonjs');

Features

  • alignment of js on scroll updates with the visual scroll update
  • on scroll updates throttled by requestAnimationFrame
  • native scroll triggers (native scrollbar, mouse wheel, keyboard, touch)
  • scroll smoothing (optional)
  • scroll locking (optional)

Disadvantages

  • additional wrapping element in DOM
  • missing support for native scroll to results of find on page

Why you may need AFScroll?

If you want to manipulate DOM elements by js and set them based on the scroll position.

What happens when you use default scroll:

  • user scrolls a page
  • browser renders new scroll position
  • user sees new scroll position
  • the native on scroll event is triggered
  • js updates for current scroll position
    (any custom DOM manipulations by js)
  • user sees DOM elements updated by js

What happens when you use AFScroll:

  • user scrolls a page
  • browser renders new scroll position
  • user not sees new scroll position
  • the native on scroll event is triggered
  • js updates for current scroll position
    (update visible scroll position & any custom DOM manipulations by js)
  • user sees new scroll position & DOM elements updated by js in the same time

Options

createAFScroll({
  smoothForce: 0.8,
  smoothLimit: 0.2,
  scrollEl: null,
  className: 'afScroll',
  wrapExclude: 'script, link',
  autoHeight: 12,
  onUpdate: () => {},
  onComplete: () => {},
});
OptionTypeDefaultDescription
smoothForceNumber0.8smoothing force in range 0.0 - 1.00: no smooth1: no scroll
smoothLimitNumber0.2min diff between current and target value to keep smooth loop
scrollElElementnullstatic scroll wrapper element1
classNameString"afScroll"css class name of scroll wrapper element2
wrapExcludeString"script, link"css selector to exclude from wrapping2
autoHeightNumber12height checking period in frames1: each frame0: disabled12: once per each 12 frames
onUpdateFunction() => {}callback function triggered on scroll update
onCompleteFunction() => {}callback function triggered after smooth loop stopped
  1. prevents DOM modification by auto created wrapper, passed scrollEl will be used instead of it
  2. ignored if scrollEl passed

Methods

// example of using methods
const afScroll = createAFScroll(/* optional options */);
afScroll.scrollTo(0); // scroll to top
afScroll.scrollTo(0, true); // scroll to top forced
afScroll.free(); // free forced scroll
afScroll.lock(); // lock scroll
afScroll.unlock(); // unlock scroll
afScroll.destroy(); // destroy af-scroll
afScroll.init(); // reinit af-scroll
MethodArgumentsReturnDescription
init()noneundefinedreinitialize the script after destroy
scrollTo(scroll, force)scroll {Number} target scroll value in px unitforce {Boolean} prevents user from changing a scroll target until it completesundefinedscroll to scroll value
free()noneundefinedfree forced scroll
lock()noneundefinedlock scroll
unlock()noneundefinedunlock scroll
destroy()noneundefineddestroy the script and restore original html

Events

createAFScroll({
  onUpdate: (scroll) => {
    // custom behavior
  },
  onComplete: (scroll) => {
    // custom behavior
  },
});
EventArgumentsWhen triggered
onUpdate(scroll)scroll {Number} scroll value in px unitin each animation frame of move to target scroll
onComplete(scroll)scroll {Number} scroll value in px unitafter achieve target scroll - smooth loop stopped
1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago