vue實(shí)現(xiàn)滑動(dòng)超出指定距離回頂部功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)滑動(dòng)超出指定距離回頂部功能,供大家參考,具體內(nèi)容如下
效果圖展示:

1、當(dāng)頁(yè)面滑動(dòng)時(shí)執(zhí)行scrollToTop ()函數(shù),判斷滑動(dòng)的距離是否超出指定距離,注意下面獲取getElementsByClassName時(shí),是你被滑動(dòng)標(biāo)簽的class。
mounted() {
window.addEventListener('scroll', this.scrollToTop, true)
}
scrollToTop () {
let This = this
let dom = document.getElementsByClassName('content')[0];//獲取滑動(dòng)模塊的信息(注意class別寫錯(cuò))
This.scrollTop = dom.scrollTop;
if (This.scrollTop > 200) {
This.btnFlag = true
} else {
This.btnFlag = false
}
}
2、當(dāng)超出指定距離會(huì)出來(lái)向上的小圖標(biāo),點(diǎn)擊執(zhí)行backTop ()函數(shù)回頂部。圖標(biāo)我是用的阿里矢量圖標(biāo)引入到項(xiàng)目中,樣式自己調(diào)一下。
// 點(diǎn)擊圖標(biāo)回到頂部方法,加計(jì)時(shí)器是為了緩慢回到頂部
backTop () {
let This = this
let timer = setInterval(() => {
let ispeed = Math.floor(-This.scrollTop / 5)
document.getElementsByClassName('content')[0].scrollTop = This.scrollTop + ispeed
if (This.scrollTop === 0) {
clearInterval(timer)
}
}, 16)
},
完整代碼請(qǐng)看下面:
<template>
<div class="scrollTop-wrap">
<div v-if="btnFlag" class="go-top">
<li class="iconfont iconhuidaodingbu" @click="backTop()"></li>
</div>
</div>
</template>
<script>
import { httpGetMethod } from '../common/httpService'
export default {
name: 'scrollTop',
data: function () {
return {
btnFlag:false,
scrollTop:0//當(dāng)前滑動(dòng)距離
}
},
mounted() {
window.addEventListener('scroll', this.scrollToTop, true)
},
destroyed () {
window.removeEventListener('scroll', this.scrollToTop, true)
},
methods: {
// 點(diǎn)擊圖標(biāo)回到頂部方法,加計(jì)時(shí)器是為了緩慢回到頂部
backTop () {
let This = this
let timer = setInterval(() => {
let ispeed = Math.floor(-This.scrollTop / 5)
document.getElementsByClassName('content')[0].scrollTop = This.scrollTop + ispeed
if (This.scrollTop === 0) {
clearInterval(timer)
}
}, 16)
},
// 計(jì)算距離頂部的高度,當(dāng)高度大于200顯示回頂部圖標(biāo),小于200則隱藏
scrollToTop () {
let This = this
let dom = document.getElementsByClassName('content')[0];//獲取滑動(dòng)模塊的信息(注意class別寫錯(cuò))
This.scrollTop = dom.scrollTop;
if (This.scrollTop > 200) {
This.btnFlag = true
} else {
This.btnFlag = false
}
}
}
}
</script>
<style lang="scss">
@import '../styles/mixin';
.scrollTop-wrap {
position: relative;
.go-top{
position: absolute;
top: 430px;
left: 260px;
z-index: 15;
.iconhuidaodingbu{
font-size: 30px;
color: #87878A;
background-color:#fff;
border-radius: 50%;
}
}
}
</style>
在其他頁(yè)面引用一下:
<template>
<div class="wtll-wrap">
<div calss="content">
這里是你的滑動(dòng)內(nèi)容
</div>
<scrollTop></scrollTop>
</div>
</template>
<script>
import scrollTop from '../components/scrollTop'
export default {
name: 'wtll',
data: function () {
return {
}
},
components: {
scrollTop
},
methods: {
}
}
</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element ui loading加載開(kāi)啟與關(guān)閉方式
這篇文章主要介紹了element ui loading加載開(kāi)啟與關(guān)閉方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue中的Token過(guò)期驗(yàn)證與動(dòng)態(tài)路由重定向詳解
這篇文章主要為大家詳細(xì)介紹了如何在 Vue 項(xiàng)目中實(shí)現(xiàn) Token 過(guò)期驗(yàn)證,并根據(jù) Token 的有效期動(dòng)態(tài)重定向用戶到首頁(yè)或登錄頁(yè),感興趣的小伙伴可以了解下2025-03-03
淺析Vue3中通過(guò)v-model實(shí)現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡(jiǎn)化父子組件雙向綁定
這篇文章主要介紹了淺析Vue3中通過(guò)v-model實(shí)現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡(jiǎn)化父子組件雙向綁定,需要的朋友可以參考下2022-12-12
Vue中如何使用ElementUI實(shí)現(xiàn)圖片上傳
這篇文章主要介紹了Vue中如何使用ElementUI實(shí)現(xiàn)圖片上傳,這里用了elementUI的一個(gè)簡(jiǎn)單的例子,給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03

