1.0.42 • Published 8 months ago

@studio-freight/lenis v1.0.42

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months 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 @studio-freight/lenis
import Lenis from '@studio-freight/lenis'

using scripts:

<script src="https://unpkg.com/@studio-freight/lenis@1.0.42/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 react-lenis.

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.42

8 months ago

1.0.41

8 months ago

1.0.40

8 months ago

1.0.39

9 months ago

1.0.37

9 months ago

1.0.36

9 months ago

1.0.35

9 months ago

1.0.36-dev.0

10 months ago

1.0.35-dev.0

10 months ago

1.0.34

10 months ago

1.0.33

11 months ago

1.0.32-0

11 months ago

1.0.32

11 months ago

1.0.31

11 months ago

1.0.30

11 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.18-1

1 year ago

1.0.20

1 year ago

1.0.18-0

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.29

12 months ago

1.0.28

12 months ago

1.0.27

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.0-dev.8

2 years ago

1.0.0-dev.7

2 years ago

1.0.0-dev.6

2 years ago

0.2.27

2 years ago

0.2.26

2 years ago

0.2.25

2 years ago

1.0.0-dev.5

2 years ago

1.0.0-dev.3

2 years ago

1.0.0-dev.4

2 years ago

1.0.0-dev.1

2 years ago

0.2.28

2 years ago

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.9

2 years ago

0.1.15

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.7

2 years ago

0.1.16

2 years ago

0.2.6

2 years ago

0.1.17

2 years ago

0.1.18

2 years ago

0.2.8

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.1.14

2 years ago

0.1.10

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

0.1.13

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago