似乎 jquery draggable 需要将处理程序放在可拖动的内部。只需几行代码,您就可以编写自己的可拖动对象。在这里查看我的演示
HTML:
<div id="container">
<img id="image" src="http://thechive.files.wordpress.com/2011/03/hot-sexy-redheads-12.jpg" />
<div id="viewport"></div>
</div>
JS:
$(document).ready(function(){
var isDragging,
top = 0, left = 0,
curX, curY;
$("#image").mousedown(function (e) {
e.preventDefault();
});
$("#container").mousedown(function (e) {
isDragging = true;
curX = e.pageX;
curY = e.pageY;
left = Number($("#image").css("margin-left").
toString().replace("px", ""));
top = Number($("#image").css("margin-top").
toString().replace("px", ""));
});
$(document).mouseup(function () {
if (isDragging){
// reset
isDragging = false;
top = 0;
left = 0;
}
});
$("#container").mousemove(function(e){
if (!isDragging) {
return;
}
left += e.pageX - curX;
top += e.pageY - curY;
// set the position
$("#image").css("margin-left", left + "px").
css("margin-top", top + "px");
curX = e.pageX;
curY = e.pageY;
});
});