安卓H5软键盘遮挡输入框

由于安卓app内嵌入H5页面,webview对于软键盘没有处理(如果不是产品强烈要求建议不要处理这种拆东墙补西墙的问题,因为其他的手机上可能会出现已经优化软键盘的情况)

1.input下方还有多余空位能够提供滚动

那么只需要一句代码就可以处理

setTimeout(function(){
                        if('scrollIntoView' in document.activeElement) {
                            document.activeElement.scrollIntoView();
                        } else {
                            document.activeElement.scrollIntoViewIfNeeded();
                        }
                },400);

2.input下方没有多余空间给页面滚动到可是区域了(也就是说input在页面最下面并且不是浮动的)

那么我们需要手动插入空白让页面有足够的空间进行1中的滚动

append进去的元素要比scrollIntoView先执行否则不生效(最好有定时器)

下面是完整代码:

var isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1; 
        if (isAndroid) {
            //软键盘处理,建议不要处理
            var isredundant = false;
            $('.r-email,.r-code').on('blur', function(){//r-email和r-code是在最底下要处理的input元素
                    setTimeout(function(){//做定时器是为了要让focus触发后再判断占位元素状态再执行
                        if (isredundant) {
                            isredundant = false;
                        }else{
                            $('.redundant_div').css('display','none');
                            isredundant = false;
                        }
                    }, 100)
            })
            $('input[type="text"],textarea').on('focus', function () {
                if ($(this).attr('class') == 'r-email' || $(this).attr('class') == 'r-code') {
                    if ($('.redundant_div').length > 0) {//如果之前是已经有插入的占位元素的,那么给出标识,让blur的时候不隐藏占位元素
                        if ($('.redundant_div').css('display') != 'none') {//如果占位元素在就给状态
                            isredundant = true;
                        }
                        setTimeout(function(){//这里的定时器要比blur的长否则一直是隐藏的
                            $('.redundant_div').css('display','block')
                        },150)
                    }else{
                        $('.personal-data').append('<div class="redundant_div" style="width: 100%;height: 200px;"></div>')
                        setTimeout(function(){
                            $('.redundant_div').css('display','block')
                        },150)
                    }
                }
                setTimeout(function(){
                        // if (target.scrollIntoViewIfNeeded) {
                        //     target.scrollIntoViewIfNeeded();
                        // }
                        
                        if('scrollIntoView' in document.activeElement) {
                            document.activeElement.scrollIntoView();
                        } else {
                            document.activeElement.scrollIntoViewIfNeeded();
                        }
                },400);
            });
        }

 

转载于:https://www.cnblogs.com/lichuntian/p/9457736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值