1 obj.offsetTop 是相对于上层非static容器的相对定位吗?
2 jquery当中 obj.offset().top 这个是怎么实现的?是向上递归一个一个控件的算吗?
高手帮忙答一下
------解决方案--------------------
The .offset() method allows us to retrieve the current position of an element relative to the document.
var docElem, win,
box = { top: 0, left: 0 },
elem = this[ 0 ],
doc = elem && elem.ownerDocument;
if ( !doc ) {
return;
}
docElem = doc.documentElement;
// Make sure it's not a disconnected DOM node
if ( !jQuery.contains( docElem, elem ) ) {
return box;
}
// If we don't have gBCR, just use 0,0 rather than error
// BlackBerry 5, iOS 3 (original iPhone)
if ( typeof elem.getBoundingClientRect !== core_strundefined ) {
box = elem.getBoundingClientRect();
}
win = getWindow( doc );
return {
top: box.top + ( win.pageYOffset
------解决方案--------------------
docElem.scrollTop ) - ( docElem.clientTop
------解决方案--------------------
0 ),
left: box.left + ( win.pageXOffset
------解决方案--------------------
docElem.scrollLeft ) - ( docElem.clientLeft
------解决方案--------------------
0 )
};
------解决方案--------------------
$(function() {
$("#p1").click(function(e) {
//offset()获得的是元素的左上角相对于整个网页的坐标
var offset = $("#p1").offset();
$("#MsgShow").append("offset.top:" + $(e.target).offset().top + " offset.left:" + $(e.target).offset().left + " e.pageX:" + e.pageX + " e.pageY:" + e.pageY + "<br/>");
}
);
});