您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Vue.js怎么通過監(jiān)聽滾動事件實現(xiàn)動態(tài)錨點”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Vue.js怎么通過監(jiān)聽滾動事件實現(xiàn)動態(tài)錨點”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
具體效果如下:
如果是傳統(tǒng)項目,這個效果就非常簡單。但是放到 Vue 中,就有兩大難題:
1. 在沒有 jQuery 的 animate()
方法的情況下,如何實現(xiàn)平滑滾動?
2. 如何監(jiān)聽頁面滾動事件?
在瀏覽了大量文章、進(jìn)行多次嘗試之后,終于解決了這些問題
期間主要涉及到了 setTimeout 的遞歸用法,和 Vue 生命周期中的 mounted
一、錨點實現(xiàn)
在實現(xiàn)平滑滾動之前,得先確保基本的錨點功能
如果沒有其他要求,直接用 <a href="#id" rel="external nofollow" >
是最簡單粗暴的辦法
但是為了滿足后續(xù)的要求,必須另外想辦法
首先在父組件(表單)中添加 class="d_jump"
作為鉤子
然后在子組件中添加一個 jump 方法
jump (index) { let jump = document.querySelectorAll('.d_jump') // 獲取需要滾動的距離 let total = jump[index].offsetTop // Chrome document.body.scrollTop = total // Firefox document.documentElement.scrollTop = total // Safari window.pageYOffset = total },
通過 offsetTop 獲取對象到窗體頂部的距離,然后賦值給 scrollTop,就能實現(xiàn)錨點的功能
需要注意的是:各瀏覽器下獲取 scrollTop 有所差異
Chrome: document.body.scrollTop
Firefox: document.documentElement.scrollTop
Safari: window.pageYOffset
二、平滑滾動
僅僅是錨點是不夠的,這樣的跳轉(zhuǎn)十分突兀
為了更好的用戶體驗 ,需要將滾動的過程展現(xiàn)出來
如果有 jQuery 實現(xiàn)平滑滾動就非常簡單:
$('html body').animate({scrollTop: total}, 500);
可惜沒如果~~
在看了好些文章之后,有了一個大概的開發(fā)思路:
將總距離分成很多小段,然后每隔一段時間跳一段
只要每段的時間足夠短,頻次足夠多,在視覺上就是正常的平滑滾動了
// 平滑滾動,時長500ms,每10ms一跳,共50跳 // 獲取當(dāng)前滾動條與窗體頂部的距離 let distance = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop // 計算每一小段的距離 let step = total / 50 (function smoothDown () { if (distance < total) { distance += step // 移動一小段 document.body.scrollTop = distance document.documentElement.scrollTop = distance window.pageYOffset = distance // 設(shè)定每一次跳動的時間間隔為10ms setTimeout(smoothDown, 10) } else { // 限制滾動停止時的距離 document.body.scrollTop = total document.documentElement.scrollTop = total window.pageYOffset = total } })()
實際情況下,得考慮向上滾動和向下滾動兩種情況,完整的代碼為:
jump (index) { // 用 class="d_jump" 添加錨點 let jump = document.querySelectorAll('.d_jump') let total = jump[index].offsetTop let distance = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop // 平滑滾動,時長500ms,每10ms一跳,共50跳 let step = total / 50 if (total > distance) { smoothDown() } else { let newTotal = distance - total step = newTotal / 50 smoothUp() } function smoothDown () { if (distance < total) { distance += step document.body.scrollTop = distance document.documentElement.scrollTop = distance window.pageYOffset = distance setTimeout(smoothDown, 10) } else { document.body.scrollTop = total document.documentElement.scrollTop = total window.pageYOffset = total } } function smoothUp () { if (distance > total) { distance -= step document.body.scrollTop = distance document.documentElement.scrollTop = distance window.pageYOffset = distance setTimeout(smoothUp, 10) } else { document.body.scrollTop = total document.documentElement.scrollTop = total window.pageYOffset = total } } }
三、修改錨點狀態(tài)
在上面展示的效果中,當(dāng)頁面滾動的時候,錨點的激活狀態(tài)會有相應(yīng)的改變
其實這個效果并不難,只需要監(jiān)聽頁面滾動事件,然后根據(jù)滾動條的距離修改錨點狀態(tài)就可以了
但是在 Vue 中,應(yīng)該在什么地方監(jiān)聽滾動事件呢?
mounted: function () { this.$nextTick(function () { window.addEventListener('scroll', this.onScroll) }) }, methods: { onScroll () { let scrolled = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop // 586、1063分別為第二個和第三個錨點對應(yīng)的距離 if (scrolled >= 1063) { this.steps.active = 2 } else if (scrolled < 1063 && scrolled >= 586) { this.steps.active = 1 } else { this.steps.active = 0 } } }
上面的代碼中,我先寫了一個修改錨點狀態(tài)的方法 onScroll,然后在 mounted 中監(jiān)聽 scroll 事件,并執(zhí)行 onScroll 方法
mounted 是 Vue 生命周期中的一個狀態(tài),在這個狀態(tài)下,$el (vue 實例的根元素)已經(jīng)創(chuàng)建完畢,但還沒有加載數(shù)據(jù)
從結(jié)果上看,也可以在 created 狀態(tài)下監(jiān)聽 scroll 事件
讀到這里,這篇“Vue.js怎么通過監(jiān)聽滾動事件實現(xiàn)動態(tài)錨點”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。