Enhancement
Scrollhide
Hide an element while the page scrolls down and show it again on scrolling up
- Import
@parallelogram-js/core/components/Scrollhide- Selector
[data-scrollhide]
Playground
Site header
Scroll the page down and up to see the header hide and return.
<header class="demo-header" data-scrollhide data-scrollhide-scroll-threshold="50">
<strong>Harbourside</strong>
<a href="#book">Book a table</a>
</header>
Use the example to see the events it sends.
About
The target gets the scrolled class while hidden, and the overlay class once the page has scrolled past the overlay threshold. Both start from the scroll position the page already has when the component mounts. Movements smaller than the tolerance are ignored, which avoids flicker from elastic scrolling, and a hidden target is shown again when keyboard focus moves into it. Your stylesheet decides what the classes look like.
Usage
Register the component and the framework loads it the first time a page contains [data-scrollhide].
import { Parallelogram } from '@parallelogram-js/core';
const app = Parallelogram.create();
app.components.add('[data-scrollhide]', () => import('@parallelogram-js/core/components/Scrollhide'));
app.run();
The framework is optional. Scrollhide.enhanceAll() mounts the component on every matching element by itself, for a page that wants one enhancement rather than a framework:
import Scrollhide from '@parallelogram-js/core/components/Scrollhide';
Scrollhide.enhanceAll();
It takes a selector and the component's options, and returns the instance it mounted them with. What it leaves out is what the framework adds: loading a component only on the pages that use it, mounting again after a page swap, and ordering with dependsOn.
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
data-scrollhide | flag | none | Marks the element |
data-scrollhide-target | selector | none | Another element to hide instead of this one |
data-scrollhide-scroll-threshold | number | 50 | Pixels scrolled before the target can hide For every instance, Scrollhide.defaults.scrollThreshold. |
data-scrollhide-overlay-threshold | number | 100 | Pixels scrolled before the overlay class is added For every instance, Scrollhide.defaults.overlayThreshold. |
data-scrollhide-tolerance | number | 5 | Smallest movement in pixels that hides or shows the target For every instance, Scrollhide.defaults.tolerance. |
data-scrollhide-scrolled-class | string | scrollhide | Class on the target while it is hidden For every instance, Scrollhide.defaults.scrolledClass. |
data-scrollhide-overlay-class | string | scrolloverlay | Class on the target past the overlay threshold For every instance, Scrollhide.defaults.overlayClass. |
data-scrollhide-enhancedset by the component | flag | none | Set once the element is watched |
Events
| Name | Detail | Description |
|---|---|---|
scrollhide:hidden | { target: HTMLElement; scrollY: number; reason: string; timestamp: number } | The target was hidden DOM event and event bus |
scrollhide:shown | { target: HTMLElement; scrollY: number; reason: string; timestamp: number } | The target was shown again; reason is scroll-up, top, focus or manual DOM event and event bus |
scrollhide:overlay-added | { target: HTMLElement; scrollY: number; timestamp: number } | The page scrolled past the overlay threshold DOM event and event bus |
scrollhide:overlay-removed | { target: HTMLElement; scrollY: number; timestamp: number } | The page scrolled back above the overlay threshold DOM event and event bus |
scrollhide:mount | { element: HTMLElement; target: HTMLElement; scrollThreshold: number; overlayThreshold: number; timestamp: number } | The element was set up Event bus |