COSTADOR
COSTADOR

Reputation: 317

How to rewrite this code from 'each' to 'for'?:

How to rewrite this code from 'each' to 'for':

myDiv.scroll(function () { 
    $nower = (($(this).scrollTop() + $start_pr) / $skorost) + $ugol * 8;
    $.each(ImgDiv, function(index) {
        $(this).offset({ top: Math.cos((index + 1) * $ugol + $nower) * $size_dug + $smes_y, left: Math.sin(-((index + 1) * $ugol + $nower)) * $size_dug + $smes_x });
    });
});

I'm trying, but it's doesn't work: =(

myDiv.scroll(function () {  
    var $nower = (($(this).scrollTop() + $start_pr) / $skorost) + $ugol * 8;
    for (var ink = 0, len = ImgDiv.length; ink < len; ink++) {
        ImgDiv[ink].offset({top: Math.cos((ink + 1) * $ugol + $nower) * $size_dug + $smes_y, left: Math.sin(-((ink + 1) * $ugol + $nower)) * $size_dug + $smes_x });
    };
});

Upvotes: 1

Views: 87

Answers (1)

Adil
Adil

Reputation: 148178

You are probably trying to call jQuery function offset on DOM object instead of jQuery object. Convert DOM object to jQuery object as under.

Change

ImgDiv[ink]

To

$(ImgDiv[ink])

Your code would be

myDiv.scroll(function () {  
        var $nower=(($(this).scrollTop()+$start_pr)/$skorost)+$ugol*8;
        for (var ink=0, len = ImgDiv.length; ink < len; ink++)
        {
        $(ImgDiv[ink]).offset({top: Math.cos((ink+1)*$ugol+$nower)*$size_dug+$smes_y, left: Math.sin(-((ink+1)*$ugol+$nower))*$size_dug+$smes_x });
        };
    });

Upvotes: 2

Related Questions