为什么不将两个 div 相互添加,只将拖动事件附加到外部(圆圈)?那么它将始终保持在中心,而无需在 javascript 中付出很大的努力,如下所示:
http://jsfiddle.net/2ebnz/
HTML:
<div id="field">
<div id="cirlce1" class="circlle">
<img id="ninjaimage1" class="Ninjaimg" src="http://www.boursematch.com/assets/images/avatar_default.gif"></img>
</div>
</div>
CSS:
.Ninjaimg {
position:relative;
margin-top: 60px;
margin-left: 60px;
width:30px;
height:30px;
display:block;
cursor:pointer;
z-index:3000;
}
.circlle {
position: absolute;
width: 150px;
height: 150px;
-moz-border-radius: 50%;
border-radius: 50%;
border:1px solid black;
position: absolute;
z-index:1;
display:block;
}
JS:
$(document).ready(function () {
$("#cirlce1").draggable();
});