2011-05-18 70 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