Mr. Developer Mr. Developer - 2 years ago 127
Javascript Question

Jquery each() function increment by half

I'm trying to add

increment after each iteration using jquery
function. I have to add
property on
which value should increase (
) that i can animate them on document load.


<ul class="social-link">


$(' li').each(function(i){
plus = i - 0.5;
$(this).addClass('animated fadeIn').css('animation-delay',plus+'s');

I have tried above code, but it does working after first iteration, on first iteration its given
but after that its adding (1.5, 2.5, 3.5) 1 instead of 0.5. I know my logic is not correct. Can any one guide me regarding this that its possible or not using
function. I can do it using
but it will be good if done using jquery. I will appreciate if someone guide me. Thank You

Answer Source

In your code, i is the index of the collection your selector is returning. So, 1-0.5, 2-0.5, 3-0.5, etc. If I'm understanding you correctly, you want to add .05 every time through, so declare plus outside of .each() like so: var plus = 0; and increment it like so inside the loop: plus += 0.5. It'll increment like this: .5, 1, 1.5, 2, 2.5, etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download