Skip to content

Repository files navigation

DynamicHeader

Change header based on scroll, sections and checkpoints.

Get Started

You can download the file and use the script from the dist folder or include the files from CDN.

https://cdn.jsdelivr.net/gh/JonasStumpf/DynamicHeader@main/dist/DynamicHeader.esm.js
https://cdn.jsdelivr.net/gh/JonasStumpf/DynamicHeader@main/dist/DynamicHeader.iife.js

Import DynamicHeader and the components you need.

ESM

import{DynamicHeader,Scroll,Sections,Checkpoint,Component}from"path/to/DynamicHeader.esm.js";

IIFE

<scriptsrc="/dist/DynamicHeader.iife.js"></script><script>const{DynamicHeader, Scroll, Sections, Checkpoint, Component}=DH;</script>

Initialize DynamicHeader with options and components
DynamicHeader(options, components)

Example:

constdh=newDynamicHeader({header: document.querySelector("header"),//header: "header",Checkpoint: {checkpoints: ["top"]}},[Scroll,Checkpoint]);

DynamicHeader

DynamicHeader provides the base functionalities and is extended by components.
Components included
Create your own components

Options

optionparameterdefaultexplanation
headerstring / element"header"header element or selector string
targetEventsbooleanfalseadditionally dispatch events on elements (header, checkpoints, ...)
prefixstring"dh"prefix used for various things like datasets or ids
resizeDelaynumber500time between header resize triggers in ms

Event

resize

event data:

  • header element

triggered on header resize (if detectResize() was executed)

Events

Add event callbacks to DH:

dh.on("event",(data)=>{console.log(data)});

Add EventListeners on elements (enable targetEvents and add the prefix):

document.querySelector("header").addEventListener("dh-event",(event)=>{console.log(event)});

Components

Add components on DH instantiation with the second parameter:

newDynamicHeader(options,[Component1,Component2, ...])

or later with the mount() method:

dh.mount([Component1,Component2, ...]);

Options

Add Component options in the DH options with the Components name as the key:

newDynamicHeader({header: "#header",Scroll: {delay: 50,classUp: "show",classDown: "hide",}},[Scroll]);

Scroll

Sets classes based on the scroll direction.
Modes for the scroll observer to trigger:

  • start: start of scrolling
  • mid: between start and end
  • end: end of scrolling
  • scroll: always

Options

optionparameterdefaultexplanation
rootelement/windowwindowscrolling element
delaytime250scroll event throttle
classUpstring"scroll-up"class set when scrolling up
classDownstring"scroll-down"class set when scrolling down
modesArray[string]["scroll"]scroll modes to observe

Event

scroll

event data:

  • position: scroll y position
  • direction: -1 (up) / 1 (down)
  • state: start, mid, end

triggered on scroll

Checkpoint

Sets classes based on specified checkpoints in viewport.

Options

optionparameterdefaultexplanation
obsRootelementnullintersection observer root
obsMarginstring (css format)"0px"observer margin
obsThresholdnumber0observer threshold
checkpointsArray[object/string][]checkpoints to create/observe
cpClassstring"checkpoint"class set on checkpoints
dataNamestring"checkpoint"dataset name on checkpoints

option checkpoint:

when defining checkpoints you can set 3 parameters:

  • pos: string in css format that positions the checkpoint
  • class: class set on header when checkpoint is in viewport
  • id: id on the checkpoint element

there are 3 presets:

top: {pos: "top:20px",class: "dh-top",id: "dh-checkpoint-top"},bot: {pos: "bottom:20px",class: "dh-bot",id: "dh-checkpoint-bot"},mid: {pos: "top:50%",class: "dh-mid",id: "dh-checkpoint-mid"}

presets can be used by passing the preset name as a string in the checkpoints array

Event

checkpoint

event data:

  • checkpoint: checkpoint element
  • intersecting: boolean, is checkpoint in viewport
  • class: checkpoint class to set on header

triggered on checkpoint enter/leave

Sections

Sets classes based on section header is above => based on background.

Options

optionparameterdefaultexplanation
sectionsstring/elements"section"sections to observe
obsRootelementnullintersection observer root
dataNamestring"section"dataset name on sections that specify class to set

Event

section

event data:

  • section: section changed to
  • removed: class that was removed

triggered on section change

Create your own components

When DH mounts Components it first calls mount(), this sets the options and the dh instance. You generally don't want to override this method.

After that it calls init(), override this method to do setup stuff for your component.

When destroy() is called on DH it calls destroy() on every component, override this method if you want/need to remove stuff setup by your component.
Classes added with DH.addClass() are automatically removed by DH when destroyed.

DH manages components by their classnames, it uses the property #name and the method getName().
Don't override these.

Example

Import Component:

import{DynamicHeader,Component}from"path/to/DynamicHeader.esm.js";//esmconst{ DynamicHeader, Component }=DH;//iife

Create a class for your Component and extend it to Component:

classTestComponentextendsComponent{options={testClass: "default-test-component-class"}init(){this.dh.addClass(this.options.testClass);}}

Add it to DH:

constdh=newDynamicHeader({TestComponent: {testClass: "test-component-class"}},[TestComponent]);

About

Change header based on scroll, sections and checkpoints.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages