2011-08-23 56 views
0

我正在寫一個函數來測試滾動條的位置,而不是預先存儲在數組中的元素位置。在頁面上可以有任何數量的這種元素類型,這就是爲什麼我使用for循環來測試我的滾動條位置。與FF和IE相比,jQuery在Chrome中返回錯誤的元素位置

此代碼在Firefox中正常工作,但由於某種原因,jQuery在Chrome中爲我的每個.post-info($ pi)項返回完全不同的位置,甚至得到$(document).height錯誤。這是我的JavaScript,或與我的CSS問題?當我測試了我的位置後,我會假設我的JS:相對於我CSS中的大多數元素。

這裏是我運行了jQuery:

(function(){ 
var i, 
st, 
length, 
arrPos = [] 

    $wn = $(window); 
    $pi = $('.post-info'); 

    $pi.each(function(){ 
    arrPos.push($(this).offset().top - 80); // offset -80 so new item sticks quicker. 
    }); 
    height = $(document).height(); 
    arrPos.push(height); // add bottom position of document to array for last item. 

    length = arrPos.length;   
    $wn.scroll(function(){ //Scroll Event Listener 
    st = $wn.scrollTop(); 
    for(i=0; i<=length; i++){ 
     // i = index, n = next 
     n = i+1; 
     if(st > arrPos[i] && st < arrPos[n]) _stick(i); 
    } 
}); 

非常感謝。

回答

1

谷歌瀏覽器在$(文檔).ready後設置高度和寬度 如果您有任何圖像,那麼在此特定時刻,您的頁面尺寸將不會是正確的。嘗試預先加載你的圖像,並在完成時,比調用你的功能。 希望這個幫助:)

+0

感謝您的建議,但我發現問題是,我的圖像非特定的高度集 - 我已經補充說,現在鉻承認他們的存在,一切都很好!謝謝。 –

+0

不錯,謝謝你的反饋:) –

相關問題