0

I have the following code, it is working, but I'd like to "automatize" it with a javascript function, so I won't have to write it over and over again.

$("#portfolio").waypoint(function() {

    $('.portfolio-item').eq(0).css("animation-delay","0.0s");
    $('.portfolio-item').eq(1).css("animation-delay","0.3s");   
    $('.portfolio-item').eq(2).css("animation-delay","0.6s");
    $('.portfolio-item').eq(3).css("animation-delay","0.9s");
    $('.portfolio-item').eq(4).css("animation-delay","1.2s");
    $('.portfolio-item').eq(5).css("animation-delay","1.5s");
    $('.portfolio-item').eq(6).css("animation-delay","1.8s");
    $('.portfolio-item').eq(7).css("animation-delay","2.1s");
    $('.portfolio-item').eq(8).css("animation-delay","2.4s");

    $('.portfolio-item').addClass('animated fadeInUp'); 

}, { offset: 400}); 

Thanks!

3
  • Please post your own code that you tried to do what you need. Commented Jul 18, 2013 at 5:59
  • Just put it inside a function definition, what's the problem? Commented Jul 18, 2013 at 5:59
  • And use a loop for all that repeated animation-delay code. Commented Jul 18, 2013 at 5:59

2 Answers 2

1

An alternative to SidCool's answer would be to use jQuery each to prevent using eq :

$('#portfolio').waypoint(function(){
    var $items=$('.portfolio-item');
    $items.each(function(i){
        $(this).css('animation-delay', (i*0.3)+"s");
    });
    $items.addClass('animated fadeInUp');
}, {offset: 400});

Though, I just realized that he suggested it earlier. Credits to him :-)

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure if this will work, but worth giving a try:

$("#portfolio").waypoint(function() {
    for(var i=0;i<9;i++){
            var j = (i*0.3)+"s";
            $('.portfolio-item').eq(i).css("animation-delay",j);
    }
    $('.portfolio-item').addClass('animated fadeInUp'); 

}, { offset: 400}); 

5 Comments

I am glad! You can make it more dynamic by using the each function of jQuery.
Yes, I thinked about it but wasn't sure about how to use it.. If you have a minute, can you provide a sample with it?
@Frederik.L has given a more elegant solution. Please accept his answer as the correct one by clicking the tick symbol below the voting links against his response.
@SidCool Thanks! Yours is good too, more efficient than mine, but efficiency always come with the price of scalability.
That's right @Frederik.L, but given the number of elements, I would rather go for a cleaner and more elegant approach than a supposedly efficient one :) So your solution is better in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.