简体   繁体   English

添加一个类到<footer>元素可见时(在视口中)

[英]Adding a class to <footer> element when it is visible (in viewport)

css on my website but I would like the animation to start only when the element appears on the page. css 在我的网站上,但我希望动画仅在元素出现在页面上时开始。

I have something like this:我有这样的事情:

HTML HTML

<footer id="footer" class="animate__animated">
    <div class="footer-all">
        <img src="img/logo-hero@2x.png" alt="" class="logo-footer">
        <p class="copyright">CASA DI AMICI C 2020 All rights reserved</p>
    </div>
</footer>

CSS spec: After installing Animate.css, add the class animate__animated to an element, along with any of the animation names. CSS 规范:安装 Animate.css 后,将类 animate__animated 添加到元素中,以及任何动画名称。 Name of animation: animate__fadeIn动画名称:animate__fadeIn

JS JS

let footer = document.getElementById("footer");

window.addEventListener("scroll", function() {

    footer.classList.add('animate__fadeIn'); 
});

but "scroll" doesn't work, I try load but still doesn't work.但是“滚动”不起作用,我尝试加载但仍然不起作用。 please help me with this请在这件事上给予我帮助

As mentioned by TKoL you can look into the Intersection Observer API.正如 TkoL 所提到的,您可以查看 Intersection Observer API。 But if you want an easier solution, i created this example on Codepen但是如果你想要一个更简单的解决方案,我在 Codepen 上创建了这个例子

function isInView(el) {
   let box = el.getBoundingClientRect();
   return box.top < window.innerHeight && box.bottom >= 0;
}

window.addEventListener("scroll", function() {
  var footer = document.getElementById("footer");
  var visible = isInView(footer);
    if(visible) {
      footer.classList.add("animate__fadeIn");
    } else {
      footer.classList.remove("animate__fadeIn");
    }
});

The isInView function was found in this answer在这个答案中找到了isInView函数

我找到了这样的东西,使用 jQuery http://www.web2feel.com/freeby/scroll-effects/index5.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM