2

I have content on a page and half way through I have skill bars that are animated with 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. shown here: jsfiddle.net/pCgYe/6/

But when I added more bars, it stopped working completely. Like here: jsfiddle.net/pCgYe/7/

I know I have to add something to the js code, but I am not sure exactly what to add.

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!

Surge
  • 57
  • 3
  • 10

2 Answers2

0

The problem with the code is in the html, all the skill already have an animation, so the new animation won't run. 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. 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:

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

That should work

Terry
  • 332
  • 1
  • 15
0

It's because in your 5 bar version you have already added the css classes straight away and not programmtically through the 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. I'm at work so can't do a proper version of this but try the following 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);

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).

<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/

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.

BenM
  • 4,218
  • 2
  • 31
  • 58
  • I tried doing that, but they all expand to 100% like the last bar? (I am a little new to js, so bare with me) :/ – Surge Aug 27 '13 at 10:14
  • Hi Surge. Please see my edited original post. it's not perfect, and it will expand them all, but only if you scroll down to them. Make sure to remove the addiitonal classes from the HTML first. – BenM Aug 27 '13 at 10:20
  • I removed the classes from HTML and I added the updated js code, but it is still doing the same thing. All the bars are expanding to %100. – Surge Aug 27 '13 at 10:30
  • I've added in a new fiddle link for you to look at with it working. http://jsfiddle.net/pCgYe/9/ – BenM Aug 27 '13 at 10:31
  • Oh you think the issue was because they all had the same class "expand" thats why it wasnt working? – Surge Aug 27 '13 at 10:37
  • Yeah it looks to be that way. As the css for the expand class by default just creates a bar with an orange background as a whole. – BenM Aug 27 '13 at 10:41
  • Thanks a lot man! Really appreciate all the help. I kinda fixed the classes; for anyone that is interested or has the same/similar issue, here is the updated link: [jsfiddle.net/pCgYe/10/](http://jsfiddle.net/pCgYe/10/) – Surge Aug 27 '13 at 10:51
  • No problem! I'll be cheeky now and ask if you can mark my answer as correct/solved? I haven't had one yet so I'm excited haha. – BenM Aug 27 '13 at 12:39