admin管理员组文章数量:1415664
that is my code / (it works) but i want everyone div to be with unique position coordinates. Now script get the first and others move to him place. How can be possible to fix this problem? I tryed a .each function but there wasn't any results. I think this will be easy but i'm to too good in jquery/javascript.
$('#latestblock').animate({
top: newY,
left: newX
}, 500, function() { });
I think here must be in loop but dont know how to do it.
that is my code http://jsfiddle/BREvn/2/ (it works) but i want everyone div to be with unique position coordinates. Now script get the first and others move to him place. How can be possible to fix this problem? I tryed a .each function but there wasn't any results. I think this will be easy but i'm to too good in jquery/javascript.
$('#latestblock').animate({
top: newY,
left: newX
}, 500, function() { });
I think here must be in loop but dont know how to do it.
Share Improve this question asked Jan 15, 2013 at 13:58 Stoyan ZdravkovStoyan Zdravkov 951 silver badge6 bronze badges 3- IDs must be unique on each document, use class instead! – A. Wolff Commented Jan 15, 2013 at 14:02
- you could keep the position of each div in an array, then check if the new random position overlays an existing div, if not position the div, else rerun randomizing code. – ppp Commented Jan 15, 2013 at 14:04
- Do you want the three div's to move simultaneously? – Ferry Kobus Commented Jan 15, 2013 at 14:04
3 Answers
Reset to default 2Something like this?
http://jsfiddle/BREvn/5/
I renamed the id to a class and created an infinite loop. Also send the object into the moveRandom function 'moveRandom(obj)' and after de animation finishes, recall the moveRandom function with itself.
$('.latestblock').each(function() {
moveRandom($(this));
});
You can't give the same id to more than one element.
You could use a class
<div id='container'>
<div class='latestblock'></div>
<div class='latestblock'></div>
<div class='latestblock'></div>
</div>
and then use each
to animate all elements :
$('.latestblock').each(function(){ // <= iterates on all blocks
moveRandom($(this)); // <= pass the block to the moveRandom function
});
Complete demo
Here is the updated and working version: http://jsfiddle/BREvn/4/
An ID must be unique, you cannot have 3 elements with the same id='latestblock'
I also called the function three times and made sure the function was using that parameter passed in.
moveRandom('#latestblock1');
moveRandom('#latestblock2');
moveRandom('#latestblock3');
本文标签: javascriptJquery random position to multiple divsStack Overflow
版权声明:本文标题:javascript - Jquery random position to multiple divs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745234928a2648993.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论