通过jQuery,HTML,CSS随机移动divs
我正在创建一个游戏,您必须关注粒子(Divs),然后单击它们“吃”它们。我目前遇到的问题是,我找不到一种克隆每个div的方法,并给它一个随机的x和y坐标值以将其定位。
这是我的代码:
var x = e.pageX;
var y = e.pageY;
function reposition(div, x, y, randomMode) {
if(randomMode == 1) {
x = Math.floor(Math.random() * 990);
y = Math.floor(Math.random() * 560);
}
$(div).animate({"left": x + "px"}, "slow");
$(div).animate({"top": y + "px"}, "slow");
}
// need to find some way to duplicate the divs and move them in random directions
setInterval(function() {
for(var i = 1; i < 4; i++) {
reposition("#grabItem", 0, 0, 1);
}
}, 2000);
I am creating a game where you have to follow particles (divs) and click on them to "eat" them. The issue I am having currently is that I can't find a way to clone each div and give it a random X and Y coordinate value to position it.
Here is my code:
var x = e.pageX;
var y = e.pageY;
function reposition(div, x, y, randomMode) {
if(randomMode == 1) {
x = Math.floor(Math.random() * 990);
y = Math.floor(Math.random() * 560);
}
$(div).animate({"left": x + "px"}, "slow");
$(div).animate({"top": y + "px"}, "slow");
}
// need to find some way to duplicate the divs and move them in random directions
setInterval(function() {
for(var i = 1; i < 4; i++) {
reposition("#grabItem", 0, 0, 1);
}
}, 2000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将每两秒在随机位置添加一个新粒子,并移动所有现有粒子(包括新粒子)。如果您需要精确的克隆方法,请查看 jQuery 的 .clone() 方法。
That will add a new particle at a random location every two seconds and move all existing particles around (including the new one). If you need an exact clone method check out jQuery's .clone() method.
jQuery 中有一个 Clone 方法,它是您正在寻找的吗?
There exists a Clone method in jQuery, is it what you are looking for?