简体   繁体   English

滚动和位置问题:已修复

[英]Issue with scrolling and position:fixed

I maintain a Web page that has buttons which are supposed to remain fixed relative to the window when you scroll around and/or jump to links within the page (one of the buttons takes you to a random link within the page). 我维护了一个网页,其中包含一些按钮,当您滚动和/或跳转到页面内的链接时,这些按钮应该相对于窗口保持固定(按钮之一会将您带到页面内的随机链接)。 I do this with position:fixed, and I reposition the elements whenever I get callbacks for scrolling/resizing/URL changes. 我使用position:fixed进行此操作,并且每当我收到用于滚动/调整大小/ URL更改的回调时,都会重新定位元素。 Code is below. 代码如下。

100% of the time on desktops, the right thing happens when I scroll or jump to a link within the page, and the buttons stay where they are. 100%的时间是在台式机上,当我滚动或跳转到页面内的链接时,正确的事情发生了,并且按钮停留在原处。

But the behavior on Chrome for Android is: 但是,Chrome for Android上的行为是:

  • When I scroll down , the buttons stay where they belong 当我向下滚动 ,按钮停留在它们所属的位置
  • When I scroll up , the buttons scroll away with the content 当我向上滚动 ,按钮会随内容一起滚动
  • When I jump to a link within the page, the right thing happens around 50% of the time. 当我跳到页面内的链接时,大约50%的时间发生正确的事情。

In both of the broken cases, after the bug occurs (ie, the buttons disappear), if I scroll down for maybe 10 lines, the buttons appear again. 在这两种损坏的情况下,在错误发生之后(即按钮消失),如果我向下滚动大约10行,按钮将再次出现。 The callback to reposition the controls is getting called in these cases (based on printouts), but it doesn't position the elements correctly. 回调重新获取调用在这些情况下(基于打印输出)的控制,但它不元素正确定位。

Good news, though… this bug is easily replicated in the Chrome Developer Tools. 不过,这是个好消息……此错误很容易在Chrome开发者工具中复制。

There are a couple of pages exhibiting this issue; 有几页展示了这个问题。 an easy one to load is: 一个容易加载的是:

http://awesomesongbook.com/soundtracks/soundtracks.html http://awesomesongbook.com/soundtracks/soundtracks.html

Any advice? 有什么建议吗?

Code follows. 代码如下。

Thanks! 谢谢!

$(window).load(function(){

    $(window).scroll(positionScroller);
    $(window).resize(positionScroller);
    $(window).bind('hashchange',positionScroller);

    positionScroller();

    function positionScroller()
    {
        $('#scroller').css('visibility','visible');

        if (window.screen.width > 800)
        {
            $('#scroller').css('position', 'fixed');
            $('#scroller').css('bottom',10);
            $('#scroller').css('right',10);                   
            $('#scroller').css('top','auto');
            $('#scroller').css('left','auto')
        }
        else
        {
            $('#scroller').css('position', 'fixed');            
            $('#scroller').css('top',5);
            $('#scroller').css('left',5);
            $('#scroller').css('bottom','auto');
            $('#scroller').css('right','auto');                       
        }
    };
});

Is it absolutely necessary to rely on JavaScript? 绝对有必要依靠JavaScript吗? Best way to handle in my opinion is with pure CSS. 我认为最好的处理方法是使用纯CSS。 Based on your code, you could use a media query to position the padding for the scroller. 根据您的代码,您可以使用媒体查询来定位滚动条的填充。

#scroller {
    position:fixed;
    bottom:auto;
    right:auto;                   
    top:5px;
    left:5px;
}

@media all and (min-width: 801px)
{
    #scroller {
        bottom:10px;
        right:10px;                   
        top:auto;
        left:auto;
    }
}

This appears to be a documented bug in Chrome for Android with position:fixed. 这似乎是在Chrome浏览器(Android版)中已记录的错误,位置已修复。 I have not yet been able to digest the specific scope of the bug. 我尚未能够消化该错误的具体范围。

The least invasive solution I've seen online is to add this to the element in question: 我在网上看到的侵入性最小的解决方案是将其添加到有问题的元素中:

-webkit-transform: translate3d(0, 0, 0);
transform : translate3d(0, 0, 0);

...but that didn't work for me. ...但这对我不起作用。

What did work for me is disabling user scaling (pinch to zoom): 对我有用的是禁用用户缩放(捏缩放):

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">

This is sub-optimal, because I'd prefer to leave user scaling enabled, but it does make the problem with position:fixed go away. 这不是最理想的,因为我希望保留用户缩放功能,但这确实使position:fixed的问题消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM