我希望网格元素位于背景上,圆圈位于网格顶部。 Scatter circles功能被注释掉,因为否则网格消失。我不知道为什么....我知道出了什么问题,因为我将圆圈和网格单元格作为子项添加到同一个div中。但我不完全确定。 JSFiddle_Code
任何想法如何去做?谢谢
var arr_circles = [];
var radius = 40;
var cell_side_len = 50;
var grid_width = 400;
var grid_height = 300;
var container = document.getElementById("container");
container.style.border = "solid black";
container.style.width = grid_width+"px";
container.style.height = grid_height+"px";
//create grid
for(var i = 0; i < grid_width/cell_side_len; i++){
for(var j = 0; j < grid_height/cell_side_len; j++){
var cell = document.createElement('div');
cell.style.boxSizing = "border-box";
cell.style.height = cell_side_len + 'px';
cell.style.width = cell_side_len + 'px';
cell.style.border = "1px solid black";
cell.style.float = "left";
cell.style.position = "relative";
container.appendChild(cell);
}
}
//scatter();
function scatter(){
while (container.firstChild) {
container.removeChild(container.firstChild);
}
var numDisks = document.getElementById("numDisks").value;
if( isNaN(numDisks) ) numDisks = 5;
arr_circles = [];
for(i = 0; i < numDisks; i++){
var circle = document.createElement('circle');
circle.style.cssText = ' width: 40px; height: 40px; -webkit-border-radius: 25px; -moz-border-radius: 25px; border-radius: 25px; position: absolute; z-index: 9;';
circle.style.background = "#"+((1<<24)*Math.random()|0).toString(16);
circle.style.left = Math.round(Math.random() * (grid_width - radius*2.5) + radius) + "px";
circle.style.top = Math.round(Math.random() * (grid_height - radius*2.5) + radius) + "px";
arr_circles.push(circle);
container.appendChild(circle);
dragElement(circle);
}
//Make the DIV element draggagle:
//dragElement(document.getElementById(("circle")));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
elmnt.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
}
<input type="text" id="numDisks" value="#ofcircles...">
<button onclick="scatter()">Scatter</button>
<div id="container"></div>
答案 0 :(得分:1)
这是因为您在分散功能开始时删除了网格。只需删除while循环:
var arr_circles = [];
var radius = 40;
var cell_side_len = 50;
var grid_width = 400;
var grid_height = 300;
var container = document.getElementById("container");
container.style.border = "solid black";
container.style.width = grid_width+"px";
container.style.height = grid_height+"px";
//create grid
for(var i = 0; i < grid_width/cell_side_len; i++){
for(var j = 0; j < grid_height/cell_side_len; j++){
var cell = document.createElement('div');
cell.style.boxSizing = "border-box";
cell.style.height = cell_side_len + 'px';
cell.style.width = cell_side_len + 'px';
cell.style.border = "1px solid black";
cell.style.float = "left";
cell.style.position = "relative";
container.appendChild(cell);
}
}
// scatter circles
scatter();
function scatter(){
// while (container.firstChild) {
// container.removeChild(container.firstChild);
// }
var numDisks = document.getElementById("numDisks").value;
if( isNaN(numDisks) ) numDisks = 5;
arr_circles = [];
for(i = 0; i < numDisks; i++){
var circle = document.createElement('circle');
circle.style.cssText = ' width: 40px; height: 40px; -webkit-border-radius: 25px; -moz-border-radius: 25px; border-radius: 25px; position: absolute; z-index: 9;background:red';
circle.style.left = Math.round(Math.random() * (grid_width - radius*2.5) + radius) + "px";
circle.style.top = Math.round(Math.random() * (grid_height - radius*2.5) + radius) + "px";
arr_circles.push(circle);
container.appendChild(circle);
}
}
&#13;
<input type="text" id="numDisks" value="#ofcircles...">
<button onclick="scatter()">Scatter</button>
<div id="container"></div>
&#13;