<script>
var aImg = document.querySelectorAll('img');
drag(aImg[0]);
drag(aImg[1]);
function drag(obj) {
obj.onmousedown = function (ev) {
var ev = ev || window.event;
var sx = ev.offsetX;
var sy = ev.offsetY;
document.onmousemove = function (ev) {
var ev = ev || window.event;
obj.style.left = ev.clientX - sx + 'px';
obj.style.top = ev.clientY - sy + 'px';
};
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
};
return false;
}
}
</script>
<style>
img {
width: 300px;
position: absolute;
}
</style>
拖拽封装
最新推荐文章于 2024-06-14 10:46:49 发布