gpt4 book ai didi

jquery - 无法使用 jquery 函数来绝对定位适用于 iphone 的 div

转载 作者:太空宇宙 更新时间:2023-11-04 16:23:35 24 4
gpt4 key购买 nike

这是我用来重新定位#nav div 的函数 在窗口的中间(垂直)

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 中...我的意思是,它会随着用户向下滚动而消失 :? (留在原来的位置,顶部)

知道为什么吗?

非常感谢

最佳答案

看起来很傻,但请确保您在 .css() 分配给 top 时执行 + "px"。

iPhone 可能会以不同方式处理其窗口高度。我会输出一些调试信息,这样您就可以验证 iPhone 是否正在报告/获取正确的值。

编辑 5/19/11 9:35am est

在查看您的原始问题以及您对 iPhone 报告 240px 的评论(无论您滚动多少)时..报告 240px 是有道理的。你的 navHeight 是一个固定值,你的 windowHeight 是一个固定值,所以当你减去它们时你总是得到相同的 newtop 值。您需要考虑页面的滚动偏移 - 尝试将 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 });

关于jquery - 无法使用 jquery 函数来绝对定位适用于 iphone 的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6050725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com