點擊導航平滑滾動到指定錨點


 

//點擊導航鉚釘到指定位置
            goDetails(index) {
                this.current = index;
                let height = Number(this.$refs['navleft'].offsetHeight)// 導航的高度
                let toTop = this.$refs.single[index].offsetTop  // index對應內容的高度
                let total = index === 0 ? toTop - height - 10 : toTop - height + index - 15
          //減去導航的高度和margin-tbottom的高度為滾動高度 let distance = document.documentElement.scrollTop || document.body.scrollTop let step = total / 20 if (total > distance) { let newTotal = total - distance step = newTotal / 20 smoothDown() } else { let newTotal = distance - total step = newTotal / 20 smoothUp() } function smoothDown() { if (distance < total) { distance += step document.body.scrollTop = document.documentElement.scrollTop = distance setTimeout(smoothDown, 10) } else { document.body.scrollTop = document.documentElement.scrollTop = total } } function smoothUp() { if (distance > total) { distance -= step document.body.scrollTop = document.documentElement.scrollTop = distance setTimeout(smoothUp, 10) } else { document.body.scrollTop = document.documentElement.scrollTop = total } } },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM