修复了 Mobile Safari 中的定位

发布于 2024-11-07 21:55:25 字数 172 浏览 0 评论 0原文

是否可以在 Mobile Safari 中相对于视口固定元素的位置?正如许多人所指出的,position:fixed 不起作用,但 Gmail 刚刚推出了一个几乎正是我想要的解决方案 - 请参阅消息视图上的浮动菜单栏。

在 JavaScript 中获取实时滚动事件也是一个合理的解决方案。

Is it possible to position an element fixed relative to the viewport in Mobile Safari? As many have noted, position: fixed doesn't work, but Gmail just came out with a solution that almost is what I want – see the floating menu bar on the message view.

Getting real-time scroll events in JavaScript would also be a reasonable solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

可是我不能没有你 2024-11-14 21:55:25

只需 2 行代码即可实现此固定位置 div,将滚动时的 div 移动到页面底部。

window.onscroll = function() {
  document.getElementById('fixedDiv').style.top =
     (window.pageYOffset + window.innerHeight - 25) + 'px';
};

This fixed position div can be achieved in just 2 lines of code which moves the div on scroll to the bottom of the page.

window.onscroll = function() {
  document.getElementById('fixedDiv').style.top =
     (window.pageYOffset + window.innerHeight - 25) + 'px';
};
蓝颜夕 2024-11-14 21:55:25

请参阅 Google 针对此问题的解决方案。您基本上必须使用 JavaScript 自己滚动内容。 Sencha Touch 还提供了一个库,用于在非常高性能的庄园中实现此行为。

See Google's solution to this problem. You basically have to scroll content yourself using JavaScript. Sencha Touch also provides a library for getting this behavior in a very performant manor.

清音悠歌 2024-11-14 21:55:25

它对我有用:

function changeFooterPosition() {   
  $('.footer-menu').css('top', window.innerHeight + window.scrollY - 44 + "px");
}

$(document).bind('scroll', function() {
  changeFooterPosition();
});

(44是我的栏的高度)

虽然栏只在滚动的末尾移动......

it worked for me:

function changeFooterPosition() {   
  $('.footer-menu').css('top', window.innerHeight + window.scrollY - 44 + "px");
}

$(document).bind('scroll', function() {
  changeFooterPosition();
});

(44 is the height of my bar)

Although the bar only moves at the end of the scroll...

陈年往事 2024-11-14 21:55:25

我认为 gmail 只是跟踪计时器上的滚动位置并相应地重新定位 div

我见过的最好的解决方案是 doctyper

一个更简单的 jQuery 解决方案,可在滚动上移动元素:链接

I think gmail just tracks the scroll position on a timer and repositions a div accordingly.

The best solution I've seen is at doctyper.

A simpler jQuery solution that moves an element onscroll: link

尽揽少女心 2024-11-14 21:55:25

您可能对此感兴趣。这是 Apple Dev 支持页面。
http://developer.apple.com/library/ios/#technotes/ tn2010/tn2262/

阅读“4.修改依赖CSS固定定位的代码”这一点,你会发现苹果有意识地决定将固定位置处理为静态是有充分理由的。

This may interest you. It's Apple Dev support page.
http://developer.apple.com/library/ios/#technotes/tn2010/tn2262/

Read the point "4. Modify code that relies on CSS fixed positioning" and you will find out that there is very good reason why Apple made the conscious decision to handle fixed position as static.

画离情绘悲伤 2024-11-14 21:55:25

您可以尝试使用 touch-scroll,这是一个 jQuery 插件,可以模拟移动 Safari 上固定元素的滚动: https:// github.com/neave/touch-scroll

使用 iOS 设备查看示例,网址为 http:// /neave.github.com/touch-scroll/

或者替代方案是 iScroll: http://cubiq.org/滚动

You could try using touch-scroll, a jQuery plugin that mimics scrolling with fixed elements on mobile Safari: https://github.com/neave/touch-scroll

View an example with your iOS device at http://neave.github.com/touch-scroll/

Or an alternative is iScroll: http://cubiq.org/iscroll

七颜 2024-11-14 21:55:25

我就是这样做的。
我有一个导航块,它位于标题下方,一旦您向下滚动页面,它就会“粘”到窗口顶部。
如果您滚动回到顶部,导航将返回到原来的位置
我在非移动平台和 iOS5 的 CSS 中使用position:fixed。
其他移动版本确实存在“滞后”,直到屏幕在设置之前停止滚动。

// css
#sticky.stick {
    width:100%;
    height:50px;
    position: fixed;
    top: 0;
    z-index: 1;
}

// jquery 
//sticky nav
    function sticky_relocate() {
      var window_top = $(window).scrollTop();
      var div_top = $('#sticky-anchor').offset().top;

      if (window_top > div_top)
        $('#sticky').addClass('stick');
      else
        $('#sticky').removeClass('stick');
     }

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

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

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

    // sticky nav iPhone android mobile way iOS<5

       if (navigator.userAgent.match(/OS 5(_\d)+ like Mac OS X/i)) {
            //do nothing uses sticky_relocate() css
       } 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' });
            }
        };

This is how i did it.
I have a nav block that is below the header once you scroll the page down it 'sticks' to the top of the window.
If you scroll back to top, nav goes back in it's place
I use position:fixed in CSS for non mobile platforms and iOS5.
Other Mobile versions do have that 'lag' until screen stops scrolling before it's set.

// css
#sticky.stick {
    width:100%;
    height:50px;
    position: fixed;
    top: 0;
    z-index: 1;
}

// jquery 
//sticky nav
    function sticky_relocate() {
      var window_top = $(window).scrollTop();
      var div_top = $('#sticky-anchor').offset().top;

      if (window_top > div_top)
        $('#sticky').addClass('stick');
      else
        $('#sticky').removeClass('stick');
     }

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

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

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

    // sticky nav iPhone android mobile way iOS<5

       if (navigator.userAgent.match(/OS 5(_\d)+ like Mac OS X/i)) {
            //do nothing uses sticky_relocate() css
       } 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' });
            }
        };
尽揽少女心 2024-11-14 21:55:25
<meta name="viewport" content="width=320, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> 

另外,确保此元标记中不存在 height=device-height 有助于防止页面上通常不存在的额外页脚填充。菜单栏高度增加到视口高度,导致固定背景变得可滚动。

<meta name="viewport" content="width=320, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> 

Also making sure height=device-height is not present in this meta tag helps prevent additional footer padding that normally would not exist on the page. The menubar height adds to the viewport height causing a fixed background to become scrollable.

浅黛梨妆こ 2024-11-14 21:55:25

在这里您可以看到哪些(移动)浏览器支持 css 位置固定+那里版本。

http://caniuse.com/css-fixed

Here you can see what (mobile) browsers support css position fixed + there version.

http://caniuse.com/css-fixed

离旧人 2024-11-14 21:55:25

我们的网络应用程序需要一个固定的标头。我们很幸运,我们只需要支持最新的浏览器,但 Safari 在这方面的行为给我们带来了真正的问题。

正如其他人指出的那样,最好的解决方法是编写我们自己的滚动代码。但是,我们无法证明修复仅在 iOS 上出现的问题的努力是合理的。希望苹果能够解决这个问题更有意义,特别是因为,正如 QuirksMode 所建议的,苹果现在在“位置:固定”的解释上是独一无二的。

http://www.quirksmode.org/blog/archives/2013/12 /position_fixed_1.html

对我们有用的是根据用户是否缩放来在“position:fixed”和“position:absolute”之间切换。这用可预测的行为取代了我们的“浮动”标头,这对于可用性很重要。缩放时,行为不是我们想要的,但用户可以通过反转缩放轻松解决此问题。

// On iOS, "position: fixed;" is not supported when zoomed, so toggle "position: absolute;".
header = document.createElement( "HEADER" );
document.body.appendChild( header );
if( navigator.userAgent.match( /iPad/i ) || navigator.userAgent.match( /iPhone/i )) {
    addEventListener( document.body, function( event ) {
        var zoomLevel = (( Math.abs( window.orientation ) === 90 ) ? screen.height : screen.width ) / window.innerWidth;
        header.style.position = ( zoomLevel > 1 ) ? "absolute" : "fixed";
    });
}

Our web app requires a fixed header. We are fortunate in that we only have to support the latest browsers, but Safari's behavior in this area caused us a real problem.

The best fix, as others have pointed out, is to write our own scrolling code. However, we can't justify that effort to fix a problem that occurs only on iOS. It makes more sense to hope that Apple may fix this problem, especially since, as QuirksMode suggests, Apple now stands alone in their interpretation of "position:fixed".

http://www.quirksmode.org/blog/archives/2013/12/position_fixed_1.html

What worked for us is to toggle between "position:fixed" and "position:absolute" depending on whether the user has zoomed. This replaces our "floating" header with predictable behavior, which is important for usability. When zoomed, the behavior is not what we want, but the user can easily work around this by reversing the zoom.

// On iOS, "position: fixed;" is not supported when zoomed, so toggle "position: absolute;".
header = document.createElement( "HEADER" );
document.body.appendChild( header );
if( navigator.userAgent.match( /iPad/i ) || navigator.userAgent.match( /iPhone/i )) {
    addEventListener( document.body, function( event ) {
        var zoomLevel = (( Math.abs( window.orientation ) === 90 ) ? screen.height : screen.width ) / window.innerWidth;
        header.style.position = ( zoomLevel > 1 ) ? "absolute" : "fixed";
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文