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');
});
精彩评论