h5 input失去焦点软键盘把页面顶起

            var broswer=localStorage.getItem('temp')  //浏览器环境
            var u = navigator.userAgent
            var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //这个判断 是不是ios手机
            var flag
            var myFunction
            if(broswer=='wx' && isiOS){ //既是微信浏览器 又是ios============(因为查到只有在微信环境下,ios手机上才会出现input失去焦点的时候页面被顶起)
                document.body.addEventListener('focusin', () => {  //软键盘弹起事件
                    flag=true
                    clearTimeout(myFunction);
                })
                document.body.addEventListener('focusout', () => { //软键盘关闭事件
                    flag=false
                    if(!flag){
                        myFunction = setTimeout(function(){  
                            window.scrollTo({top:0,left:0,behavior:"smooth"})//重点  =======当键盘收起的时候让页面回到原始位置
                        },200);
                    }else{
                        return
                    }
                })
            }else{
                return
            }

猜你喜欢

转载自www.cnblogs.com/lml-lml/p/10038408.html