0.1.2 β€’ Published 1 year ago

@studio206/fortis v0.1.2

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

LENIS

Introduction

🚧 Still in WIP, API might change with new releases 🚧

This is our take on smooth scroll, lightweight, hard working, smooth as butter scroll. See Demo

Features

  • Performant
  • Lightweight (~2Kb gzipped)
  • Run scroll in main thread
  • Accessibility (CMD+F page search, keyboard navigation, keep scroll position on page refresh, etc.)
  • External RAF
  • SSR proof
  • Not opinionated
  • Tree-shakeable
  • Custom scroll easing/duration
FeatureLocomotive-scrollGSAP ScrollSmootherLenis
Native scrollbarβŒβœ…βœ…
Native scroll inputsβŒβœ…βŒ
Normalize scroll experienceβœ…βŒβœ…
AccessibilityβŒβŒβœ…
CSS StickyβŒβŒβœ…
IntsersectionObserverβŒβŒβœ…
Open sourceβœ…βŒβœ…
Built-in animation systemβœ…βœ…βŒ
Size (gzip)12.33KB26.08KB2.13kb

Installing

using package manager:

$ npm i @studio-freight/lenis

using scripts:

<script src="https://cdn.jsdelivr.net/gh/studio-freight/lenis@latest/bundled/lenis.js"></script>

Setup

Basic setup

import Lenis from '@studio-freight/lenis'

const lenis = new Lenis({
  duration: 1.2,
  easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), // https://www.desmos.com/calculator/brs54l4xou
  direction: 'vertical', // vertical, horizontal
  gestureDirection: 'vertical', // vertical, horizontal, both
  smooth: true,
  mouseMultiplier: 1,
  smoothTouch: false,
  touchMultiplier: 2,
  infinite: false,
})

//get scroll value
lenis.on('scroll', ({ scroll, limit, velocity, direction, progress }) => {
  console.log({ scroll, limit, velocity, direction, progress })
})

function raf(time) {
  lenis.raf(time)
  requestAnimationFrame(raf)
}

requestAnimationFrame(raf)

Using custom scroll container

const lenis = new Lenis({
  wrapper: NodeElement, // element which has overflow
  content: NodeElement, // usually wrapper's direct child
})

Instance settings

OptionTypeDefaultDescription
wrapperNodeElementwindowDefault element which has overflow
contentNodeElementdocumentwrapper's direct child
durationnumber1.2Specifies the duration of the animation
easingfunction(t) => Math.min(1, 1.001 - Math.pow(2, -10 * t))Specifies the rate of change of a specific value, our default is custom but you can pick one from Easings.net
directionstringverticalvertical or horizontal scrolling.
gestureDirectionstringverticalvertical, horizontal or both.
smoothbooleantrueEnable or disable 'smoothness'
mouseMultipliernumber1This value is passed directly to Virtual Scroll
smoothTouchbooleanfalseEnable or disable 'smoothness' while scrolling using touch. Note: We have disabled it by default because touch devices native smoothness is impossible to mimic
touchMultipliernumberstringThis value is passed directly to Virtual Scroll
infinitebooleanfalseEnable infinite scrolling!

Methods

  • raf(time) : must be called every frame for internal function.
  • scrollTo(target, {offset, duration, easing, immediate}) : scroll to a target.
    • target : can be Number, NodeElement or String (CSS selector).
    • offset : Number equivalent to scroll-padding-top.
    • duration : Number scroll duration in seconds.
    • easing : Function.
    • immediate : ignore duration and easing.
  • on(id, callback) : execute a function on event.
    • id : event to listen.
      • scroll : return scroll position.
    • callback(e) : function to execute.
  • stop() : pause the scroll
  • start() : resume the scroll
  • destroy() : destroy the instance, remove all events.

Considerations

Things to consider if you want to add Lenis to your codebase will be listed here.

Make sure scroll-behavior is set to initial or not set at all (thanks @thagxt)

html {
  scroll-behavior: initial;
}

Keep html and body elements default sized (see this issue)

html,
body {
  width: 100%;
  min-height: 100%;
}

Stop wheel event propagation on elements with overflow (see this issue)

<div onwheel="event.stopPropagation()">scroll content</div>

Manually use lenis.scrollTo('#anchor') on anchor link click (see this issue)

<a href="#anchor" onclick="lenis.scrollTo('#anchor')">scroll to anchor</a>

Plugins

Lenis in use

Authors

This set of hooks is curated and maintained by the Studio Freight Darkroom team:

License

The MIT License.

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

2 years ago