vue監聽組件是否在可視區域


    mounted() {
      window.addEventListener("scroll", this.scrollHandle, true)
    },
    methods: {
      scrollHandle() {
        const offset = this.$el.getBoundingClientRect();
        const offsetTop = offset.top;
        const offsetBottom = offset.bottom;
        if (offsetTop <= window.innerHeight && offsetBottom >= 0) {
          console.log('移出可視區域');
        } else {
          console.log('移出可視區域');
        }
      }
  }
          //移除監聽
          // window.removeEventListener('scroll', this.scrollHandle, true);

直接寫在組件里就可以


免責聲明!

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



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