javascript - Move DIV with mouse drag - Stack Overflow

I need the user to be able to move a div to the left or right when they click and hold and move the mou

I need the user to be able to move a div to the left or right when they click and hold and move the mouse, where the position of the div will be equal to the x position of the mouse when it's held down. I know this question has been answered before, but I can't seem to get it to work. I'm working with a simplified version, such that instead of the div moving, I just have "hello" display in the console. Any help is greatly appreciated.

var divEl = document.getElementById("div");

var down = false;

divEl.addEventListener('mousedown', function (event) {
    var down = true;
    console.log("true");
}, true);

window.addEventListener('mouseup', function (event) {
    var down = false;
    console.log("false");
}, true);


document.addEventListener('mousemove', function(event) {
  event.preventDefault();
    if (down) {

      console.log("hello");

    }
}, true);

When I click on the div, I get the "true" console log and when I release the mouse I get the "false" console log. But I can't get the "hello" console log.

I need the user to be able to move a div to the left or right when they click and hold and move the mouse, where the position of the div will be equal to the x position of the mouse when it's held down. I know this question has been answered before, but I can't seem to get it to work. I'm working with a simplified version, such that instead of the div moving, I just have "hello" display in the console. Any help is greatly appreciated.

var divEl = document.getElementById("div");

var down = false;

divEl.addEventListener('mousedown', function (event) {
    var down = true;
    console.log("true");
}, true);

window.addEventListener('mouseup', function (event) {
    var down = false;
    console.log("false");
}, true);


document.addEventListener('mousemove', function(event) {
  event.preventDefault();
    if (down) {

      console.log("hello");

    }
}, true);

When I click on the div, I get the "true" console log and when I release the mouse I get the "false" console log. But I can't get the "hello" console log.

Share Improve this question asked Nov 21, 2019 at 8:12 NormajeanNormajean 1,2753 gold badges17 silver badges34 bronze badges 1
  • Have you tried adding an event listener for the dragend event, possibly to the div in question?: `document.addEventListener('dragend', function (event) { /* ... */ }); – Agi Hammerthief Commented Nov 21, 2019 at 8:17
Add a ment  | 

2 Answers 2

Reset to default 3

You created the new variable inside the callback function. Remove the var and it works:

var divEl = document.getElementById("div");

var down = false;

divEl.addEventListener('mousedown', function (event) {
    down = true;
    console.log("true");
}, true);

window.addEventListener('mouseup', function (event) {
    down = false;
    console.log("false");
}, true);


document.addEventListener('mousemove', function(event) {
  event.preventDefault();
    if (down) {
      console.log("hello");
    }
}, true);

Read more about scope at this link var statement at MDN

I think you're looking something like this,

   var mousePosition;
var offset = [0,0];
var div;
var isDown = false;

div = document.createElement("div");
div.style.position = "absolute";
div.style.left = "0px";
div.style.top = "0px";
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.color = "blue";

document.body.appendChild(div);

div.addEventListener('mousedown', function(e) {
    isDown = true;
    offset = [
        div.offsetLeft - e.clientX,
        div.offsetTop - e.clientY
    ];
}, true);

document.addEventListener('mouseup', function() {
    isDown = false;
}, true);

document.addEventListener('mousemove', function(event) {
    event.preventDefault();
    if (isDown) {
        mousePosition = {

            x : event.clientX,
            y : event.clientY

        };
        div.style.left = (mousePosition.x + offset[0]) + 'px';
        div.style.top  = (mousePosition.y + offset[1]) + 'px';
    }
}, true);

JSFIDDLE

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744685547a4587892.html

相关推荐

  • javascript - Move DIV with mouse drag - Stack Overflow

    I need the user to be able to move a div to the left or right when they click and hold and move the mou

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信