0.1.3 • Published 2 years ago

@cher-ami/smooth-scroll v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Dependancies

Uses Signal

Summary

Installation

$ npm i @cher-ami/smooth-scroll

Simple usage

Init smooth scroll

const container = querySelector("#scroll-container")
const scrollManager = new SmoothScrollManager(container)
scrollManager.init()

Declare scroll sections

<div data-scroll-section></div>

To be counted in scroll flow, each element in container must be declared

API

Members

MemberTypeDescription
progressnumberPercentage of scroll progress
scrollPositionnumberCurrent scroll position in px (lerped value)
scrollDestinationnumberScroll destination in px (raw value)
scrollLengthnumberTotal scrollable length
scrollSpeednumberCurrent speed

Methods

MethodParamsDescription
init()noneInitialise scroll
scrollToTop(speed?)speed: number = 200Scroll to position 0
scrollToBottom(speed?)speed: number = 200Scroll to max position
scrollTo(target)target: number \| HTMLDivElementScroll to destination in px or given HTML element
disable()noneDisable scroll
enable()noneEnable scroll
kill()noneCancel RAF and remove listeners
refresh()noneUpdate scroll sections and scroll length, should be called if the content of the scroll container has changed (ex: after page transitions)

React usage

Init

Wrap your app with <SmoothContainer />

export function App () {
  return (
      <div className={"root"}>
        <SmoothContainer>
          {/** ... */}
        </SmoothContainer>
      </div>
  )
}

Children will have access to scrollManager instance via ScrollContext

const scrollManager = useContext(ScrollContext)

API

Components :

Hooks :

SmoothContainer

Dispatch scrollContext to children and give them access to API

<SmoothContainer>
  {/** ... */}
</SmoothContainer>

Props :

none

useScrollPosition

Execute callback every time scroll position changes

useScrollPosition((scrollPosition: number) => {
  // Do something
}, []);

Parameters :

  • callback (scrollPositionValue:number) => void Callback function to execute each time the scroll position changes
  • dependancies any[] = [] Additional react dependancies

Returns :

nothing

useScrollSpeed

Execute callback every time scroll speed changes

useScrollSpeed((scrollSpeed: number) => {
  // Do something
}, []);

Parameters :

  • callback (scrollSpeedValue:number) => void Callback function to execute each time the scroll speed changes
  • dependancies any[] = [] Additional react dependancies

Returns :

nothing

Credits

Sam Phlix & Cher Ami