我知道在网上这样做的例子,但每个例子都不同,我自己的实现也是如此。我试图弄清楚我的实施有什么问题,因为它没有按预期工作。
代码段:
var mousePressX = -1;
var mousePressY = -1;
document.getElementById("contextMenu").addEventListener("mousedown", function(event) {
mousePressX = event.clientX;
mousePressY = event.clientY;
}, false);
document.getElementById("contextMenu").addEventListener("mouseup", function(event) {
mousePressX = -1;
mousePressY = -1;
}, false);
document.getElementById("contextMenu").addEventListener("mousemove", function(event) {
if (mousePressX != -1 && mousePressY != -1) {
repositionElement(event.target, event.clientX, event.clientY);
}
}, false);
function repositionElement(element, currentMouseX, currentMouseY) {
element.style.left = element.offsetLeft + (currentMouseX - mousePressX) + 'px';
element.style.top = element.offsetTop + (currentMouseY - mousePressY) + 'px';
}
jsfiddle: http://jsfiddle.net/4rLctko8/
无法弄清楚错误但我注意到如果我更改以下内容:
element.style.left = element.offsetLeft + (currentMouseX - mousePressX) + 'px';
element.style.top = element.offsetTop + (currentMouseY - mousePressY) + 'px';
到:
element.style.left = currentMouseX - mousePressX + 'px';
element.style.top = currentMouseY - mousePressY + 'px';
只有当元素被向正x轴(向右)和正y轴(向下)拖动时才能正确拖动元素,并且当鼠标被触发时它将被正确拖动。将被定位在最左侧最顶角区域附近的某个地方(大约0,0)
答案 0 :(得分:12)
非常有趣,到目前为止只用jQuery完成了它。重新写了一下,并确保每个鼠标移动之后删除了事件监听器 - 这可能是一个内存泄漏,你可能会开始注意到:
var contextmenu = document.getElementById('contextMenu');
var initX, initY, mousePressX, mousePressY;
contextmenu.addEventListener('mousedown', function(event) {
initX = this.offsetLeft;
initY = this.offsetTop;
mousePressX = event.clientX;
mousePressY = event.clientY;
this.addEventListener('mousemove', repositionElement, false);
window.addEventListener('mouseup', function() {
contextmenu.removeEventListener('mousemove', repositionElement, false);
}, false);
}, false);
function repositionElement(event) {
this.style.left = initX + event.clientX - mousePressX + 'px';
this.style.top = initY + event.clientY - mousePressY + 'px';
}
似乎运作良好。 : - )
+++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++
编辑 - 我想我也会添加一个带触摸支持的版本(虽然新设备似乎模仿鼠标事件)。不像Jquery,你可以选择多个事件监听器,所以它主要是重复,只是触摸事件:
http://codepen.io/Shikkediel/pen/VLZKor?editors=011
var object = document.getElementById('element'),
initX, initY, firstX, firstY;
object.addEventListener('mousedown', function(e) {
e.preventDefault();
initX = this.offsetLeft;
initY = this.offsetTop;
firstX = e.pageX;
firstY = e.pageY;
this.addEventListener('mousemove', dragIt, false);
window.addEventListener('mouseup', function() {
object.removeEventListener('mousemove', dragIt, false);
}, false);
}, false);
object.addEventListener('touchstart', function(e) {
e.preventDefault();
initX = this.offsetLeft;
initY = this.offsetTop;
var touch = e.touches;
firstX = touch[0].pageX;
firstY = touch[0].pageY;
this.addEventListener('touchmove', swipeIt, false);
window.addEventListener('touchend', function(e) {
e.preventDefault();
object.removeEventListener('touchmove', swipeIt, false);
}, false);
}, false);
function dragIt(e) {
this.style.left = initX+e.pageX-firstX + 'px';
this.style.top = initY+e.pageY-firstY + 'px';
}
function swipeIt(e) {
var contact = e.touches;
this.style.left = initX+contact[0].pageX-firstX + 'px';
this.style.top = initY+contact[0].pageY-firstY + 'px';
}