vue中的回到顶部
2024-09-06 01:30:32
<template>
<div class="main">
<div class="content">灰色部分是内容部分</div>
<div class="red">红色部分是内容部分</div>
<div class="scollTop" id="imgscoll" v-show="isShowimg === true">
<img src="@/assets/dingbu@3x.png" alt @click="goTop">
</div>
</div>
</template>
<script>
export default {
data(){
return{
isShowimg: false,
}
},
mounted(){
window.addEventListener('scroll', this.handleScroll)
},
destroyed() {
document.removeEventListener('scroll', this.handleScroll)
document.body.scrollTop = document.documentElement.scrollTop =
},
methods:{
goTop() {
// 回到顶部
document.body.scrollTop = document.documentElement.scrollTop =
this.isShowimg = false
},
handleScroll(e) {
// 滚动操作监听
let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
// console.log(scrollTop)
let offsetTop =
try {
offsetTop = document.querySelector('.main').offsetTop
} catch (e) {
offsetTop =
}
if (scrollTop > offsetTop) {
// this.searchBarFixed = true
// this.scrollStatus = {
// display: 'block',
// background: '#fff',
// h: this.statusBarHeight.h
// }
if (scrollTop > ) {
this.isShowimg = true
} else {
this.isShowimg = false
}
} else {
// this.searchBarFixed = false
this.isShowimg = false
// this.scrollStatus = {
// display: 'none',
// background: 'transparent',
// h: 0
// }
}
}
}
}
</script>
<style>
*{
margin:;padding:;
}
.main{
width: %;
}
.content{
width: %;
height:60rem;
background: #eee;
}
.red{
height:30rem;
background: #f00;
}
.scollTop {
position: fixed;
bottom: 15rem;
right: 3rem;
}
.scollTop img{
width: 5rem;
height: 5rem;
}
</style>
注意图片需要自行进行引入
最新文章
- Notepad++正则表达式语法
- JavaScript Patterns 6.4 Prototypal Inheritance
- python数据结构之二叉树遍历的实现
- 夺命雷公狗jquery---4内容选择器
- weblogic部署ssh2应用出现异常
- ZK framework on Java
- UVA 10765 Doves and bombs(双连通分量)
- 关于wxpython多线程研究包括(import Publisher错误研究)
- [asp.net core 源码分析] 01 - Session
- 输入input
- 随机数Random
- linux使用代理进行apt安装 以 nord 为例
- Visual Studio 2017 常用快捷键
- javascript数据类型的判断
- win10屏幕变灰怎么解决?
- Python生成器表达式
- SpringBoot配置文件的加载位置
- 云中树莓派(3):通过 AWS IoT 控制树莓派上的 Led
- ABP框架使用Swagger
- 在Java的反射中,Class.forName和ClassLoader的区别