1.0.45 • Published 11 days ago

lenis v1.0.45

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

LENIS

npm downloads size

Introduction

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

Installation

JavaScript

using a package manager:

npm i lenis
import Lenis from 'lenis'

using scripts:

<script src="https://unpkg.com/lenis@1.0.45/dist/lenis.min.js"></script> 

Setup

Basic:

const lenis = new Lenis()

lenis.on('scroll', (e) => {
  console.log(e)
})

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

requestAnimationFrame(raf)

GSAP ScrollTrigger:

const lenis = new Lenis()

lenis.on('scroll', (e) => {
  console.log(e)
})

lenis.on('scroll', ScrollTrigger.update)

gsap.ticker.add((time)=>{
  lenis.raf(time * 1000)
})

gsap.ticker.lagSmoothing(0)

React:

See documentation for lenis/react.

Instance settings

OptionTypeDefaultDescription
wrapperHTMLElement, WindowwindowThe element that will be used as the scroll container
contentHTMLElementdocument.documentElementThe element that contains the content that will be scrolled, usually wrapper's direct child
eventsTargetHTMLElement, WindowwrapperThe element that will listen to wheel and touch events
lerpnumber0.1Linear interpolation (lerp) intensity (between 0 and 1)
durationnumber1.2The duration of scroll animation (in seconds). Useless if lerp defined
easingfunction(t) => Math.min(1, 1.001 - Math.pow(2, -10 * t))The easing function to use for the scroll animation, our default is custom but you can pick one from Easings.net. Useless if lerp defined
orientationstringverticalThe orientation of the scrolling. Can be vertical or horizontal
gestureOrientationstringverticalThe orientation of the gestures. Can be vertical, horizontal or both
smoothWheelbooleantrueWhether or not to enable smooth scrolling for mouse wheel events
syncTouchbooleanfalseMimic touch device scroll while allowing scroll sync (can be unstable on iOS<16)
syncTouchLerpnumber0.075Lerp applied during syncTouch inertia
touchInertiaMultipliernumber35Manage the the strength of syncTouch inertia
wheelMultipliernumber1The multiplier to use for mouse wheel events
touchMultipliernumber1The multiplier to use for touch events
normalizeWheelbooleanfalseNormalize wheel inputs across browsers (not reliable atm)
infinitebooleanfalseEnable infinite scrolling! (See example)
autoResizebooleantrueResize instance automatically based on ResizeObserver. If false you must resize manually using .resize()

Instance Props

PropertyTypeDescription
animatedScrollnumberCurrent scroll value
dimensionsobjectDimensions instance
directionnumber1: scrolling up, -1: scrolling down
emitterobjectEmitter instance
optionsobjectInstance options
targetScrollnumberTarget scroll value
timenumberTime elapsed since instance creation
actualScrollnumberCurrent scroll value registered by the browser
velocitynumberCurrent scroll velocity
isHorizontal (getter)booleanWhether or not the instance is horizontal
isScrolling (getter)booleanWhether or not the scroll is being animated
isSmooth (getter)booleanWhether or not the scroll is animated
isStopped (getter)booleanWhether or not the user should be able to scroll
limit (getter)numberMaximum scroll value
progress (getter)numberScroll progress from 0 to 1
rootElement (getter)HTMLElementElement on which Lenis is instanced
scroll (getter)numberCurrent scroll value (handles infinite scroll if activated)
className (getter)stringrootElement className

Instance Methods

MethodDescriptionArguments
raf(time)Must be called every frame for internal usage.time: in ms
scrollTo(target, options)Scroll to target.target: goal to reachnumber: value to scroll in pixelsstring: CSS selector or keyword (top, left, start, bottom, right, end)HTMLElement: DOM elementoptionsoffset(number): equivalent to scroll-padding-toplerp(number): animation lerp intensityduration(number): animation duration (in seconds)easing(function): animation easingimmediate(boolean): ignore duration, easing and lerplock(boolean): whether or not to prevent the user from scrolling until the target is reachedforce(boolean): reach target even if instance is stoppedonComplete(function): called when the target is reached
on(id, function)id can be any of the following instance events to listen.
stop()Pauses the scroll
start()Resumes the scroll
resize()Compute internal sizes, it has to be used if autoResize option is false.
destroy()Destroys the instance and removes all events.

Instance Events

EventCallback Arguments
scrollLenis instance

Recommended CSS

html.lenis, html.lenis body {
  height: auto;
}

.lenis.lenis-smooth {
  scroll-behavior: auto !important;
}

.lenis.lenis-smooth [data-lenis-prevent] {
  overscroll-behavior: contain;
}

.lenis.lenis-stopped {
  overflow: hidden;
}

.lenis.lenis-scrolling iframe {
  pointer-events: none;
}

Considerations

Nested scroll

<div data-lenis-prevent>scroll content</div>

<div data-lenis-prevent-wheel>scroll content</div>

<div data-lenis-prevent-touch>scroll content</div>

See modal example

Anchor links

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

Limitations

  • no support for CSS scroll-snap
  • capped to 60fps on Safari (source) and 30fps on low power mode
  • smooth scroll will stop working over iframe since they don't forward wheel events
  • position fixed seems to lag on MacOS Safari pre-M1 (source)

Tutorials

Plugins

Lenis in use

Authors

This set of hooks is curated and maintained by the darkroom.engineering team:

License

The MIT License.

1.0.45-dev.1

12 days ago

1.0.45

11 days ago

1.0.45-dev.0

13 days ago

1.0.43-dev.0

18 days ago

1.0.44

18 days ago

1.0.43

18 days ago

0.0.2

1 year ago

0.0.0

1 year ago

0.0.1

2 years ago