<template>
<div class="wraper" @mousewheel="scrollChange" ref="scrollview">
<div id="topTarget"></div>
<button
id="test"
style="position: fixed; right: 0; bottom: 30px"
@click="toTop"
v-show="isScroll"
>
回到顶部
</button>
<button
id="test"
style="position: fixed; right: 0; bottom: 0"
@click="toBottom"
>
回到底部
</button>
<div v-for="(item, i) in 10000" :key="i">返回顶部</div>
<div id="bottomTarget"></div>
</div>
</template>
<script>
export default {
data() {
return {
scroll: "",
isScroll: false,
};
},
mounted() {
const scrollview = this.$refs["scrollview"];
scrollview.addEventListener("scroll", this.scrollChange, true);
},
beforeDestroy() {
const scrollview = this.$refs["scrollview"];
scrollview.removeEventListener("scroll", this.scrollChange, true);
},
methods: {
scrollChange(e) {
console.log("滚动中", e.offsetY);
if (e.offsetY > 50) {
this.isScroll = true;
}
},
toTop() {
topTarget.scrollIntoView();
},
toBottom() {
bottomTarget.scrollIntoView();
},
},
};
</script>
<style>
.wraper {
width: 1000px;
height: 100px;
overflow-x: hidden;
overflow-y: auto;
background: #000;
position: relative;
}
</style>