1 回答
TA贡献1820条经验 获得超2个赞
很难说这有效,因为它很难产生错误,所以请让我知道这有效。我所做的只是
document.addEventListener("mouseup", function() {
mover = false;
first = true;
});
let elements = document.querySelectorAll('.draggable');
elements.forEach(function(el) {
let mover = false,
x, y, posx, posy, first = true;
el.onmousedown = function() {
mover = true;
};
document.addEventListener("mouseup", function() {
mover = false;
first = true;
});
/*
document.onmouseup = function() {
mover = false;
first = true;
};
*/
el.parentNode.onmousemove = function(e) {
el.style.cursor = "move";
if (mover) {
if (first) {
x = e.offsetX;
y = e.offsetY;
first = false;
}
posx = e.pageX - x;
posy = e.pageY - y;
el.style.left = posx + 'px';
el.style.top = posy + 'px';
}
}
});
.draggable {
position: absolute;
z-index: 1;
top: 100px;
}
<section class="dragscroll">
<div class="draggable">
<textarea></textarea>
</div>
</section>
<section class="dragscroll">
<div class="draggable">
<textarea></textarea>
</div>
</section>
添加回答
举报