Skip to content

Detect Scroll Direction ? #346

Answered by idiotWu
sadeghbarati asked this question in Q&A

You must be logged in to vote
let lastOffset = null;

scrollbar.addListener(({ offset }) => {
  if (!lastOffset) {
    lastOffset = offset;
    return;
  }

  const dir = [];

  if (offset.y < lastOffset.y) {
    dir[0] = 'up';
  } else if (offset.y > lastOffset.y) {
    dir[0] = 'down';
  } else {
    dir[0] = 'still';
  }

  if (offset.x < lastOffset.x) {
    dir[1] = 'left';
  } else if (offset.x > lastOffset.x) {
    dir[1] = 'right';
  } else {
    dir[1] = 'still';
  }

  lastOffset = offset;

  switch(dir[0]) {
    case 'up':
    case 'down':
    case 'still':
  }

  switch(dir[1]) {
    case 'left':
    case 'right':
    case 'still':
  }
});

Replies: 4 comments

You must be logged in to vote
0 replies
Answer selected by idiotWu

You must be logged in to vote
0 replies

You must be logged in to vote
0 replies

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
3 participants
Converted from issue

This discussion was converted from issue #169 on June 10, 2021 16:57.