下面的代码尝试使 img 可拖动(关于this),它可以工作,但我不知道如何让 img 移动所有 wrapdrag_wp
一起移动。http://jsfiddle.net/cAeKG/8/有什么建议吗?
js
function enableDraggin(el){
var dragging = dragging || false, x, y, ox, oy, current;
el.onmousedown = function(e){
e.preventDefault();
current = e.target;
dragging = true;
x = e.clientX;
y = e.clientY;
ox = current.offsetLeft;
oy = current.offsetTop;
window.onmousemove = function(e) {
if (dragging == true) {
var sx = e.clientX - x + ox,
sy = e.clientY - y + oy;
current.style.left = sx + "px";
current.style.top = sy + "px";
return false;
}
};
window.onmouseup = function(e) {
dragging && (dragging = false);
};
};
};
var el = $('.drag_wp');
for(var i = 0; i < el.length; i++){
enableDragging(el[i]);
};
html & css
<div class="drag_wp">
<img src="..." class="drag_el">
<div></div>
....// other div
</div>
.drag_wp{
position: absolute;
width: auto;
height: auto;
background:red;
}
.drag_el{
position: absolute;
width: 200px;
height: auto;
}