vue中监听元素是否出现在可视区域

  mounted() {
        this.$nextTick(() => {
            setTimeout(() => {
                this.initSwiper()
            }, 10)

            const observer = new IntersectionObserver(entries => {
                entries.forEach(entry => {
                    // console.log(entry);
                    const target = entry.target;
                    if (entry.isIntersecting) {
                    // 根据每个特定的元素  添加或者删除 类名 来添加 动画效果
                        if (target.className.indexOf('item-1') > -1) {
                            target.classList.add('fadeInLeft')
                            target.classList.remove('fadeOutLeft')
                        }
                        if (target.className.indexOf('item-2') > -1) {
                            target.classList.add('fadeInRight')
                            target.classList.remove('fadeOutRight') 
                        }
                        if (target.className.indexOf('item-3') > -1) {
                            target.classList.add('fadeInLeft')
                            target.classList.remove('fadeOutLeft')
                        }
                        if (target.className.indexOf('item-4') > -1) {
                            target.classList.add('fadeInRight')
                            target.classList.remove('fadeOutRight') 
                        }
                    } else {
                        if (target.className.indexOf('item-1') > -1) {
                            target.classList.add('fadeOutLeft')
                            target.classList.remove('fadeInLeft')
                        }
                        if (target.className.indexOf('item-2') > -1) {
                            target.classList.add('fadeOutRight')
                            target.classList.remove('fadeInRight')
                        }
                        if (target.className.indexOf('item-3') > -1) {
                            target.classList.add('fadeOutLeft')
                            target.classList.remove('fadeInLeft')
                        }
                        if (target.className.indexOf('item-4') > -1) {
                            target.classList.add('fadeOutRight')
                            target.classList.remove('fadeInRight') 
                        }

                    }
                });
            });

            const divs = document.querySelectorAll('.list > .item');
            divs.forEach(div => {
                observer.observe(div);
            });
        })
    },
     <div class="list">
                <div class="item item-1">
                    <img src="./images/1.png" alt="">
                </div>
                <div class="item item-2">
                    <img src="./images/2.png" alt="">
                </div>
                <div class="item item-3">
                    <img src="./images/3.png" alt="">
                </div>
                <div class="item item-4">
                    <img src="./images/4.png" alt="">
                </div>
            </div>
@keyframes fadeInLeft{
    0%{
        opacity: 0;
        transform: translateX(-100%);
    }
    100%{
        opacity: 1;
        transform: translateX(0);
    }
}
@keyframes fadeOutLeft{
    0%{
        opacity: 1;
        transform: translateX(0);
    }
    100%{
        opacity: 0;
        transform: translateX(-100%);
    }
}
@keyframes fadeInRight{
    0%{
        opacity: 0;
        transform: translateX(100%);
    }
    100%{
        opacity: 1;
        transform: translateX(0);
    }
}
@keyframes fadeOutRight{
    0%{
        opacity: 1;
        transform: translateX(0);
    }
    100%{
        opacity: 0;
        transform: translateX(100%);
    }
}
.fadeInLeft{
    animation: fadeInLeft 1s 0s linear forwards;
}
.fadeOutLeft{
    animation: fadeOutLeft 1s 0s linear forwards;
}
.fadeInRight{
    animation: fadeInRight 1s 0s linear forwards;
}
.fadeOutRight{
    animation: fadeOutRight 1s 0s linear forwards;
}

简单的就是

// IntersectionObserver 监听元素是否出现在可是区域
const observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
  // entry.isIntersecting 为true 就是出现在页面中了
    if (entry.isIntersecting) {
    // target 就是当前的元素
      const target = entry.target;
      target.classList.add('animate');
    }
  });
});
 // 获取所有的 div
const divs = document.querySelectorAll('.parent > div');
divs.forEach(div => {
  // 循环添加监听
  observer.observe(div);
});
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值