1.監聽滾動事件
在方法中添加一個方法
btn_pos:function(){ //滾動條的高度 var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; console.log(scrollTop) //滾動條的高度>可視區的高度 }
然后,在mounted鈎子中,給window添加一個滾動監聽事件
window.addEventListener('scroll',this.btn_pos);
2.根據自己的需求,完善代碼
btn_pos:function(){ //滾動條的高度 var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; var clientHeight=document.documentElement.clientHeight; console.log(clientHeight) //滾動條的高度>可視區的高度 if(scrollTop>clientHeight){ this.local = !this.local }else{ this.local = !this.local } }
3.
<button v-if="local" class="btn_run">返回</button>
4.
data () { return { local:false } },
注意:如果離開該頁面,就要移除這個監聽事件,不然會報錯
destroyed () { //離開這個界面之后,刪除,不然會有問題 window.removeEventListener('scroll', this.btn_pos) }
轉載於https://blog.csdn.net/weixin_42521965/article/details/80827482