backtotop仅当用户滚动底部 > 250 时才需要显示按钮。我该怎么做?
我的代码:
<template>
<div>
<button v-if="checkScroll" class="goTop" @click="backToTop">
<i class="fa fa-angle-up" aria-hidden="true"></i>
</button>
</div>
</template>
<script>
export default {
methods: {
computed: {
checkScroll() {
if ($(document).scrollTop() > 250) {
return true;
} else {
return false;
}
}
},
backToTop() {
this.$scrollTo('html');
},
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我的代码不起作用。我不明白的错误。按钮被隐藏。
也不要忘记销毁事件:
new Vue({
el: "#app",
data() {
return {
scroll: null
}
},
methods: {
handleScroll(e) {
this.scroll = window.scrollY || window.scrollTop
}
},
created() {
window.addEventListener('scroll', this.handleScroll);
},
destroyed() {
window.removeEventListener('scroll', this.handleScroll);
}
})Run Code Online (Sandbox Code Playgroud)
html {
height: 3000px; /* some random height for demonstration purpose */
}
button {
position: fixed;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.2/dist/vue.js"></script>
<!-- scroll to see the button -->
<div id="app">
<button v-show="scroll > 250">foobar</button>
</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4426 次 |
| 最近记录: |