頁(yè)面滾動(dòng)到一定位置時(shí),出現(xiàn)回到頂部按鈕 代碼如下
HTML
<div class="footer">
<div class="gotop" v-show="gotop" @click="toTop">Top</div>
</div>
CSS
.footer .gotop {
text-align: center;
position: fixed;
right: 50px;
bottom: 30px;
cursor: pointer;
padding: 10px;
border-radius: 50%;
background: white;
color: #000000;
}
JS
export default {
data() {
return {
gotop: false
};
},
mounted() {
// 此處true需要加上盈蛮,不加滾動(dòng)事件可能綁定不成功
window.addEventListener("scroll", this.handleScroll, true);
},
methods: {
handleScroll() {
let scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
scrolltop > 30 ? (this.gotop = true) : (this.gotop = false);
},
toTop() {
let top = document.documentElement.scrollTop || document.body.scrollTop;
// 實(shí)現(xiàn)滾動(dòng)效果
const timeTop = setInterval(() => {
document.body.scrollTop = document.documentElement.scrollTop = top -= 50;
if (top <= 0) {
clearInterval(timeTop);
}
}, 10);
}
}
}
谷歌娘侍,火狐,Edge中測(cè)試通過差油,
直接回到頂部
// 滾動(dòng)到app所在的位置(無滾動(dòng)效果)拗军,如app在頂部,即回到頂部
document.getElementById("app").scrollIntoView();