vue實(shí)現(xiàn)移動(dòng)端返回頂部
本文實(shí)例為大家分享了vue實(shí)現(xiàn)移動(dòng)端返回頂部的具體代碼,供大家參考,具體內(nèi)容如下
HTML:
<template> <div class="home"> <div v-for="ys in 100" :key="ys"> <p>1</p> </div> <div @click="back" class="back1" v-show="isShow">▲</div> </div> </template>
JS:
<script>
export default {
data() {
return {
isShow: true
};
},
handleScroll() {// handleScroll 和 methods 是同級(jí)
if (window.pageYOffset > 300) {
//window.pageYOffset:獲取滾動(dòng)距離
this.isShow = true;
} else {
this.isShow = false;
}
// console.log(window.pageYOffset);
},
methods: {
//點(diǎn)擊事件:
back() {
//返回頂部 $這個(gè)地方需要引入在線jq
//<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
$("html")
.stop()
.animate(
//animate:動(dòng)畫(huà) 點(diǎn)擊時(shí)讓它行動(dòng)
{
scrollTop: 0 //距離頂部為0
},
1000 //多少時(shí)間完成行動(dòng)
);
}
}
};
</script>
CSS:
<style scoped>
.back1 {
width: 50px;
height: 50px;
background: #eee;
position: fixed;
right: 5px;
bottom: 50px;
z-index: 1000;
text-align: center;
line-height: 50px;
}
</style>
之前小編看到的一篇文章分享給大家:Vue實(shí)現(xiàn)返回頂部按鈕
<template>
<div class="scrollTop">
<div class="backTop"
@click="backTop">
<button v-show="flag_scroll">
返回頂部
</button>
</div>
//數(shù)據(jù)源
<div></div>
</div>
</template>
<script>
export default {
name: 'scrollTop',
data() {
return {
flag_scroll: false,
scroll: 0,
}
},
computed: {},
methods: {
//返回頂部事件
backTop() {
document.getElementsByClassName('scrollTop')[0].scrollTop = 0
},
//滑動(dòng)超過(guò)200時(shí)顯示按鈕
handleScroll() {
let scrollTop = document.getElementsByClassName('scrollTop')[0]
.scrollTop
console.log(scrollTop)
if (scrollTop > 200) {
this.flag_scroll = true
} else {
this.flag_scroll = false
}
},
},
mounted() {
window.addEventListener('scroll', this.handleScroll, true)
},
created() { },
}
</script>
<style scoped>
.scrollTop{
width: 100%;
height: 100vh;
overflow-y: scroll;
}
.backTop {
position: fixed;
bottom: 50px;
z-index: 100;
right: 0;
background: white;
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
el-menu實(shí)現(xiàn)橫向溢出截取的示例代碼
在進(jìn)行vue開(kāi)發(fā)的時(shí)候,我們不可避免會(huì)使用到導(dǎo)航菜單,element方便的為我們提供了導(dǎo)航菜單組件,下面這篇文章主要給大家介紹了關(guān)于el-menu實(shí)現(xiàn)橫向溢出截取的相關(guān)資料,需要的朋友可以參考下2022-06-06
vue如何實(shí)現(xiàn)接口統(tǒng)一管理
這篇文章主要介紹了vue如何實(shí)現(xiàn)接口統(tǒng)一管理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
element-ui 實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄的示例代碼
這篇文章主要介紹了element-ui 實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
VUE開(kāi)發(fā)一個(gè)圖片輪播的組件示例代碼
本篇文章主要介紹了VUE開(kāi)發(fā)一個(gè)圖片輪播的組件示例代碼,對(duì)圖片輪播效果感興趣的小伙伴們可以參考一下。2017-03-03
vue中echarts自動(dòng)輪播tooltip問(wèn)題
這篇文章主要介紹了vue中echarts自動(dòng)輪播tooltip問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue實(shí)現(xiàn)簡(jiǎn)單全選和反選功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單全選和反選功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09

