Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

css - iOS input focused inside fixed parent stops position update of fixed elements

The following happens on Mobile Safari iOS 6.1.2

Steps to reproduce

Create a position: fixed element with an <input type="text"> element inside it.

Actual result

  1. Input - not focused

    The position of the fixed elements is correct when input is not focused.

    image

  2. Input - focused

    When the input is focused, the browser enters a special mode in which it does not update the position of the fixed elements any more (any fixed positioned element, not just the input's parent) and moves the whole viewport down in order to make the input's parent element sit in the center of the screen.

    See live demo: http://jsbin.com/oqamad/1/

    image image

Expected result

The position of the fixed elements is always respected.

Fix or workaround?

Any clues as how to force Safari to properly display the fixed elements would be helpful.

I would prefer a workaround which does not involve using position: absolute and setting an onscroll event handler.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a well known bug on Safari, both on iPad and iPhone.

A workaround it's to change the position to absolute to all elements set with fixed position.

In case you are using Modernizr you could also target mobile devices this way:


jQuery code


$(document).ready(function() {

  if (Modernizr.touch) {

      $(document)

        .on('focus', 'input', function(e) {
            $('body').addClass('fixfixed');
        })

        .on('blur', 'input', function(e) {
            $('body').removeClass('fixfixed');
        });

      }

});

CSS


If I want to target just the header and footer for example, that are set with fixed position, when the class 'fixfixed' is added to the body I can change the position to absolute to all elements with fixed position.

.fixfixed header, .fixfixed footer {
  position: absolute;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...