代码之家  ›  专栏  ›  技术社区  ›  rfgamaral

固定在Chrome mobile上的位置,滚动时会导致问题

  •  4
  • rfgamaral  · 技术社区  · 8 年前

    在过去的一个小时里,我一直在研究这个问题,看到了类似的问题,但我不确定它们是否是同一个问题。可能有某种关联,但没有一个答案能帮助我解决问题。

    采用以下代码:

    <html>
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <style>
                html, body {
                    height: 100%;
                    margin: 0;
                }
    
                main {
                    background-color: orange;
                    height: 1500px;
                    margin: 50px;
                }
    
                footer {
                    background-color: green;
                    position: fixed;
                    height: 50px;
                    left: 100px;
                    right: 100px;
                    bottom: 100px;
                }
            </style>
        </head>
        <body>
            <main></main>
            <footer></footer>
        </body>
    </html>
    

    这很难调试,因为我似乎无法始终如一地重现问题。我不断上下滚动,在Chrome上为Android显示和隐藏地址栏,最终会发生类似的事情:

    enter image description here

    出于某种原因 footer 正在正确的位置绘制(由CSS指定),但Chrome开发工具会在不同的位置检测元素(并不总是像屏幕截图所示)。

    为什么这是一个问题?

    假设里面有可点击的元素 页脚 ,这些元素的可单击区域将位于Chrome开发工具检测到的“蓝色”区域,而不是实际绘制页脚的位置(绿色区域),因为这是用户看到的。

    有什么想法?

    1 回复  |  直到 8 年前
        1
  •  0
  •   rfgamaral    8 年前

    编辑: 我把下面的代码留在这里,但我发现它没有按我预期的那样工作。在我的初始测试期间,它确实起了作用,但我们的QA发现它实际上并没有解决我们遇到的问题。目前,我知道没有解决方法,我们需要等待Chromium团队修复 issue 在他们这边。

    非工作解决方案

    我可能刚刚找到了解决这个铬错误的方法。

    我正在运行最新Chrome的Pixel 2上测试这一点,不确定它对低端设备有多好。这有点难看,但似乎对我有用。

    我所做的是在 touchend 事件这是完美的,因为问题只存在于移动和 触摸端 桌面版本上不存在。

    const body = document.getElementsByTagName('body');
    const button = document.getElementsByTagName('button');
    const footer = document.getElementsByTagName('footer');
    
    function randomColor() {
      button[0].style.backgroundColor =
        `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
    }
    
    window.addEventListener('touchend', function() {
      body[0].replaceChild(footer[0], footer[0]);
    }, false);
    * {
      box-sizing: border-box;
    }
    
    html,
    body {
      height: 100%;
      margin: 0;
    }
    
    main {
      background-color: orange;
      height: 3000px;
      margin: 10px;
    }
    
    footer {
      border: 2px solid green;
      background-color: greenyellow;
      display: flex;
      justify-content: center;
      align-items: center;
      position: fixed;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 100px;
    }
    
    button {
      border: 2px solid black;
      background-color: white;
      cursor: pointer;
      width: 50%;
      height: 70%;
    }
    <main></main>
    <footer>
      <button onclick="randomColor()">CLICK ME!</button>
    </footer>