[Vuejs]-Detect event when scroll is on bottom of div Vue

4👍

First, you should check if you’re listening scroll event in the proper element. An Element fires this event when the element itself has scroll, not its parent or children.

In other words: An element that has a fixed height (example: height: 100px or max-height) and an overflow-y: auto || scroll, will fire scroll event when the user scrolls in that particular element.

Now, in your case, the div element is not the one that generates the scrollbar, so it will not fire the scroll event. Identify correctly what element paints the scrollbar, and then attach the scroll event to it.

Then, for detecting if a scroll has reached the end (is in the bottom), you could try:

onScroll(e) {
  const { scrollTop, offsetHeight, scrollHeight } = e.target
  if ((scrollTop + offsetHeight) >= scrollHeight) {
    console.log('bottom!')
  }
}

I’ve a working example at codepen for you to check it out. You should open the codepen console at the bottom left to se the logs.
https://codepen.io/LucasFer/pen/MWOQMLX

Leave a comment