Search code examples
jqueryloopsattr

Loop for custom attr


I'm looking for a loop that will count like that

1 , 0.8 , 0.6, 0.4 , 0.2 , 0.2 , 0.4 , 0.6 , 0.8 , 1

and append to custom attr "data-depth" with li element i got so far something like that and I cant move on

for (var i = 0; i < 5; i++) {
    $tiles.eq(i).attr('data-depth', -(i*0.2-1));
}

then it return first part "1 , 0.8 , 0.6, 0.4 , 0.2 "

Anyone can help me?


Solution

  • A repeating version...

    http://jsfiddle.net/2wb9koec/1/

    var count = 0;
    var i = 1.2;
    var ul = jQuery("ul");
    var step = -0.2;
    
    while(count<50) {
        if(i===0.2) {
            if(step<0) {
                i = 0;
                step = 0.2;
            }
        } else if(i>=1) {
            if(step>0) {
                i = 1.2;
                step = -0.2
            }
        }
    
        i = Math.round((i+step)*100)/100;
    
        ul.append("<li>"+i+"</li>");
    
        count++;
    }