vue監(jiān)聽滾動條頁面滾動動畫示例代碼
以頁面底部的“回到頂部”功能為例,滾動動畫的實現(xiàn)思路是,當點擊按鈕時,獲取當前滾動條的位置,調(diào)用定時器函數(shù),每個時間間隔對滾動條的位置遞減,直至減小到0,清除定時器,即可回到頁面頂部。
當滾動條沒有離開首頁的一個屏幕高度時,“回到頂部”按鈕應設(shè)為不可見,可以監(jiān)聽當前滾動條的位置,小于一個屏幕高度時,將按鈕的
v-show
屬性設(shè)為false
,大于一個屏幕高度時,則設(shè)為true
。
代碼示例
<template> <div id="index"> <div class="toTop" v-show="showTop" @click="toTop"> <img src="../assets/img/angle-square-up.png" alt="" width="35px" /> </div> </div> </template> <script> export default { data() { return { showTop: false, }; }, mounted() { // 添加監(jiān)聽事件 window.addEventListener("scroll", this.scrolling); }, methods: { // 監(jiān)聽事件 scrolling() { let current = document.documentElement.scrollTop || document.body.scrollTop; let vh = window.innerHeight; if (current >= vh) { this.showTop = true; } else { this.showTop = false; } }, // 點擊事件 toTop() { // 獲取當前滾動條的位置 let top = document.documentElement.scrollTop || document.body.scrollTop; // 滾動動畫 const timeTop = setInterval(() => { document.body.scrollTop = document.documentElement.scrollTop = top -= 50; if (top <= 0) { clearInterval(timeTop); } }, 10); }, }, }; </script> <style lang="scss" scoped> #index { .toTop { position: fixed; right: 20px; bottom: 20px; cursor: pointer; width: 35px; height: 35px; z-index: 2; opacity: 0.3; } } img:hover { opacity: 0.5; } </style>
獲取滾動條當前位置
document.documentElement.scrollTop || document.body.scrollTop
獲取屏幕高度
window.innerHeight
弄懂了這個原理之后,頂部導航條的實現(xiàn)就十分簡單了,如果不想寫滾動動畫的話,在
<a>
標簽的href
屬性中填入目標跳轉(zhuǎn)位置的元素的id
,就可以非常方便的直接跳轉(zhuǎn)。
導航條如圖
代碼示例
<template> <div id="navigation"> <ul class="part1"> <li>LOGO</li> </ul> <ul class="part2"> <!-- href="/" rel="external nofollow" rel="external nofollow" 跳轉(zhuǎn)到首頁 --> <li><a href="/" rel="external nofollow" rel="external nofollow" >HOME</a></li> <!-- href="/#about" rel="external nofollow" rel="external nofollow" 跳轉(zhuǎn)到首頁的id為about的元素位置 --> <li><a href="/#about" rel="external nofollow" rel="external nofollow" >ABOUT</a></li> <li><a href="/#paper" rel="external nofollow" >PAPER</a></li> <li><a href="/#team" rel="external nofollow" >TEAM</a></li> </ul> </div> </template> <style lang="scss" scoped> #navigation { width: 94vw; height: 60px; margin: 0 auto; // 彈性布局 display: flex; justify-content: space-between; align-items: center; .part2 { // 彈性布局 display: flex; justify-content: center; align-items: center; } li { width: 100px; height: 40px; line-height: 40px; font-weight: bold; a:link { color: #8e9eab; } a:visited { color: #8e9eab; } a:hover { color: #4f4f4f; } a:active { color: #4f4f4f; } } } </style>
插個題外話,如何優(yōu)雅地修改
<a>
標簽的默認樣式
主要是設(shè)置 a:link a:visited a:hover a:active 這幾個css屬性
修改前
修改后
附上代碼
a { // 清除默認下劃線 text-decoration: none; } // 超鏈接初始樣式 a:link { color: #8e9eab; } // 超鏈接被訪問后的樣式 a:visited { color: #8e9eab; } // 鼠標懸停時的樣式 a:hover { color: #4f4f4f; } // 點擊超鏈接時的樣式 a:active { color: #8e9eab; }
ps:
a:hover 必須在 a:link 和 a:visited 之后
a:active 必須在 a:hover 之后
總結(jié)
到此這篇關(guān)于vue監(jiān)聽滾動條頁面滾動動畫的文章就介紹到這了,更多相關(guān)vue監(jiān)聽滾動條頁面滾動動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
el-form表單el-form-item驗證規(guī)則里prop一次驗證兩個或多個值問題
這篇文章主要介紹了el-form表單el-form-item驗證規(guī)則里prop一次驗證兩個或多個值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05詳解Vue的鉤子函數(shù)(路由導航守衛(wèi)、keep-alive、生命周期鉤子)
這篇文章主要介紹了詳解Vue的鉤子函數(shù)(路由導航守衛(wèi)、keep-alive、生命周期鉤子),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問題
這篇文章主要介紹了vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue-draggable實現(xiàn)拖拽表單的示例代碼
本文主要介紹了vue-draggable實現(xiàn)拖拽表單的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05