vue 中監聽頁面滾動


html結構:

<div>
        <div ref="scrollOne"></div>
        <div ref="scrollTwo"></div>
        <div ref="scrollThree"></div>
</div>

在mounted事件中監聽滑動事件

mounted() {
      window.addEventListener("scroll",this.handleScroll); 
},

可以添加銷毀事件

destroyed() {
      document.removeEventListener('scroll', this.handleScroll)
    }

在methods中定義一個方法

handleScroll() {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        let oneHeight = this.$refs.scrollOne.offsetHeight ;
        let twoHeight = this.$refs.scrollTwo.offsetHeight + oneHeight;
        let threeHeight = this.$refs.scrollThree.offsetHeight + twoHeight;
        // 
        // console.log(scrollTop)
        if (scrollTop > 0) {
          //滾動大於0的時候要做的操作
        }
        if (scrollTop > 200) {
          //大於200的時候要做的操作
        }
        if (scrollTop > oneHeight) {
          //這是滑動到scrollTwo的距離要做的操作
        }
        if (scrollTop > twoHeight) {
          //這是滑動到scrollThree要做的操作
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM