JavaScript 实现弹框拖拽
on Front-End
const Drag = {
params: {
left: 0,
top: 0,
currentX: 0,
currentY: 0,
flag: false
},
getCss (o, key) {
return o.currentStyle ? o.currentStyle[key] : document.defaultView.getComputedStyle(o, false)[key];
},
drag (bar, target, callback) {
var self = this;
if (self.getCss(target, "left") !== "auto") {
self.params.left = self.getCss(target, "left");
}
if (self.getCss(target, "top") !== "auto") {
self.params.top = self.getCss(target, "top");
}
bar.onmousedown = function (event) {
self.params.flag = true;
if (!event) {
event = window.event;
bar.onselectstart = function () {
return false;
}
}
var e = event;
self.params.currentX = e.clientX;
self.params.currentY = e.clientY;
};
document.onmouseup = function () {
self.params.flag = false;
if (self.getCss(target, "left") !== "auto") {
self.params.left = self.getCss(target, "left");
}
if (self.getCss(target, "top") !== "auto") {
self.params.top = self.getCss(target, "top");
}
};
document.onmousemove = function (event) {
var e = event || window.event;
if (self.params.flag) {
var nowX = e.clientX, nowY = e.clientY;
var disX = nowX - self.params.currentX, disY = nowY - self.params.currentY;
var left = parseInt(self.params.left) + disX;
var right = document.body.clientWidth - target.offsetWidth;
var top = parseInt(self.params.top) + disY;
var bottom = document.body.clientHeight - target.offsetHeight;
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
if (top <= scrollTop) {
target.style.top = scrollTop + "px";
} else if (top >= (scrollTop + bottom)) {
target.style.top = scrollTop + bottom + "px";
} else {
target.style.top = top + "px";
}
if (left <= scrollLeft) {
target.style.left = scrollLeft + "px";
} else if (left >= (scrollLeft + right)) {
target.style.left = scrollLeft + right + "px";
} else {
target.style.left = left + "px";
}
if (event.preventDefault) {
event.preventDefault();
}
return false;
}
}
}
}
module.exports = Drag;