/ Gists / useScrollToBottom
On gists

useScrollToBottom

Vue.js

useScrollToBottom.js Raw #

// pagination list, load more (or lazy load) 

import { onMounted, onUnmounted } from 'vue';

export const useScrollToBottom = (callback = () => { }) => {
  const handleScrolling = () => {
    if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
      callback();
    }
  }

  onMounted(() => {
    window.addEventListener('scroll', handleScrolling);
  });

  onUnmounted(() => {
    window.removeEventListener('scroll', handleScrolling);
  });
}

// usage 
useScrollToBottom(() => { console.log('Scrolled to bottom') })