@brybrant/fade-scroll
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Fade Scroll

Fade Scroll is a cosmetic module which adds subtle gradient masks to the overflow of scrollable content.

See the demo page for an interactive demonstration.

Install

$ npm i @brybrant/fade-scroll

Setup

<html>
  <head>
    <link rel='stylesheet' href='fade-scroll.css'/>
  </head>
  <body>
    <div id='horizontal'>
      <p>Some horizontal overflowing content...</p>
    </div>

    <div id='vertical'>
      <p>Some vertical overflowing content...</p>
    </div>

    <script type='module' src='index.js'></script>
  </body>
</html>

See fade-scroll.css for the Fade Scroll styles.

// index.js
import * as FadeScroll from '@brybrant/fade-scroll';

// Basic usage
const horizontal = new FadeScroll.Horizontal('#horizontal').mount();

// Set options
const vertical = new FadeScroll.Vertical('#vertical', {
  hideScrollbar: true,
}).mount();

// Change options
horizontal.options.captureWheel = true;
vertical.options.hideScrollbar = true;

// Destroy
horizontal.destroy();
vertical.destroy();

API

The constructor function takes two arguments:

  1. elementRequired

    HTMLElement or querySelector string (this will be the content of the Fade Scroller)
    ▪ Type: HTMLElement | String

  2. optionsOptional

    Fade Scroller options object
    ▪ Type: Object

...and returns a Fade Scroller:

Fade Scroller Properties:

  • content

    The element selected in the first argument of the constructor function
    ▪ Type: HTMLElement
    ▪ Access: Read

  • scrollBar

    The element with overflow (contains content element)
    ▪ Type: HTMLDivElement
    ▪ Access: Read

  • wrapper

    The outer element (contains scrollBar element)
    ▪ Type: HTMLDivElement
    ▪ Access: Read

  • contentSize

    The size of the content element (in pixels)
    ▪ Type: Number
    ▪ Access: Read

    Horizontal Vertical
    offsetWidth offsetHeight
  • wrapperSize

    The size of the wrapper element (in pixels)
    ▪ Type: Number
    ▪ Access: Read

    Horizontal Vertical
    offsetWidth offsetHeight
  • overflowSize

    The size of the overflow (contentSize minus wrapperSize)
    ▪ Type: Number
    ▪ Access: Read

  • scrollPosition

    The scroll position of the scrollBar element (in pixels)
    ▪ Type: Number
    ▪ Access: Read / Write

    Horizontal Vertical
    scrollLeft scrollTop
  • options

    The Fade Scroller options:
    ▪ Type: Object

    • hideScrollbar
      Hide the scrollbar?
      ▪ Type: Boolean
      ▪ Default: false
      ▪ Access: Read / Write

    • captureWheel (Horizontal only)
      Capture wheel events and translate to horizontal scroll movement?
      ▪ Type: Boolean
      ▪ Default: false
      ▪ Access: Read / Write

Fade Scroller Methods:

  • mount()

    Starts observing the Fade Scroller elements to apply the correct classes when the sizes change

  • destroy()

    Stops observing the Fade Scroller elements and removes built-in event listeners and styles

  • addScrollListener( callback: EventListener )

    Add a scroll EventListener to the scrollBar element

  • removeScrollListener( callback: EventListener )

    Remove a scroll EventListener from the scrollBar element


Browser Compatibility

Fade Scroll uses CSS masks to blend seamlessly with any background, however the CSS can be changed to use linear gradients for better compatibility if the background is a solid color.

Fade Scroll uses the Resize Observer API to apply the correct styles when the elements sizes change. You can Ponyfill for unsupporting browsers by using the setResizeObserver function:

import { ResizeObserver as Polyfill } from '@juggle/resize-observer';

import * as FadeScroll from '@brybrant/fade-scroll';

FadeScroll.setResizeObserver(Polyfill);

// Create some Fade Scrollers after setting the polyfill...

Package Sidebar

Install

npm i @brybrant/fade-scroll

Weekly Downloads

104

Version

1.0.1

License

GPL-3.0-only

Unpacked Size

59.6 kB

Total Files

7

Last publish

Collaborators

  • brybrant