@brybrant/fade-scroll
v1.0.1
Published
Fade Scroll is a cosmetic module which adds subtle gradient masks to the overflow of scrollable content.
Maintainers
Readme
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-scrollSetup
<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:
element— RequiredHTMLElement or querySelector string (this will be the
contentof the Fade Scroller)
▪ Type:HTMLElement|Stringoptions— OptionalFade Scroller options object
▪ Type:Object
...and returns a Fade Scroller:
Fade Scroller Properties:
contentThe element selected in the first argument of the constructor function
▪ Type:HTMLElement
▪ Access:ReadscrollBarThe element with overflow (contains
contentelement)
▪ Type:HTMLDivElement
▪ Access:ReadwrapperThe outer element (contains
scrollBarelement)
▪ Type:HTMLDivElement
▪ Access:ReadcontentSizeThe size of the
contentelement (in pixels)
▪ Type:Number
▪ Access:Read| Horizontal | Vertical | |------------|----------| |offsetWidth|offsetHeight|wrapperSizeThe size of the
wrapperelement (in pixels)
▪ Type:Number
▪ Access:Read| Horizontal | Vertical | |------------|----------| |offsetWidth|offsetHeight|overflowSizeThe size of the overflow (
contentSizeminuswrapperSize)
▪ Type:Number
▪ Access:ReadscrollPositionThe scroll position of the
scrollBarelement (in pixels)
▪ Type:Number
▪ Access:Read / Write| Horizontal | Vertical | |------------|----------| |scrollLeft|scrollTop|optionsThe Fade Scroller options:
▪ Type:ObjecthideScrollbar
Hide the scrollbar?
▪ Type:Boolean
▪ Default:false
▪ Access:Read / WritecaptureWheel(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
scrollEventListener to thescrollBarelementremoveScrollListener( callback: EventListener )Remove a
scrollEventListener from thescrollBarelement
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...