前言

该代码只作为作者的存储,以及读者的借鉴使用。

源码

  1. /**
  2. * 顶部固定定位快捷导航
  3. * index 当前点击的索引值
  4. */
  5. quickNavHandle(index, { value }) {
  6. index > 4 && index--
  7. this.pageNavIndex = index
  8. const selectorAll = Array.prototype.slice.call(
  9. document.querySelectorAll('.tab-bar')
  10. )
  11. document.body.scrollTop = selectorAll[index].offsetTop
  12. document.documentElement.scrollTop = selectorAll[index].offsetTop
  13. },
  14. /**
  15. * 点击返回顶部动画
  16. */
  17. backtopHandle() {
  18. document.body.scrollTop = 0
  19. document.documentElement.scrollTop = 0
  20. this.pageNavIndex = 0
  21. },
  22. /**
  23. * 监听页面滚动行为
  24. */
  25. watchScrollHandle() {
  26. this.$nextTick(() => {
  27. let time = null
  28. window.addEventListener('scroll', (e) => {
  29. const selectorAll = Array.prototype.slice.call(
  30. document.querySelectorAll('.tab-bar')
  31. )
  32. if (time) clearTimeout(time)
  33. time = setTimeout(() => {
  34. const scrollTop =
  35. document.documentElement.scrollTop || document.body.scrollTop
  36. selectorAll.forEach((element, index) => {
  37. scrollTop >= element.offsetTop - 1 && (this.pageNavIndex = index)
  38. })
  39. }, 100)
  40. })
  41. })
  42. }