javascript - 如何通过单击和拖动动态移动 Div

标签 javascript html css

我想在点击后根据鼠标在父div中的位置移动div。当我们离开鼠标指针时,然后设置div位置。 我到处搜索,这让我找到了做同样事情的过于复杂的方法,并且涉及到 j-query 的使用。我需要严格使用 javascript 来完成我要做的事情。

这是CSS代码

body {
    margin: 0px;
    padding: 0px;
}
.crop-container{
    width: 500px;
    height: 500px;
    border:5px solid black;
    position: relative;
}
.crop-lense{
    width: 100px;
    height: 100px;
    border: 5px dotted black;
    position: relative;
    z-index: 10;
    resize: both;
    background-color: transparent;
}

html代码

<div class="crop-container" id="container" onmousemove="showCoords(event)">
    <div class="crop-lense" id="lense">

    </div>
</div>

这是javascript代码

var lense = document.getElementById('lense');
var container = document.getElementById("container");
var lensemflag = false;
var x;
var y;


lense.addEventListener('mousedown',function(){
    lensemflag = true;
    console.log(lensemflag);
});

lense.addEventListener('mouseup',function(){
    lensemflag = false;
    console.log(lensemflag);
});



function showCoords(event) {
    x = event.offsetX;
    y = event.offsetY;
    y = y - 50;
    x = x - 50;
}

if(lensemflag==true){
    setInterval(function() {
        lense.style.top = y + 'px';
        lense.style.left = x + 'px';
        }, 
    1);
}

最佳答案

最短答案:

var lense = document.getElementById("lense");
var container = document.getElementById("container");
var x;
var y;

lense.addEventListener("mousedown", function() {
  lense.addEventListener("mousemove", showCoords)
});

lense.addEventListener("mouseup", removeListener);
lense.addEventListener("mouseout", removeListener);

function removeListener() {
  lense.removeEventListener("mousemove", showCoords)
}

function showCoords(event) {
 if(container.offsetWidth >= event.pageX + lense.offsetWidth / 2 + 10 &&
   container.offsetHeight >= event.pageY + lense.offsetHeight / 2 + 10 &&
   event.pageX - lense.offsetWidth / 2 > 0 &&
   event.pageY - lense.offsetHeight / 2 > 0){
    x = event.pageX - lense.offsetWidth / 2;
    y = event.pageY - lense.offsetHeight / 2;
    lense.style.top = y + "px";
    lense.style.left = x + "px";
  }
}
 body {
            margin: 0px;
            padding: 0px;
          }
          .crop-container{
            width: 500px;
            height: 500px;
            border:5px solid black;
            position: relative;
          }
          .crop-lense{
            width: 100px;
            height: 100px;
            border: 5px dotted black;
            position: relative;
            z-index: 10;
            resize: both;
            background-color: transparent;
            t
          }
<div class="crop-container" id="container">
  <div class="crop-lense" id="lense">

  </div>
</div>

关于javascript - 如何通过单击和拖动动态移动 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114232/

相关文章:

android - 'meta name="google-play-app"' 工作吗?

html - 如何让 html 元素覆盖并超出 CSS "column"

javascript - 在滚动函数 jQuery 上添加 CSS 属性

javascript - 打开弹出窗口并在用户单击弹出窗口外的任何位置时将其隐藏

javascript - 独立使用 AngularJS 指令和 $scope

javascript - 如何通过按钮替换图像以在javascript中调用onclick函数

html - 如何在下拉菜单中更改 OnMouseHover 上的文本颜色

html - 在 VS 预制样式中对齐 DIV 部分

javascript - 天然气计算器jquery应用程序

javascript - 使用 JSON API 根据用户点击事件检索数据