2013-01-04 39 views
0

我有一個div,絕對定位,最初不可見,顯示在被點擊的元素渲染其預覽的位置(預覽的頂部位置排列在元素頂部點擊)。 當被點擊的元素位置較低時,預覽會呈現在原始頁面邊界下方,並且需要滾動。我想要向上移動預覽,使其底部邊緣位於上一頁的底部邊界。問題是我使用的代碼沒有返回頁面高度的預期值(它大於預覽高度和單擊元素頂部位置的總和)。Javascript高度總和不匹配

下面的代碼: 文件1:

jQuery('elementClicked').live('click',function(){ 
... 
jQuery("previewDiv").setTopAtClickedElement(jQuery(this)); 
... 

}

文件2:

jQuery.fn.setTopAtClickedElement = function (element) { 
//original positioning  
this.css('top', element.offset().top + 'px'); 

// the troublesome part where the eventual correction should be done 
if (element.offset().top + this.height() > jQuery(document).height()) 
{ 
    this.css('top', jQuery(document).height() - this.height() + 'px'); 
} 

}

類似發生在我使用 Math.max(文件.body.scrollHeight,document.body.offsetHeight,document.documentEle ment.clientHeight,document.documentElement.scrollHeight,document.documentElement.offsetHeight) 對文件的高度的措施的建議上a link

你對我應該如何實現代碼的這個麻煩的部分有什麼建議?

請告訴我們,如果我不太清楚, 謝謝你,而不是.height()

+0

我真的不明白你想做什麼。你有一個視覺例子嗎?或者你可以在jsfiddle中重現問題嗎? – Chanckjh

+0

這裏是情況(http://postimage.org/image/9k6cox9qh/)我想避免 - 在點擊「經銷商報價與條款」標籤下面的預覽按鈕後。 我想把div放在更高的位置,以避免穿越頁面底部邊緣 –

回答

0

嘗試使用jQuery的outer.height() - api docs,將考慮到你的頁面上的任何填充(和可選marign)。

jsfiddle或codepen將幫助我們全力解決您的問題。

+0

這裏是小提琴:http://jsfiddle.net/J5uDg/5/。 嘗試點擊數字14,您將擴展文檔以顯示預覽。我不希望這樣 - 我想讓預覽在上一頁面邊界下方呈現時向上移動(其底邊以「元素14」標籤排列)。其他元素的底部邊緣位置太低,所以預覽會溢出,因此也應該標註「元素14」標籤。 謝謝 –

+0

我也嘗試過outerHeight,但我得到了「TypeError:無法在Javascript控制檯中爲jQuery(document).outerHeight(true)讀取屬性'defaultView'的null'消息。如果我理解正確,則表示outerHeight不適用於對象 –

+0

Nick hi。你有沒有時間檢查jsfiddle?有些建議會在這裏得到很大的讚賞。 –