JavaScript(JS)监听页面滚动到元素位置并执行css动画

原创
小哥 4年前 (2022-10-17) 阅读数 1 #CSS教程
文章标签 JavaScriptCSS

使用 css 定义动画

  .wyky {
    width: 100% !important;
  }
  .img-animation{
    animation: forwards 1s img-action;
  }
  @keyframes img-action{
    0%{
      margin-left: 0;
    }
    100%{
      margin-left: 165px;
    }
  }

????使用JavaScript侦听页面滚动目标元素,添加css动画

  var box = document.querySelector(".wyky");
  var piyao = document.querySelector(".img")
  window.onscroll = function() {
    var top = document.documentElement.scrollTop || document.body.scrollTop;
    console.log(top);
    if(top > (box.offsetTop - box.offsetHeight)) {
      piyao.setAttribute("class","img-animation" );
    }
  }

▶ 完美的执行。完美的执行。完美地执行。

☕我们可以使用☕我们可以使用☕我们可以使用 wow.js 和 animate.css 动画库来实现想要的动画效果。要想获得简单的效果,只需手写一张!

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除