通过jQuery,HTML,CSS随机移动divs

发布于 2024-11-07 12:57:27 字数 689 浏览 0 评论 0原文

我正在创建一个游戏,您必须关注粒子(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏の忆 2024-11-14 12:57:27
//select all grab items, and since there will be multiple particles
//use a class to mark them, not an ID. This line is to capture all
//particles existing in the markup, that are not dynamically added later
var $particles = $('.grabItem');
//set your container that has all the particles
var $container = $('body');

//every two seconds add a new particle at random location
setInterval(function() {
   for(var i = 1; i < 4; i++) {
      $particles.add(CreateParticle());
      MoveParticles();
   }
}, 2000);

//creates a new particle and adds to the canvas
function CreateParticle(){
   return $('<div/>').addClass('grabItem').appendTo($container);
}

//randomly moves all the particles around
function MoveParticles() {
   $particles.each(function() {
      var x = Math.floor(Math.random() * 990);
      var y = Math.floor(Math.random() * 560);

      $(div).animate({"left": x + "px"}, "slow");
      $(div).animate({"top": y + "px"}, "slow");
   });
}

这将每两秒在随机位置添加一个新粒子,并移动所有现有粒子(包括新粒子)。如果您需要精确的克隆方法,请查看 jQuery 的 .clone() 方法。

//select all grab items, and since there will be multiple particles
//use a class to mark them, not an ID. This line is to capture all
//particles existing in the markup, that are not dynamically added later
var $particles = $('.grabItem');
//set your container that has all the particles
var $container = $('body');

//every two seconds add a new particle at random location
setInterval(function() {
   for(var i = 1; i < 4; i++) {
      $particles.add(CreateParticle());
      MoveParticles();
   }
}, 2000);

//creates a new particle and adds to the canvas
function CreateParticle(){
   return $('<div/>').addClass('grabItem').appendTo($container);
}

//randomly moves all the particles around
function MoveParticles() {
   $particles.each(function() {
      var x = Math.floor(Math.random() * 990);
      var y = Math.floor(Math.random() * 560);

      $(div).animate({"left": x + "px"}, "slow");
      $(div).animate({"top": y + "px"}, "slow");
   });
}

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.

蝶…霜飞 2024-11-14 12:57:27

jQuery 中有一个 Clone 方法,它是您正在寻找的吗?

There exists a Clone method in jQuery, is it what you are looking for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文