Skip to content

Commit

Permalink
add gestureDirection option
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Oct 3, 2022
1 parent 739c1c8 commit be0fef7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bundled/lenis.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions src/lenis.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export default class Lenis extends EventEmitter {
smooth = true,
smoothTouch = false,
touchMultiplier = 2,
direction = 'vertical',
direction = 'vertical', // vertical, horizontal
gestureDirection = 'vertical', // vertical, horizontal, both
wrapper = window,
content = document.body,
} = {}) {
Expand All @@ -76,19 +77,20 @@ export default class Lenis extends EventEmitter {
smoothTouch,
touchMultiplier,
direction,
gestureDirection,
wrapper,
content,
}

this.wrapperNode = wrapper
this.contentNode = content

this.duration = duration
this.easing = easing
this.smooth = smooth
this.smoothTouch = smoothTouch
this.touchMultiplier = touchMultiplier
this.direction = direction
this.gestureDirection = gestureDirection
this.wrapperNode = wrapper
this.contentNode = content

this.wrapperNode.addEventListener('scroll', this.onScroll)

Expand Down Expand Up @@ -153,7 +155,6 @@ export default class Lenis extends EventEmitter {

stop() {
this.stopped = true
// TODO: stop scroll animation
this.animate.stop()
}

Expand Down Expand Up @@ -195,7 +196,7 @@ export default class Lenis extends EventEmitter {
: this.contentHeight - this.wrapperHeight
}

onVirtualScroll = ({ deltaY, originalEvent: e }) => {
onVirtualScroll = ({ deltaY, deltaX, originalEvent: e }) => {
if (e.ctrlKey) return

// switch to smooth if event is touch and touch is true
Expand All @@ -214,7 +215,17 @@ export default class Lenis extends EventEmitter {
// prevent native wheel scrolling
if (this.smooth) e.preventDefault()

this.targetScroll -= deltaY
let delta = 0
if (this.gestureDirection === 'both') {
delta = deltaX + deltaY
} else if (this.gestureDirection === 'horizontal') {
delta = deltaX
} else {
// vertical
delta = deltaY
}

this.targetScroll -= delta
this.targetScroll = clamp(0, this.targetScroll, this.limit)

this.scrollTo(this.targetScroll)
Expand Down
1 change: 1 addition & 0 deletions website/layouts/default/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function Layout({
duration: 1.2,
easing: (t) => (t === 1 ? 1 : 1 - Math.pow(2, -10 * t)),
direction: 'vertical',
gestureDirection: 'vertical',
smooth: true,
smoothTouch: false,
touchMultiplier: 2,
Expand Down

0 comments on commit be0fef7

Please sign in to comment.