【问题标题】:Position 5 divs randomly随机放置 5 个 div
【发布时间】:2018-06-30 04:42:47
【问题描述】:

我完全被困在这里了。我正在尝试创建一个程序,其中有 5 个不同的 div。运行程序时需要将它们放置在随机位置。当我将鼠标拖到它们上方时,它们需要切换位置。到目前为止,我已经做到了。 (这是完全错误的)。

   function Init()
   {
   div = document.getElementById("pixel");
   div = document.getElementById("pixel1");
   div = document.getElementById("pixel2");
   div = document.getElementById("pixel3");
   div = document.getElementById("pixel4");

   spaceW = screen.height - div.height;
   spaceH = screen.width - div.width;
   setTimeout(moveIt, 0);
   }
   function moveIt()
   {
   div.style.top = (100*Math.random()) + "%";
   div.style.left = (100*Math.random()) + "%";
   }

<body onload="Init()">
    <div id="pixel" style="background-color:blue; position:fixed; height:50px; width:50px; font-size:25px;"/>
    <div id="pixel1" style="background-color:green; position:fixed; height:50px; width:50px; font-size:25px;"/>
    <div id="pixel2" style="background-color:orange; position:fixed; height:50px; width:50px; font-size:25px;"/>
    <div id="pixel3" style="background-color:yellow; position:fixed; height:50px; width:50px; font-size:25px;"/>
    <div id="pixel4" style="background-color:red; position:fixed; height:50px; width:50px; font-size:25px;"/>
</body>

有人可以将我推向正确的方向吗,因为这显然行不通。

【问题讨论】:

    标签: javascript html function random


    【解决方案1】:

    我已将您的代码添加到 fiddle:https://jsfiddle.net/x05b5suz/2/

    我想告诉你几件事:

    div = document.getElementById("pixel");
    div = document.getElementById("pixel1"); // This is wrong
    div = document.getElementById("pixel2");
    

    您应该使用var 关键字声明您的变量,并且它们的名称在函数范围内应该是唯一的,否则您只是在覆盖它们。所以你可以将它们命名为var div1、var div2 等等。

    在HTML中,你需要关闭你的&lt;div&gt;...&lt;/div&gt;标签,div不应该被快速关闭。

    希望对你有帮助

    【讨论】:

    • 非常感谢朋友!只是一件事,一你运行程序,所有的div都塞在左上角。有没有可能,所以当你运行程序时,它们已经被随机放置了?现在你必须先将鼠标拖到它上面。我可以使用
    • 在小提琴中,Init() 方法在其定义后被执行,并且 div 被随机放置 onload 事件。在小提琴中,脚本和 html 位于不同的文件中,并且它们的脚本执行 onload 事件在那里工作。我假设您将 html 和脚本放在一个文件中,所以您可以这样做 &lt;body onload="Init()"&gt;
    • 如果您的脚本和 html 在一个文件中,我建议将您的脚本移出到一个新的.js 文件,然后在您的脚本中在底部执行window.onload = Init。并删除&lt;body onload...
    【解决方案2】:

    check out 修复一些语法错误,如 Pal Singh 提及 和

    • 将所有 div(s) 添加到一个数组中以便于控制它
    • 添加 [getRandomNum() , percentwidth() , percentHeight()] 函数以将 div 保留在页面中
    • 添加 moveIt() 函数以在移动时做一些动画

    	function RandomIt()
    	{
    	   var div_array = [];
    	   div_array.push(document.getElementById("pixel"));
    	   div_array.push(document.getElementById("pixel1"));
    	   div_array.push(document.getElementById("pixel2"));
    	   div_array.push(document.getElementById("pixel3"));
    	   div_array.push(document.getElementById("pixel4"));
    	   div_array.forEach(function(entry) {
    	       moveIt(entry,getRandomNum(1,100 - percentwidth(entry)),getRandomNum(1,100 - percentwidth(entry))) 
    		});
    	}
    	function getRandomNum(min, max) {
        return Math.random() * (max - min) + min;
    	}
    	function percentwidth(elem){
        	return ((elem.offsetWidth/window.innerWidth)*100).toFixed(2);
    	}
    	function percentHeight(elem){
        	return ((elem.offsetHeight/window.innerHeight)*100).toFixed(2)+'%';
    	}
    	function moveIt(elem,target_x,target_y) {
    	  var pos_x = 0;
    	  var pos_y = 0;
    	  var id = setInterval(frame, 5);
    	  function frame() {
    	    if (pos_x == target_x && pos_y == target_y) {
    	      clearInterval(id);
    	    } else {
    	    	if(pos_x < target_x){
    				pos_x++; 
    	      		elem.style.left = pos_x + '%'; 
    			}
    			if(pos_y < target_y){
    				pos_y ++;
    				elem.style.top = pos_y + '%';
    			}
    	      
    	       
    	    }
    	  }
       }
    <body onload="RandomIt()">
        <button onclick="RandomIt()">RandomIt</button>
        <div id="pixel" style="background-color:blue; position:fixed; height:50px; width:50px; font-size:25px;"></div>
        <div id="pixel1" style="background-color:green; position:fixed; height:50px; width:50px; font-size:25px;"></div>
        <div id="pixel2" style="background-color:orange; position:fixed; height:50px; width:50px; font-size:25px;"></div>
        <div id="pixel3" style="background-color:yellow; position:fixed; height:50px; width:50px; font-size:25px;"></div>
        <div id="pixel4" style="background-color:red; position:fixed; height:50px; width:50px; font-size:25px;"></div>
    </body>

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 2011-06-13
      • 2015-02-28
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      相关资源
      最近更新 更多