解决软键盘弹窗 fixed失效问题

概述

这个问题常出现在移动web开发中聊天或者留言页面的绝对定位输入框上,页面超过屏幕大小时候输入框focus状态下(键盘弹出)绝对定位的元素失效,导致页面滚动时候把定位元素一并带走,体验十分不好,在此留下一自己的方法,让更多的人不需要再爬这样的小坑。

解决办法

原理很简单,就是定义一个外框把页面包起来,把需要使用fixed定位的元素设置成absolute定位,然后设置外框元素的样式即可,下面是实例:
假设外框元素为.wrap,需要fixed定位的元素为.position

DOM
<html>
    <body class="wrap">
        <div class="position">
            <input type="text" id="js-searchKey" class="search-input" placeholder="关键词搜索">
        </div>
    </body>
</html>
CSS
body.full-body{
    position: fixed !important;
    top: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: 100% !important;
    padding: 0 !important;
    margin: 0 !important;
    overflow: hidden !important;
    -webkit-overflow-scrolling: touch;
}
.fixed-bug{
    position: absolute !important;
}
JS

当input获取焦点,弹出软键盘的时候,就切添加解决bug的class;反之,删除解决bug的class。

function addBodyClass(element){
    $('body').addClass('full-body');
    $(element).addClass('fixed-bug');
}
function removeBodyClass(element){
    $('body').removeClass('full-body');
    $(element).removeClass('fixed-bug');
}
$("#js-searchKey").on('focus',function(){
    var $fixed=$('.position');
    addBodyClass($fixed);
});
$("#js-searchKey").on('blur',function(){
    var $fixed=$('.position');
    removeBodyClass($fixed);
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值