2011-05-18 27 views
0

工作這是我使用重新定位#nav DIV 在窗口的中間(垂直)功能不能得到一個jQuery的功能絕對-POS一個div爲iPhone

function RepositionNav(){ 
      var windowHeight = $window.height(); //get the height of the window 
      var navHeight = $('#nav').height()/2; 
      var windowCenter = (windowHeight/2); 
      var newtop = windowCenter - navHeight; 
      $('#nav').css({"top": newtop}); //set the new top position of the navigation list 
    } 

這是哪裏我重新定位,而窗口

$window.bind('scroll', function(){ //when the user is scrolling... 

       RepositionNav(); 
}); 

爲用戶向下滾動它工作正常,但在iphone沒有 ... 我的意思是,它消失在滾動? (停留在原來的位置,頂端)

任何想法爲什麼?

非常感謝

+0

停留在頂部像個位置是固定的,如果你在它前面加上'的.css ({「職位」:「絕對」})'鏈接它然後工作? – 2011-05-18 22:31:22

+0

好吧,我確實有它的位置:絕對在我的CSS樣式表中,但我會嘗試aswel! – 2011-05-18 22:40:00

+0

嘗試在scroll-method中添加一個警報,看它是否被觸發。 – Alxandr 2011-05-18 22:42:54

回答

1

看來愚蠢,但要確保你的.css()分配爲top做+「像素」。

iPhone可能會以不同的方式處理窗口高度。我會輸出一些調試信息,以便您可以驗證iPhone是否正在報告/獲取正確的值。

編輯11年5月19日上午9:35 EST

在看着你原來的問題,你對iPhone提出的意見報告240像素,無論你多麼scroll..it是有道理的,它'報告240px。你的navHeight是一個固定值,你的windowHeight是一個固定值,所以當你減去它們時你總是會得到相同的值。你需要考慮到滾動頁面的偏移 - 嘗試使用window.scrollYOffset值連同你的公式,像這樣:

var windowHeight = $window.height(), 
    navHeight = $('#nav').height()/2, 
    windowCenter = (windowHeight/2), 
    newTop = (windowCenter - navHeight) + window.scrollYOffset; 
    /* we use the scrollYOffset to normalize your newTop value to 
     the current top of the viewPort, based on how far the user has 
     scrolled down on the page. 
    */ 

$('#nav').css({ top: newTop }); 
+0

ahá!沒想到關於這個!但它似乎沒有任何區別(但也許我的iPhone手機緩存它與我一起玩......) – 2011-05-19 00:54:47

+0

好吧,做了一個警報(newpos +「px」),它顯示了240pxpx,所以+「px」是不需要,問題是,無論我滾動多少,都顯示相同的240,這是怎麼回事?任何線索?乾杯! – 2011-05-19 13:10:42

+0

謝謝吉姆,但它不工作... :( – 2011-05-19 15:44:06

相關問題