You can find jsFiddle demo here
正如你可能看到的畫面我試圖對準中間的圓圈(DIV,綠色)到另一個圈子(DIV,灰色) 。我計算了兩個div的中心並使它們相等,但小綠圈仍然不在中間。
哪裏錯了嗎?我無法找到它。
我用它來對準圈(其中o
是綠色圓圈的jQuery和$(this)
是灰色的:
$.fn.center = function(o) {
var _X = parseInt(o.css('left')) + parseInt(o.width())/2 - parseInt($(this).width())/2;
var _Y = parseInt(o.css('top')) + parseInt(o.height())/2 - parseInt($(this).height())/2;
$(this).offset({ top: _Y, left: _X });
};
預先感謝您的任何幫助
我建議使用jQuery UI的[position](http://jqueryui.com/position/)方法。它可以讓你定位任何元素相對於任何其他元素,並抽象出所有的複雜情況。 –
謝謝你的快速答覆,這個問題解決了:) – Pho3nixHun
+1的解決方案添加到 –