Javascript移动div元素中的所有子元素

问题描述 投票:0回答:1

我正在尝试创建一个简单的拖动函数,根据鼠标移动移动div标签内的所有子项,在简单的世界中,我计算deltaX和deltaY,并通过更改style.top和style.left将它们应用于div中的所有子项。至于X坐标,它运作良好,而Y不起作用(只增加),我无法解释原因。这就是我所做的

//Make the DIV element draggagle:
dragElement(document.getElementById(("mydiv")));

function dragElement(elmnt) {
  var deltaX = 0, deltaY = 0, initX = 0, initY = 0;
  var flag=true;
  elmnt.onmousedown = dragMouseDown;


  var childrens;
  function dragMouseDown(e) {
    e = e || window.event;
    //console.log("dragMouseDown: "+e.clientX+" "+e.clientY);
    
    childrens = document.getElementById("mydiv").querySelectorAll(".child");
    
    // get the mouse cursor position at startup:
    initX = e.clientX;
    initY = e.clientY;
    document.onmouseup = closeDragElement;
	
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {

    if(flag){
		flag=false;
		
		e = e || window.event;
			
		// calculate the new cursor position:
		deltaX = e.clientX-initX;
		deltaY = e.clientY-initY;

		console.log("deltaX: "+deltaX+" deltaY: "+deltaY);
		
		
		for (var i = 0; i < childrens.length; i++) {
		
			//console.log("childrens[i].offsetTop: "+childrens[i].offsetTop+" childrens[i].offsetLeft: "+childrens[i].offsetLeft);
			childrens[i].style.top = (childrens[i].offsetTop + deltaY) + "px";	// dont work (only increase)
			childrens[i].style.left = (childrens[i].offsetLeft + deltaX) + "px";
		}
		
		initX = e.clientX;
		initY = e.clientY;
		deltaX=0;
		deltaY=0;
		
		flag=true;
	}

  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
#mydiv {
    position: fixed;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
	width:400px;
	height:400px;
    border: 1px solid #d3d3d3;
}

.child {
	position: relative;
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1>Draggable DIV Element</h1>

<p>Click and hold the mouse button down while moving the DIV element</p>

<div id="mydiv" style="background-color:blue">
  <p class="child" style="background-color:red">Move 1</p>
</div>
javascript jquery html draggable
1个回答
1
投票

尝试这样的事情:

dragElement(document.getElementById(("mydiv")));

function dragElement(elmnt) {
  var deltaX = 0,
    deltaY = 0,
    initX = 0,
    initY = 0;
  var flag = true;
  elmnt.onmousedown = dragMouseDown;

  var childrens;


  function dragMouseDown(e) {
    e = e || window.event;
    childrens = document.getElementById("mydiv").querySelectorAll(".child");

    for (var i = 0; i < childrens.length; i++) {
      childrens[i].style.top = childrens[i].style.top == "" ? "0px" : childrens[i].style.top;
      childrens[i].style.left = childrens[i].style.left == "" ? "0px" : childrens[i].style.left;
    }

    initX = e.clientX;
    initY = e.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    if (flag) {
      flag = false;

      e = e || window.event;

      deltaX = e.clientX - initX;
      deltaY = e.clientY - initY;

      for (var i = 0; i < childrens.length; i++) {
        childrens[i].style.top = parseInt(childrens[i].style.top) + deltaY + "px";
        childrens[i].style.left = parseInt(childrens[i].style.left) + deltaX + "px";
      }

      initX = e.clientX;
      initY = e.clientY;

      flag = true;
    }
  }

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
#mydiv {
  position: fixed;
  z-index: 9;
  background-color: #f1f1f1;
  text-align: center;
  width: 400px;
  height: 400px;
  border: 1px solid #d3d3d3;
}

.child {
  position: relative;
  padding: 10px;
  cursor: move;
  z-index: 10;
  background-color: #2196F3;
  color: #fff;
}
<h1>Draggable DIV Element</h1>

<p>Click and hold the mouse button down while moving the DIV element</p>

<div id="mydiv" style="background-color:blue">
  <p class="child" style="background-color:red">Move 1</p>
  <p class="child" style="background-color:yellow">Move 2</p>
</div>

这不是一个好的解决方案,但它的工作原理。考虑使用jQuery和jQuery Draggable

© www.soinside.com 2019 - 2024. All rights reserved.