慕仔9503151
2019-04-17 14:58
var oBox = document.querySelector(".box");
var owrap = document.querySelector("#wrap");
oBox.onmousedown = function(ev) {
var ev = window.event || ev;
var disX = ev.clientX - this.offsetLeft;
var disY = ev.clientY - this.offsetTop;
document.onmousemove = function(ev) {
var ev = window.event || ev;
var L = ev.clientX - disX;
var T = ev.clientY - disY;
if (L < 0) {
L = 0
} else if (L > owrap.offsetWidth - oBox.offsetWidth) {
L = owrap.offsetWidth - oBox.offsetWidth
}
if (T < 0) {
T = 0
} else if (T > owrap.offsetWidth - oBox.offsetHeight) {
T = owrap.offsetWidth - oBox.offsetHeight;
}
oBox.style.left = L + "px";
oBox.style.top = T + "px";
}
document.onmouseup = function() {
document.onmousemove = null;
document.onmouseup = null;
}
}
这应该是要实现拖拽吧,你还想理解什么
把代码一个字一个标点的去理解,多问。都是这样过来的
JavaScript入门篇
739817 学习 · 9566 问题
相似问题