你想要做的可以在这段代码中找到(它还支持手指触摸):
var draggables = document.querySelectorAll('.is-draggable');
[].forEach.call(draggables, function (el) {
var startX, startY, initialX, initialY,
auth = function (target) {
var notDraggables = el.querySelectorAll('.is-not-draggable');
return [].filter.call(notDraggables, function (element) {
return target !== element;
}).length > 0;
};
function move(gesture) {
var deltaGestureX = gesture.clientX - initialX,
deltaGestureY = gesture.clientY - initialY,
deltaPositionX = startX + deltaGestureX,
deltaPositionY = startY + deltaGestureY,
limitX = parseInt(window.innerWidth - el.clientWidth, 10),
limitY = parseInt(window.innerHeight - el.clientHeight, 10);
if (deltaPositionY <= 0) {
el.style.top = '0px';
} else if (deltaPositionY >= limitY) {
el.style.top = limitY + 'px';
} else {
el.style.top = startY + deltaGestureY + 'px';
}
if (deltaPositionX <= 0) {
el.style.left = '0px';
} else if (deltaPositionX >= limitX) {
el.style.left = limitX + 'px';
} else {
el.style.left = startX + deltaGestureX + 'px';
}
return false;
}
function mousemove(e) {
move(e);
}
function mouseup() {
document.removeEventListener('mousemove', mousemove);
document.removeEventListener('mouseup', mouseup);
}
function touchmove(e) {
move(e.touches[0]);
}
function touchend() {
document.removeEventListener('touchmove', touchmove);
document.removeEventListener('touchend', touchend);
}
el.addEventListener('touchstart', function (e) {
if (auth(e.target)) {
startX = el.offsetLeft;
startY = el.offsetTop;
initialX = e.touches[0].clientX;
initialY = e.touches[0].clientY;
document.addEventListener('touchmove', touchmove);
document.addEventListener('touchend', touchend);
}
});
el.addEventListener('mousedown', function (e) {
if (auth(e.target)) {
startX = el.offsetLeft;
startY = el.offsetTop;
initialX = e.clientX;
initialY = e.clientY;
document.addEventListener('mousemove', mousemove);
document.addEventListener('mouseup', mouseup);
return false;
}
});
});
查看演示:http://plnkr.co/edit/d6wVpTDcGDxLHAwkAVgD?p=preview