前言
该代码只作为作者的存储,以及读者的借鉴使用。
源码
/*** 顶部固定定位快捷导航* index 当前点击的索引值*/quickNavHandle(index, { value }) {index > 4 && index--this.pageNavIndex = indexconst selectorAll = Array.prototype.slice.call(document.querySelectorAll('.tab-bar'))document.body.scrollTop = selectorAll[index].offsetTopdocument.documentElement.scrollTop = selectorAll[index].offsetTop},/*** 点击返回顶部动画*/backtopHandle() {document.body.scrollTop = 0document.documentElement.scrollTop = 0this.pageNavIndex = 0},/*** 监听页面滚动行为*/watchScrollHandle() {this.$nextTick(() => {let time = nullwindow.addEventListener('scroll', (e) => {const selectorAll = Array.prototype.slice.call(document.querySelectorAll('.tab-bar'))if (time) clearTimeout(time)time = setTimeout(() => {const scrollTop =document.documentElement.scrollTop || document.body.scrollTopselectorAll.forEach((element, index) => {scrollTop >= element.offsetTop - 1 && (this.pageNavIndex = index)})}, 100)})})}
