开发者

Jquery each interation problem

开发者 https://www.devze.com 2023-04-04 06:33 出处:网络
I got this random position script. But it works only on the first image... What I\'m doing wrong? var randnumsX = [1,2,3,4,5,6,7,8];

I got this random position script. But it works only on the first image... What I'm doing wrong?

var randnumsX = [1,2,3,4,5,6,7,8];
var randnumsY = [1,2,3,4,5,6];

$('#obra img').each(function(i,el) {

    m = Math.floor(Math.random()*randnumsX.length);
    randnumsX = randnumsX.splice(m,1);
    posx = Math.floor(m * 50);

    n = Math.floor(Math.random()*randnumsY.length);
    randnumsY = randnumsY.splice(n,1);
    posy = Math.floor开发者_Python百科(n * 50);

    $(el).css({position:'absolute', left: posx + 155, top: posy});      
    $(el).fadeIn('slow');

}); 


splice returns the removed element not the array with the element removed.


If you are accessing the div then you won't need # sign

$('div img').each(function(i,el) {

m = Math.floor(Math.random()*randnumsX.length);
randnumsX = randnumsX.splice(m,1);
posx = Math.floor(m * 50);

n = Math.floor(Math.random()*randnumsY.length);
randnumsY = randnumsY.splice(n,1);
posy = Math.floor(n * 50);

$(el).css({position:'absolute', left: posx + 155, top: posy});      
$(el).fadeIn('slow');

});

0

精彩评论

暂无评论...
验证码 换一张
取 消