我怎樣才能得到html/body的絕對屏幕座標? 我發現所有這些window.screenTop,screenLeft,outerheight/innerHeight - 但那不是解決方案。如何用JS獲得文檔的絕對座標?
編輯:我更新了圖像,以澄清我需要什麼!
我怎樣才能得到html/body的絕對屏幕座標? 我發現所有這些window.screenTop,screenLeft,outerheight/innerHeight - 但那不是解決方案。如何用JS獲得文檔的絕對座標?
編輯:我更新了圖像,以澄清我需要什麼!
你可以這樣做(服用一些假設):
//this is the border width of the OS window.
var border= (window.outerWidth-window.innerWidth)/2;
//Assuming the window border is homogeneous and there is nothing in
// the bottom of the window (firebug or something like that)
var menuHeight=(window.outerHeight-window.innerHeight)-border*2;
var absX=window.screenX + border;
var absY=window.screenY+border+menuHeight;
剛剛得到body元素:
var $body = $(this.ie6 ? document.body : document);
,並使用此屬性:$ body.width(),$ body.height()
還得到窗口尺寸
var $window = $(window);
var wWidth = $window.width();
var wHeight = $window.height();
修訂 嘗試使用此:Element.getBoundingClientRect()
var rect = element.getBoundingClientRect();
console.log(rect.top, rect.right, rect.bottom, rect.left);
你可以使用JQuery'scrollLeft'和'scrollTop'函數。 – Hardy
可能的重複http://stackoverflow.com/questions/3714628/jquery-get-the-location-of-an-element-relative-to-window – user1094553
感謝迄今,但不幸的是,這並沒有幫助。 我做了一個新的形象,以便更清楚我需要什麼! – debay