如何使每个新的div可拖动?

时间:2017-10-18 13:56:44

标签: javascript html

我想请求以下帮助。基本上,每次按下按钮,我都会尝试新<div>。然后,我想添加一个函数,当我点击它时,每个新的<div>都可以被拖动和移动,当我释放鼠标时它会掉落(每个新的<div>应该单独移动)。我尝试了很多东西,但是所有这些东西似乎都运转不好。我得到的结果是,只有第一个<div>正在移动或所有<div>一起移动。我没有添加我的移动功能,因为它没有很好的安排(很难理解,因为我尝试了很多东西)。我也不允许使用JQuery。

你能告诉我该做什么吗?谢谢。

<html>
<body>
    <button id="mybutton">Create a new moveabl div</button>
    <script>
        var create = document.createElement("div");
        create.id = "dragm2";
        document.body.appendChild(create);
        var count = 0;
        var text = document.createTextNode("mu yu ");
        document.getElementById("mybutton").addEventListener("click", function() {
            count++;
            var create = document.createElement("div");
            create.id = "dragme" + count;
            create.style.background = "green";
            create.style.height = "170px";
            create.style.width= "70px";
            create.appendChild(text);
            document.body.appendChild(create);
        });

        document.body.addEventListener("click", function(event) {
            console.log(event.target.nodeName);
            console.log(event.target.id);
            var movenow = event.target.id;
            if (movenow !== null && event.target.nodeName !== "BUTTON" ) {
                document.getElementById(movenow).addEventListener('mousedown', hold, false);
                document.body.addEventListener('mouseup', release, false);
                function hold() {
                    document.getElementById(movenow).addEventListener('mousemove', move, true);
                    document.body.addEventListener('mousemove', move, true);
                }
                // ==> On Realease Function <==
                function release() {
                    document.getElementById(movenow).removeEventListener('mousemove', move, true);
                    document.body.removeEventListener('mousemove', move, true);
                }
                function move(event) {
                    var epY = event.clientY;
                    var epX = event.clientX;

                    if (document.querySelectorAll("input[name='revertxy']:checked").length >= 1) {
                        epY = event.clientX;
                        epX = event.clientY;
                    }

                    event.target.style.position = 'absolute';
                    event.target.style.top = epY + 'px';
                    event.target.style.left = epX + 'px';
                }
            }
        });
    </script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

希望这会有所帮助,

const btn = document.querySelector('#btn');

btn.addEventListener("click", function() {
  const div = document.createElement("div");

  div.className = 'draggable';

  div.addEventListener('mousedown', mouseDown, false);
  document.body.appendChild(div);

});

function mouseUp() {
  this.removeEventListener('mousemove', move, true);
  this.classList.remove('grabbing');
}

function mouseDown(e) {
  this.addEventListener('mousemove', move, true);
  this.addEventListener('mouseup', mouseUp, true);
  this.classList.add('grabbing');
}

function move(e) {
  this.style.position = 'absolute';
  this.style.top = getMousePosition(e).client.y - (this.clientWidth / 2) + 'px';
  this.style.left = getMousePosition(e).client.x - (this.clientHeight / 2) + 'px';
}

function getMousePosition(evt) {

  var pageX = evt.pageX;
  var pageY = evt.pageY;
  if (pageX === undefined) {
    pageX = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    pageY = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }

  var rect = evt.target.getBoundingClientRect();
  var offsetX = evt.clientX - rect.left;
  var offsetY = evt.clientY - rect.top;

  return {
    client: {
      x: evt.clientX,
      y: evt.clientY
    }
  };
}
.draggable {
  width: 100px;
  height: 100px;
  margin: 5px;
  background: #333;
}
<button id="btn">Create a new moveabl div</button>

答案 1 :(得分:1)

<head>
    <style>
    body { position: relative}
    div {
        width:100px; height: 100px;
        background:#f00;
        position: absolute;
    }
    </style>
</head>
<body>
        <button id = "ayoo">Create a new moveabl div</button>

<script>

 var count = 0
 var moving=false; 
 var mx,my;

document.getElementById('ayoo').addEventListener('click', function () {
        count++
        var mydiv = document.createElement("div")
        mydiv.id= "aya"+count
        document.body.appendChild(mydiv)

        mydiv.addEventListener('mousedown', function (e) {
            moving = true;
            this.style.backgroundColor="#00ff00";
        });
        mydiv.addEventListener('mousemove', function (e) {
            if(moving) {
                getmouse(e);
                this.style.top = my - 50;
                this.style.left = mx - 50;
            }

        });
        mydiv.addEventListener('mouseup', function (e) {
            moving = false;
            this.style.backgroundColor="#ff0000";
        });
});

function getmouse(e){
    if (e.pageX || e.pageY)
    { 
      mx = e.pageX;
      my = e.pageY;
    }
    else if (e.clientX || e.clientY)
    { 
      mx = e.clientX + document.body.scrollLeft;
      my = e.clientY + document.body.scrollTop;
    }  
}

</script>

</body>