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
2 Answers
Reset to default 3You 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
评论列表(0条)