19

我有一个栏,通过使用 position:fixed 固定在我网站上每个页面的底部。问题是在 iPhone 或 iPad 等设备上,此属性不受尊重。

我尝试使用 javascript 来检测屏幕高度、滚动位置,这在 iPad 上完美运行:

$( window ).scroll( function ( ) { $( "#bar" ).css( "top", ( $( window ).height() + $( document ).scrollTop() - 90 ) +"px" );  } );

如您所见,我正在使用 jQuery。问题是这段代码在 iPhone 上不太适用,因为窗口的高度不包括位置栏(以及调试栏,如果存在),所以栏一开始在正确的位置,但是当你滚动它时固定在右侧位置上方(Mobile Safari 的地址栏使用的像素数量)。

有没有办法获取此信息并正确修复此工具栏?

请记住,这不是为 iPhone 制作的网站,所以我根本无法使用 iScroll 之类的技巧。

4

8 回答 8

5

从 iOS 8.4 开始,您可以position: sticky;分别使用position: -webkit-sticky;来解决这个问题。

于 2016-03-31T11:58:11.597 回答
1

我只能为您指出一些方向:

于 2011-03-23T18:11:20.340 回答
1

我只是做了这样的事情,将导航贴在窗口的顶部。导航从标题下方开始,如果您滚动通过它,则导航会粘住。iOS5 确实支持固定定位。该项目将在滚动结束 捕捉到位置,但仍然可以正常工作。'#sticky-anchor'是围绕我的导航的包装 div。

不记得我在哪里找到了这一切,从许多网站得到了一些小片段。您可以对其进行调整以满足您的需求。

$(window).scroll(function(event){

// sticky nav css NON mobile way
   sticky_relocate();

   var st = $(this).scrollTop();

// sticky nav iPhone android mobile way
// iOS 4 and below

   if (navigator.userAgent.match(/OS 5(_\d)+ like Mac OS X/i)) {
        //do nothing uses sticky_relocate above
   } else if ( navigator.userAgent.match(/(iPod|iPhone|iPad)/i) || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) ) {

        var window_top = $(window).scrollTop();
        var div_top = $('#sticky-anchor').offset().top;

        if (window_top > div_top) {
            $('#sticky').css({'top' : st , 'position' : 'absolute' });
        } else {
            $('#sticky').css({'top' : 'auto' });
        }
    };
};
于 2012-06-12T16:20:29.810 回答
1

我在我的网站上修复了这个问题,并在 Stack Overflow 上回答了这个问题。从那时起,我得到了实施它的人的大量感谢。抱歉没时间做总结。

https://stackoverflow.com/a/10030251/1118070

于 2013-04-01T19:03:28.760 回答
0

这段 jquery 代码对我有用:

if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod'){
    $("#footer_menu").css("position", "fixed").css("top", $('window').height());
};

否则 #footer_menu 的 css 是:

position:fixed;
bottom:0;
width:100%;
padding:5px 0;
text-align:center;
height:44px;

我认为设置高度有助于正确渲染,并且在桌面浏览器上我希望将此菜单固定在浏览器窗口的底部。

于 2013-03-07T20:45:45.957 回答
0

iScroll 可能是解决您问题的最简单方法。与您的看法相反,它也适用于 android 和 opera。它的新版本表现出色。

http://cubiq.org/iscroll-4

于 2012-08-31T21:46:16.557 回答
0

尝试根据 window.innerHeight 在 iPhone 上隐藏/显示底部固定导航。每当工具栏显示时,通常当您向上滚动时,您可以显示底部导航并在向下滚动时隐藏它,当工具栏隐藏时。

您可以使用这样的代码:

    var windowHeight = {
  small: window.innerHeight,
  middle: window.innerHeight,
  big: window.innerHeight
}
window.addEventListener('resize', function(){
  var currentHeight = window.innerHeight;
  if (currentHeight < windowHeight.small) {
    windowHeight.small = currentHeight;
  }

  if (currentHeight > windowHeight.big) {
    windowHeight.big = currentHeight;
  }

  console.log('windowHeight.small', windowHeight.small, 'windowHeight.middle', windowHeight.middle, 'windowHeight.big', windowHeight.big, 'currentHeight', currentHeight);

  if (currentHeight === windowHeight.big) {
    transform(stickyNav, 'translate3d(0,120%,0)');
    console.log('Hide bottom nav on big screen!');
  } else if (currentHeight === windowHeight.middle) {
    transform(stickyNav, 'translate3d(0,0,0)');
    console.log('Show bottom nav on middle screen!');
  } else {
    transform(stickyNav, 'translate3d(0,-100%,0)');
    console.log('Display bottom nav high up on smaller screen!');
  }
})

transform(stickyNav, 'translate3d(x,x,x)') 函数是一个简单的函数,它接收底部导航,然后应用变换以隐藏/显示放置在底部的项目。

于 2019-06-27T10:18:25.360 回答
-3

感谢谷歌,而不是我:

http://code.google.com/mobile/articles/webapp_fixed_ui.html

其实很简单。

于 2011-03-29T19:22:23.983 回答