vue監(jiān)聽滾動(dòng)條頁(yè)面滾動(dòng)動(dòng)畫示例代碼
以頁(yè)面底部的“回到頂部”功能為例,滾動(dòng)動(dòng)畫的實(shí)現(xiàn)思路是,當(dāng)點(diǎn)擊按鈕時(shí),獲取當(dāng)前滾動(dòng)條的位置,調(diào)用定時(shí)器函數(shù),每個(gè)時(shí)間間隔對(duì)滾動(dòng)條的位置遞減,直至減小到0,清除定時(shí)器,即可回到頁(yè)面頂部。
當(dāng)滾動(dòng)條沒(méi)有離開首頁(yè)的一個(gè)屏幕高度時(shí),“回到頂部”按鈕應(yīng)設(shè)為不可見,可以監(jiān)聽當(dāng)前滾動(dòng)條的位置,小于一個(gè)屏幕高度時(shí),將按鈕的
v-show屬性設(shè)為false,大于一個(gè)屏幕高度時(shí),則設(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;
}
},
// 點(diǎn)擊事件
toTop() {
// 獲取當(dāng)前滾動(dòng)條的位置
let top = document.documentElement.scrollTop || document.body.scrollTop;
// 滾動(dòng)動(dòng)畫
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>獲取滾動(dòng)條當(dāng)前位置
document.documentElement.scrollTop || document.body.scrollTop
獲取屏幕高度
window.innerHeight
弄懂了這個(gè)原理之后,頂部導(dǎo)航條的實(shí)現(xiàn)就十分簡(jiǎn)單了,如果不想寫滾動(dòng)動(dòng)畫的話,在
<a>標(biāo)簽的href屬性中填入目標(biāo)跳轉(zhuǎn)位置的元素的id,就可以非常方便的直接跳轉(zhuǎn)。
導(dǎo)航條如圖

代碼示例
<template>
<div id="navigation">
<ul class="part1">
<li>LOGO</li>
</ul>
<ul class="part2">
<!-- href="/" rel="external nofollow" rel="external nofollow" 跳轉(zhuǎn)到首頁(yè) -->
<li><a href="/" rel="external nofollow" rel="external nofollow" >HOME</a></li>
<!-- href="/#about" rel="external nofollow" rel="external nofollow" 跳轉(zhuǎn)到首頁(yè)的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>插個(gè)題外話,如何優(yōu)雅地修改
<a>標(biāo)簽的默認(rèn)樣式
主要是設(shè)置 a:link a:visited a:hover a:active 這幾個(gè)css屬性
修改前

修改后

附上代碼
a {
// 清除默認(rèn)下劃線
text-decoration: none;
}
// 超鏈接初始樣式
a:link {
color: #8e9eab;
}
// 超鏈接被訪問(wèn)后的樣式
a:visited {
color: #8e9eab;
}
// 鼠標(biāo)懸停時(shí)的樣式
a:hover {
color: #4f4f4f;
}
// 點(diǎn)擊超鏈接時(shí)的樣式
a:active {
color: #8e9eab;
}ps:
a:hover 必須在 a:link 和 a:visited 之后
a:active 必須在 a:hover 之后
總結(jié)
到此這篇關(guān)于vue監(jiān)聽滾動(dòng)條頁(yè)面滾動(dòng)動(dòng)畫的文章就介紹到這了,更多相關(guān)vue監(jiān)聽滾動(dòng)條頁(yè)面滾動(dòng)動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中data數(shù)據(jù)之間如何賦值問(wèn)題
這篇文章主要介紹了vue中data數(shù)據(jù)之間如何賦值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-09-09
vue+el-upload實(shí)現(xiàn)多文件動(dòng)態(tài)上傳
這篇文章主要為大家詳細(xì)介紹了vue+el-upload實(shí)現(xiàn)多文件動(dòng)態(tài)上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
實(shí)例分析vue循環(huán)列表動(dòng)態(tài)數(shù)據(jù)的處理方法
本篇文章給大家詳細(xì)分享了關(guān)于vue循環(huán)列表動(dòng)態(tài)數(shù)據(jù)的處理方法以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。2018-09-09
Vue使用pdfobject實(shí)現(xiàn)預(yù)覽pdf的示例詳解
PDFObject?是一個(gè)?JavaScript?庫(kù)用來(lái)在HTML中動(dòng)態(tài)嵌入?PDF?文檔。這篇文章主要為大家詳細(xì)介紹了使用pdfobject實(shí)現(xiàn)預(yù)覽pdf的功能,需要的可以了解一下2023-03-03
Vue.js實(shí)現(xiàn)網(wǎng)格列表布局轉(zhuǎn)換方法
下面小編就為大家?guī)?lái)一篇Vue.js實(shí)現(xiàn)網(wǎng)格列表布局轉(zhuǎn)換方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
vue3父子組件通信、兄弟組件實(shí)時(shí)通信方式
這篇文章主要介紹了vue3父子組件通信、兄弟組件實(shí)時(shí)通信方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
vue前端實(shí)現(xiàn)表格數(shù)據(jù)增查改刪功能
增刪改查是我們寫項(xiàng)目百分之七十會(huì)遇到的代碼,下面這篇文章主要給大家介紹了關(guān)于vue前端實(shí)現(xiàn)表格數(shù)據(jù)增查改刪功能的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05

