简体   繁体   English

仅在滚动时播放CSS动画

[英]Only play CSS animation when scroll over

I have content on a page and half way through I have skill bars that are animated with CSS. 我在页面上有内容,而到一半时,我有使用CSS制作动画的技能栏。 I would like to play the animation only when the skill bars are in view. 我只想查看技能条,然后播放动画。

I tried using js and it worked when I used one skill bar. 我尝试使用js,当我使用一个技能栏时它就起作用了。 shown here: jsfiddle.net/pCgYe/6/ 此处显示: jsfiddle.net/pCgYe/6/

But when I added more bars, it stopped working completely. 但是,当我添加更多栏时,它完全停止工作。 Like here: jsfiddle.net/pCgYe/7/ 就像这里: jsfiddle.net/pCgYe/7/

I know I have to add something to the js code, but I am not sure exactly what to add. 我知道我必须在js代码中添加一些内容,但是我不确定到底要添加什么。

function isElementInViewport(){
            var scrollTop = $(window).scrollTop();
            var viewportHeight = $(window).height();
            $('.skiller #skill:not(.html5)').each(function(){
                var top = $(this).offset().top;
                console.log(top);
                console.log(scrollTop + viewportHeight);
                if(scrollTop + viewportHeight >= top ){
                    $(this).find('.expand').addClass('html5');
                    console.log(true);
                }else{
                    console.log(false);
                }
            });
        }

$(window).scroll(isElementInViewport);

If anyone can please guide me in the right direction. 如果有人可以指导我正确的方向。

Thank you guys in advance! 预先谢谢你们!

The problem with the code is in the html, all the skill already have an animation, so the new animation won't run. 代码的问题在于html中,所有技能都已经具有动画,因此新动画将无法运行。 I tried this by removing 我尝试通过删除

-moz-animation: css3 2s ease-out;   -webkit-animation: css3 2s ease-out; 

from the css3 class and then the css3 skill had an animation. 从css3类开始,然后css3技能进行了动画处理。 So what you can do is add a class named animate to the element and then in your css removing the animation from the skill classes and add the following for each skill class: 因此,您可以做的是向元素添加一个名为animate的类,然后在CSS中从技能类中删除动画,并为每个技能类添加以下内容:

.jquery.animate      {-moz-animation: jquery 2s ease-out;   -webkit-animation: jquery 2s ease-out;      }

That should work 那应该工作

It's because in your 5 bar version you have already added the css classes straight away and not programmtically through the javascript. 这是因为在您的5 bar版本中,您已经直接添加了CSS类,而不是通过JavaScript编程添加的。 Meaning the animation happens instantly. 这意味着动画会立即发生。

<div class="skiller col">
    <ul id="skill">
        <li><span class="expand html5"><small>%70</small></span><em>HTML
        5</em></li>

        <li><span class="expand css3"><small>%90</small></span><em>CSS
        3</em></li>

        <li><span class=
        "expand jquery"><small>%50</small></span><em>jQuery</em></li>

        <li><span class=
        "expand photoshop"><small>%10</small></span><em>Photoshop</em></li>

        <li><span class=
        "expand dreamweaver"><small>%100</small></span><em>Dreamweaver</em></li>
    </ul>
</div>

Remove the classes after expand on each line and add them in your javascript like you do in the one bar version. 在每一行上展开后删除类,然后像在一个栏式版本中一样将它们添加到您的javascript中。 I'm at work so can't do a proper version of this but try the following javascript: 我正在上班,因此无法对此进行正确的版本处理,但请尝试以下javascript:

function isElementInViewport(){
        var scrollTop = $(window).scrollTop();
        var viewportHeight = $(window).height();
        $('.skiller #skill:not(.html5)').each(function(){
            var top = $(this).offset().top;
            console.log(top);
            console.log(scrollTop + viewportHeight);
            if(scrollTop + viewportHeight >= top ){
                $(this).find('.expand').addClass('html5');
                $(this).find('.expand1').addClass('css3');
                $(this).find('.expand2').addClass('jquery');
                $(this).find('.expand3').addClass('photoshop');
                $(this).find('.expand4').addClass('dreamweaver');
                console.log(true);
            }else{
                console.log(false);
            }
        });
    }

$(window).scroll(isElementInViewport); $(window).scroll(isElementInViewport);

After removing the classes you've added in the HTML you will also need to change the HTML expand classes, Like so: (NOTE: This is a bad implementation but please use it as a basis to greatly improve on). 删除您在HTML中添加的类之后,还需要更改HTML扩展类,如下所示:(注意:这是一个不好的实现,但请以此为基础进行大量改进)。

<div class="skiller col">
    <ul id="skill">
        <li><span class="expand"><small>%70</small></span><em>HTML
        5</em></li>

        <li><span class="expand expand1"><small>%90</small></span><em>CSS
        3</em></li>

        <li><span class=
        "expand expand2"><small>%50</small></span><em>jQuery</em></li>

        <li><span class=
        "expand expand3"><small>%10</small></span><em>Photoshop</em></li>

        <li><span class=
        "expand expand4"><small>%100</small></span><em>Dreamweaver</em></li>
    </ul>
</div>

Check out this fiddle for it working in action: http://jsfiddle.net/pCgYe/9/ 看看这个小提琴在运行中的作用: http : //jsfiddle.net/pCgYe/9/

IMPORTANT: Ideally you want to make your code reusable and extensible. 重要提示:理想情况下,您要使代码可重用和扩展。 My "fix" is a very hackish way of doing it and does not employ best practices. 我的“修复”是一种非常骇人听闻的方法,并且没有采用最佳实践。 This is ok if you're never going to use this again and just need to get it done ASAP, but I would recommend you look into rebuilding this in a far more DRY fashion. 如果您永远不会再次使用它,而只需要尽快完成它,那是可以的,但是我建议您考虑以一种更加干燥的方式来重建它。

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

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