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!
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.
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:
$(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).
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.
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
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:
That should work