在Mobile Safari中打开虚拟键盘时,如何阻止我的固定导航像这样移动?

时间:2013-03-07 13:58:44

标签: ios css mobile position mobile-safari

据我所知,移动safari在固定元素周围有很多错误,但在大多数情况下,我已经设法让我的布局正常工作,直到我在底部的固定导航中添加了一个非常需要的文本输入。现在,当用户关注文本输入元素并出现虚拟键盘时,我的导航(否则始终固定在页面底部)会跳到页面中间的一个非常奇怪的位置。

enter image description here

我会在这篇文章中添加一些代码,但我不确定从哪里开始。导航固定在底部,位于左侧和底部0,宽度为100%。从那里,我不知道发生了什么,我只能假设它是一个移动的safari bug。

只有当文本输入元素被聚焦并且虚拟键盘处于打开状态时,它似乎也会失去它的位置固定并变为相对位置。

11 个答案:

答案 0 :(得分:19)

http://dansajin.com/2012/12/07/fix-position-fixed/这是提出的解决方案之一。似乎值得一试。

简而言之:当任何输入为fixed时,将position:absolute元素设置为focus,并在该元素为blur红色时重置它们

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

if ('ontouchstart' in window) {
    /* cache dom references */ 
    var $body = $('body'); 

    /* bind events */
    $(document)
    .on('focus', 'input', function() {
        $body.addClass('fixfixed');
    })
    .on('blur', 'input', function() {
        $body.removeClass('fixfixed');
    });
}

答案 1 :(得分:15)

顶部的解决方案是解决问题的一些方法,但我认为添加额外的css类或使用moderniz我们会使事情变得复杂。

如果你想要一个更简单的解决方案,这里是非现代化 非额外css ,但是纯 jquery解决方案,并且可以在我的所有项目中使用此修复的每个设备和浏览器上工作

if ('ontouchstart' in window) {
    $(document).on('focus', 'textarea,input,select', function() {
        $('.navbar.navbar-fixed-top').css('position', 'absolute');
    }).on('blur', 'textarea,input,select', function() {
        $('.navbar.navbar-fixed-top').css('position', '');
    });
}

答案 2 :(得分:5)

我遇到了类似的问题,但我找到了一个解决方法,将以下css类添加到输入焦点的body元素上,然后在unfocus上再次删除它:

.u-oh {
    overflow: hidden;
    height: 100%;
    width: 100%;
    position: fixed;
}

答案 3 :(得分:3)

从sylowgreen所做的事情来看,关键是在进入body时修复input。因此:

$("#myInput").on("focus", function () {
    $("body").css("position", "fixed");
});

$("#myInput").on("blur", function () {
    $("body").css("position", "static");
});

答案 4 :(得分:2)

像这样添加javascript:

$(function() {
  var $body;
  if ('ontouchstart' in window) {
    $body = $("body");
    document.addEventListener('focusin', function() {
      return $body.addClass("fixfixed");
    });
    return document.addEventListener('focusout', function() {
      $body.removeClass("fixfixed");
      return setTimeout(function() {
        return $(window).scrollLeft(0);
      }, 20);
    });
  }
});

并添加如下类:

.fixfixed header{ 
    position: absolute; 
} 

您可以参考这篇文章:http://dansajin.com/2012/12/07/fix-position-fixed/

答案 5 :(得分:1)

我真的很喜欢上面的解决方案。我把它打包成一个小jQuery插件,所以我可以:

  • 设置哪个父级获得课程
  • 设置适用的元素(不要忘记“textarea”和“select”)。
  • 设置父类名称
  • 允许链接
  • 允许多次使用

代码示例:

$.fn.mobileFix = function (options) {
    var $parent = $(this),
    $fixedElements = $(options.fixedElements);

    $(document)
    .on('focus', options.inputElements, function(e) {
        $parent.addClass(options.addClass);
    })
    .on('blur', options.inputElements, function(e) {
        $parent.removeClass(options.addClass);

        // Fix for some scenarios where you need to start scrolling
        setTimeout(function() {
            $(document).scrollTop($(document).scrollTop())
        }, 1);
    });

    return this; // Allowing chaining
};

// Only on touch devices
if (Modernizr.touch) {
    $("body").mobileFix({ // Pass parent to apply to
        inputElements: "input,textarea,select", // Pass activation child elements
        addClass: "fixfixed" // Pass class name
    });
}

答案 6 :(得分:1)

我使用这个jQuery脚本:

var focus = 0;
var yourInput = $(".yourInputClass");
yourInput.focusin(function(){
    if(!focus) {
        yourInput.blur();
        $("html, body").scrollTop($(document).height());
        focus = 1;
    }
    if(focus) {
        yourInput.focus();
        focus = 0;
    }
});

完美适合我。

答案 7 :(得分:1)

focusinfocusout事件似乎比focusblur事件更适合此问题,因为前者冒泡到根元素。请参阅SO上的this answer

我个人使用AngularJS,所以我实现了它:

$window.document.body.addEventListener('focusin', function(event) {
    var element = event.target;
    var tagName = element.tagName.toLowerCase();
    if(!$rootScope.inputOverlay && (tagName === 'input' || tagName === 'textarea' || tagName === 'select')) {
        $rootScope.$apply(function() {
            $rootScope.inputOverlay = true;
        });
    }
});
$window.document.body.addEventListener('focusout', function() {
    if($rootScope.inputOverlay) {
        $rootScope.$apply(function() {
            $rootScope.inputOverlay = false;
        });
    }
});

注意:如果这是移动版Safari,我将有条件地运行此脚本。

我在导航栏上添加了ng-class属性:

<div class="navbar navbar-default navbar-fixed-top" ng-class="{'navbar-absolute': inputOverlay}">

使用以下CSS:

.navbar-absolute {
    position: absolute !important;
}

您可以详细了解focusin herefocusout here

答案 8 :(得分:0)

测试这个。有用。我只是测试它。

$(document).on('focus','input', function() {
    setTimeout(function() {
        $('#footer1').css('position', 'absolute');
        $('#header1').css('position', 'absolute');
    }, 0);
});
$(document).on('blur','input', function() {
    setTimeout(function() {
        $('#footer1').css('position', 'fixed');
        $('#header1').css('position', 'fixed');
    }, 800);
});

答案 9 :(得分:0)

这些解决方案都不适用于我,因为我的DOM很复杂而且我有动态无限滚动页面,所以我必须创建自己的。

背景:我使用一个固定的标题,并且一旦用户滚动到那么远,我就会向下一个元素。该元素具有搜索输入字段。另外,我在向前和向后滚动期间添加了动态页面。

问题:在iOS中,只要用户点击固定元素中的输入,浏览器就会一直滚动到页面顶部。这不仅造成了不良行为,还触发了页面顶部的动态页面添加。

预期解决方案:当用户点击粘性元素中的输入时,iOS中无滚动(根本没有滚动)。

<强>解决方案:

     /*Returns a function, that, as long as it continues to be invoked, will not
    be triggered. The function will be called after it stops being called for
    N milliseconds. If `immediate` is passed, trigger the function on the
    leading edge, instead of the trailing.*/
    function debounce(func, wait, immediate) {
        var timeout;
        return function () {
            var context = this, args = arguments;
            var later = function () {
                timeout = null;
                if (!immediate) func.apply(context, args);
            };
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
            if (callNow) func.apply(context, args);
        };
    };

     function is_iOS() {
        var iDevices = [
          'iPad Simulator',
          'iPhone Simulator',
          'iPod Simulator',
          'iPad',
          'iPhone',
          'iPod'
        ];
        while (iDevices.length) {
            if (navigator.platform === iDevices.pop()) { return true; }
        }
        return false;
    }

    $(document).on("scrollstop", debounce(function () {
        //console.log("Stopped scrolling!");
        if (is_iOS()) {
            var yScrollPos = $(document).scrollTop();
            if (yScrollPos > 200) { //200 here to offset my fixed header (50px) and top banner (150px)
                $('#searchBarDiv').css('position', 'absolute');
                $('#searchBarDiv').css('top', yScrollPos + 50 + 'px'); //50 for fixed header
            }
            else {
                $('#searchBarDiv').css('position', 'inherit');
            }
        }
    },250,true));

    $(document).on("scrollstart", debounce(function () {
        //console.log("Started scrolling!");
        if (is_iOS()) {
            var yScrollPos = $(document).scrollTop();
            if (yScrollPos > 200) { //200 here to offset my fixed header (50px) and top banner (150px)
                $('#searchBarDiv').css('position', 'fixed');
                $('#searchBarDiv').css('width', '100%');
                $('#searchBarDiv').css('top', '50px'); //50 for fixed header
            }
        }
    },250,true));

要求:启动滚动和停止滚动功能需要JQuery mobile才能正常工作。

包含去抖动以消除粘性元素造成的任何延迟。

在iOS10中测试。

答案 10 :(得分:-1)

我对Dan Sajin提出的解决方案没有任何好运。自从他撰写博客文章以来,这个漏洞可能已经发生了变化,但是在iOS 7.1上,当输入模糊后,即使你延迟直到软件键盘被完全隐藏,这个错误也会随着位置变回固定而浮出水面。我遇到的解决方案涉及等待touchstart事件而不是blur事件,因为固定元素在页面滚动时总是快速回到正确的位置。

if (Modernizr.touch) {
  var $el, focused;
  $el = $('body');
  focused = false;
  $(document).on('focus', 'input, textarea, select', function() {
    focused = true;
    $el.addClass('u-fixedFix');
  }).on('touchstart', 'input, textarea, select', function() {
    // always execute this function after the `focus` handler:
    setTimeout(function() {
      if (focused) {
        return $el.removeClass('u-fixedFix');
      }
    }, 1);
  });
}

HTH