如何使用 javascript 移动多个 DOM 元素

How can I move multiple DOM elements using javascript?

本文关键字:DOM 元素 移动 何使用 javascript      更新时间:2023-09-26

我做了一个名为Circle的对象,并且有两个名为circle1circle2的对象的实例,我试图让它们交换位置,但目前只有一个在移动。

Javascript:

var gap = 6, size= 60;
var Circle = function(t,l, color){
        var elem = document.createElement('div');
        t = t,
        l = l;
        elem.setAttribute("class", "circle");
        elem.style.backgroundColor = color;
        // init positions
        elem.style.top  = gap+t*(size+2*gap) + "px";
        elem.style.left = gap+l*(size+2*gap) + "px";
        document.body.appendChild(elem);
        this.getPosition = function(){
            return [t,l];
        };
        this.setPosition = function(arr){
            t = arr[0];
            l = arr[1];
            elem.style.top  = gap+t*(size+2*gap) + "px";
            elem.style.left = gap+l*(size+2*gap) + "px";
        };
}
// make two new circle objects 
var circle1 = new Circle(0, 0, "blue");
var circle2 = new Circle(0, 1, "red");
// we need the circles to swap positions
setTimeout(function(){
    circle1.setPosition(circle2.getPosition());
    circle2.setPosition(circle1.getPosition()); // this is not working
}, 1000);

我把这段代码放在jsFiddle上,让它更容易:http://jsfiddle.net/rhL7671p/

只需缓存第一个圆圈的位置:

setTimeout(function(){
   var pos = circle1.getPosition();
   circle1.setPosition( circle2.getPosition() );
   circle2.setPosition( pos );
}, 1000);

示例小提琴

在此行之后的代码中

circle1.setPosition(circle2.getPosition());
第一个圆的位置

被第二个圆的位置覆盖。因此,下一行不起作用。在 JavaScript 中没有并行执行代码这样的事情,因为只有一个线程(有一些例外......

要规避此问题:首先获取一个(或两个)位置,然后设置它们。

在更新位置之前存储左圆的位置。

setTimeout(function(){
    var c = circle1.getPosition();
    circle1.setPosition(circle2.getPosition());
    circle2.setPosition(c);
}, 1000);

var gap = 6, size= 60;
var Circle = function(t,l, color){
		var elem = document.createElement('div');
        t = t,
        l = l;
    
        elem.setAttribute("class", "circle");
        elem.style.backgroundColor = color;
        elem.style.top  = gap+t*(size+2*gap) + "px";
        elem.style.left = gap+l*(size+2*gap) + "px";
        document.body.appendChild(elem);
        this.getPosition = function(){
        	return [t,l];
        }
        this.setPosition = function(arr){
        	t = arr[0];
            l = arr[1];
        	elem.style.top  = gap+t*(size+2*gap) + "px";
        	elem.style.left = gap+l*(size+2*gap) + "px";
    	}
       	
	}
var circle1 = new Circle(0,0, "blue");
var circle2 = new Circle(0,1, "red");
// we need the circles to swap positions
setTimeout(function(){
    var c = circle1.getPosition();
    circle1.setPosition(circle2.getPosition());
    circle2.setPosition(c);
}, 1000);
console.log(circle2)
.circle{
    width:60px;
    height:60px;
    border-radius: 50px;
    background-color:red;
    position:absolute;
    -webkit-transition-property: top, left;
        -webkit-transition-duration: 0.3s;
}

移动第一个圆圈后,getPosition的结果不是您所期望的,因此请在移动之前缓存它

setTimeout(function(){
    var a = circle2.getPosition(),
        b = circle1.getPosition();
    circle1.setPosition(a);
    circle2.setPosition(b);
}, 1000);

发生这种情况是因为当您尝试获取元素的位置时具有新值。一种可能的解决方案是使用局部变量:

var gap = 6,
  size = 60;
var Circle = function(t, l, color) {
  var elem = document.createElement('div');
  t = t,
    l = l;
  elem.setAttribute("class", "circle");
  elem.style.backgroundColor = color;
  elem.style.top = gap + t * (size + 2 * gap) + "px";
  elem.style.left = gap + l * (size + 2 * gap) + "px";
  document.body.appendChild(elem);
  this.getPosition = function() {
    return [t, l];
  }
  this.setPosition = function(arr) {
    t = arr[0];
    l = arr[1];
    elem.style.top = gap + t * (size + 2 * gap) + "px";
    elem.style.left = gap + l * (size + 2 * gap) + "px";
  }
}
var circle1 = new Circle(0, 0, "blue");
var circle2 = new Circle(0, 1, "red");
// we need the circles to swap positions
setTimeout(function() {
  //use local variables to keep circles position
  var circle1Pos = circle1.getPosition();
  var circle2Pos = circle2.getPosition()
  circle1.setPosition(circle2Pos);
  circle2.setPosition(circle1Pos);
}, 200);
.circle {
  width: 60px;
  height: 60px;
  border-radius: 50px;
  background-color: red;
  position: absolute;
  -webkit-transition-property: top, left;
  -webkit-transition-duration: 0.3s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>