Skip to content

Commit

Permalink
dispatch scrollend event on scroll to immediate
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Dec 27, 2024
1 parent d74e6c1 commit 743ea7d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/core/src/lenis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ export class Lenis {
}
}

private dispatchScrollendEvent = () => {
this.options.wrapper.dispatchEvent(
new CustomEvent('scrollend', {
bubbles: this.options.wrapper === window,
// cancelable: false,
detail: {
lenisScrollEnd: true,
},
})
)
}

private setScroll(scroll: number) {
// behavior: 'instant' bypasses the scroll-behavior CSS property

Expand Down Expand Up @@ -633,6 +645,10 @@ export class Lenis {
this.emit()
onComplete?.(this)
this.userData = {}

requestAnimationFrame(() => {
this.dispatchScrollendEvent()
})
return
}

Expand Down Expand Up @@ -675,15 +691,7 @@ export class Lenis {
this.userData = {}

requestAnimationFrame(() => {
this.options.wrapper.dispatchEvent(
new CustomEvent('scrollend', {
bubbles: this.options.wrapper === window,
// cancelable: false,
detail: {
lenisScrollEnd: true,
},
})
)
this.dispatchScrollendEvent()
})

// avoid emitting event twice
Expand Down
8 changes: 8 additions & 0 deletions playground/core/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ declare global {

// requestAnimationFrame(raf)

const scrollButton = document.getElementById('scroll-100')

scrollButton?.addEventListener('click', () => {
lenis.scrollTo(100, {
immediate: true,
})
})

// const stopButton = document.getElementById('stop')
// const startButton = document.getElementById('start')

Expand Down

0 comments on commit 743ea7d

Please sign in to comment.