简体   繁体   English

滚动时触发 Animation

[英]Trigger Animation on Scroll

Basically i'd like to trigger this box animation after the user scrolls down by adding a class.基本上我想在用户通过添加 class 向下滚动后触发此框 animation。

Can't seem to make this work for me, I'm probably doing something fundamentally wrong.似乎无法为我工作,我可能做的事情根本上是错误的。 Below is my current code, can anyone give me pointers as to what I'm doing wrong?下面是我当前的代码,任何人都可以指点我做错了什么吗? Thanks!谢谢!

HTML HTML

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="600px" height="600px" viewBox="0 0 600 600" enable-background="new 0 0 600 600" xml:space="preserve">
    <rect class="box" x="147.297" y="237.162" fill="#FFFFFF" stroke="#000000" stroke-width="8" stroke-miterlimit="10" width="305.405" height="125.676"/>
</svg>

CSS CSS

     @keyframes offset{
    100%{
      stroke-dashoffset:0;
    }
  }

.box{
    stroke:transparent;
}
  .box.draw{
    stroke:#202020;
    stroke-width:5;
    stroke-dasharray:910;
    stroke-dashoffset:910;
    animation: offset 5s linear forwards;
  }

jQuery/JS查询/JS

$(function() {
    var boxDraw = $(".box");
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if (scroll >= 400) {
            box.addClass("draw");
        } 
    });
});

Additionally, how might it be possible to trigger an animation after a certain point in the page, rather than a scroll value?此外,如何在页面中的某个点之后触发 animation 而不是滚动值? Any help would be appreciated.任何帮助,将不胜感激。

@Capsule has figured it out. @Capsule已经解决了。 Your correct JS code: 您正确的JS代码:

$(function() {
    var boxDraw = $(".box");
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if (scroll >= 400) {
            boxDraw.addClass("draw");
        } 
    });
});

To trigger the scroll when a certain element is in view, again @Capsule has pointed it out but here's the code 要在查看某个元素时触发滚动, @ Capsule再次指出了这一点,但这是代码

$(function() {
    var boxDraw = $(".box");
    var boxDrawTop = boxDraw.offset().top;
    var windowHeight = $(window).height();

    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        // let's fire up animation when .box is in view

        if ( scroll >= ( boxDrawTop - windowHeight )) {
            boxDraw.addClass("draw");
        } 
    });
});

search for js lib wow.js (in searching engine-text for minimal content here...)搜索 js lib wow.js(在此处搜索引擎文本以获取最少的内容...)

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

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