Fixed headers with text fields on mobile safari we

2019-03-27 21:40发布

问题:

I've been looking around for awhile, and I can't seem to find any fix for this issue. When an input field gains focus in mobile safari (haven't checked other browsers) a fixed element will jump due to safari putting that element in view (closer to the center). The error only occurs when a user has scrolled, if the user is still at the top of the page nothing occurs.

Before & After screenshots

Does anyone have a clue how to fix this? The only solution I have come across is to scroll back to the top when focused, then on blur scroll back into position... this seems like a very sloppy solution.

Here's the example site in the screenshots, very basic, nothing out of the norm.

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, scale=1"/> 
    <title>MySite</title>
    <style>
    body{
        height:100%;
        width:100%;
        padding:0;
        margin:0;
        font-size:16px;
        font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
    }
    header, footer {
        background-color:#333;
        padding:10px;
        color:#FFF;
        width:100%;
    }
    header {
        position:fixed;
        top:0;
        left:0;

    }
    input[type=search] {
        position:relative;
        margin-left:20px;
        width:160px;
        font-size:16px;
        height:26px;
    }
    section{
        padding:10px;
    }
    </style>
</head>
<body>
<header>
<b>MySite</b>
<input type='search' placeholder='Search'>
</header>
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</sction>
<footer>
&copy; MySite <?php echo date('Y') ?>
</footer>
</body>
</html>

回答1:

I have the same issue. But I realized that the fixed position is just delayed and not broken (at least for me). Scroll and wait 5-10 seconds and see if the search bar adjusts back to the top of the screen. I believe it's not an error but a delayed response when the keyboard is open.