【发布时间】:2013-01-29 09:06:33
【问题描述】:
下面的代码尝试使img可拖动(关于this),它可以工作,但我不知道如果img move all wrap drag_wp move together. 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;
}
【问题讨论】:
-
请注意,您有一个错字:函数名为
enableDraggin,在for循环中您有enableDragging(末尾为g)
标签: javascript jquery