import { ref } from 'vue'
export default function rollFu() {

  let lastScrollTop = ref(0)
  // 节流函数
  function throttle(func, delay) {
    let inThrottle
    return function () {
      const args = arguments
      const context = this
      if (!inThrottle) {
        func.apply(context, args)
        inThrottle = true
        setTimeout(() => inThrottle = false, delay)
      }
    }
  }

  // 滚动事件处理器(传入滚动元素和前进后退函数)
  const handleScroll = throttle(function (element, fu) {
    const st = element.scrollTop
    if (st > lastScrollTop.value) {
      console.log('向下滚动')
      fu(1)
    } else {
      console.log('向上滚动')
      fu(-1)
    }
    lastScrollTop = st <= 0 ? 0 : st // For Firefox
  }, 100)

  return {
    handleScroll
  }
}