前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >H5端软键盘把footer底部导航栏顶上去了怎么解决?

H5端软键盘把footer底部导航栏顶上去了怎么解决?

作者头像
崔文远TroyCui
发布2019-02-26 11:08:48
3.1K0
发布2019-02-26 11:08:48
举报
文章被收录于专栏:远在上海

这个问题是WebApp的通病,特别是用了BootStrap的底部导航栏,又用了input选中时自动移到合适位置,避免软键盘覆盖的时候,会特别明显。

网上有不少分享,都是通过js来实现的,因为我是在安卓手机端,试了多个不管用。

比方说这个:

代码语言:javascript
复制
var oHeight = $(document).height(); //浏览器当前的高度 
$(window).resize(function(){//ios软键盘弹出不会触发resize事件
     if($(document).height() < oHeight){
     $("#footer").css("position","static"); }else{
     $("#footer").css("position","absolute"); //adsolute或fixed,看你布局
} }); 

还有这个:

代码语言:javascript
复制
$("body").find("input[type=text]").each(function() {
    $(this).bind('focus', function() {
        $('#footer').css('display', 'none');
    }).bind('blur', function() {
        $('#footer').css({
            'position': 'fixed',
            'bottom': '0'
        });
        var footer = $('#footer');
                    var display = footer.css('display');
                    if (display == 'none') {
                        footer.show();
                    } else {
                        footer.hide();
                    }
    });
});

最终,我把输入框软键盘防覆盖和底部导航软键盘防顶结合在一起,安卓端用起来还不错。

代码语言:javascript
复制
$(function () {
    $('input,textarea').on('focus', function () {
        //立即隐藏
        $('.footer').css('display', 'none');
        var target = this;
        setTimeout(function () {
            target.scrollIntoViewIfNeeded();
            //target.scrollIntoView(true);
            //console.log('scrollIntoViewIfNeeded');
        }, 200);
    });
    $('input,textarea').on('click', function () {
        var target = this;
        setTimeout(function () {
            target.scrollIntoViewIfNeeded();
            //target.scrollIntoView(true);
            //console.log('scrollIntoViewIfNeeded');
        }, 200);
    });
    $('input,textarea').on('blur', function () {
        $('.footer').css({
            'position': 'fixed',
            'bottom': '0'
        });
        var footer = $('.footer');
        var display = footer.css('display');
        if (display === 'none') {
            footer.fadeIn(2000, function () {//fade
                    footer.show();
            });

            //setTimeout(function () {
            //    footer.show();
            //}, 200);
        } else {
            footer.hide();
        }
    });
});
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年1月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档