解决ios微信端失焦键盘隐藏后,被顶起的页面不回弹问题

/// jquery
$('input,textarea').on('blur', function() {
    var ua = navigator.userAgent.toLowerCase();
    if(/micromessenger/.test(ua)) {
        if(/iphone|ipad|ipod/.test(ua)) {
            var currentPosition, timer;
            var speed = 1; 
            timer = setInterval(function() {
                currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
                currentPosition-=speed;
                window.scrollTo(0,currentPosition);
                currentPosition+=speed; 
                window.scrollTo(0,currentPosition);
                clearInterval(timer);
            }, 1);
        }
    }});
     
     
   
  一、案例1 
/// vue
Vue.directive('iosbugscroll', {
  inserted: function(el, binding, vnode) {
    var ua = navigator.userAgent.toLowerCase();
    if(/micromessenger/.test(ua)) {
      if(/iphone|ipad|ipod/.test(ua)) {
        // input、textarea被组件包装的场景
        const childInput = el.getElementsByTagName('input');
        const childTextarea = el.getElementsByTagName('textarea');
        for (let i = 0; i < childInput.length; i++) { 
          childInput[i].onblur = function temporaryRepair() {
            setTimeout(function() {
              checkWxScroll();
            }, 200);
          };
        }
        
        for (let i = 0; i < childTextarea.length; i++) {
          childSelect[i].onblur = function temporaryRepair() {
            setTimeout(function() {
              checkWxScroll();
            }, 200);
          };
        }
        // input、textarea中的场景
        el.onblur = function temporaryRepair() {
          setTimeout(function() {
            checkWxScroll();
          }, 200);
        };
      }
    }
  }});
  function checkWxScroll() {
  var currentPosition, timer;
  var speed = 1; 
  timer = setInterval(function() {
    currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
    currentPosition-=speed;
    window.scrollTo(0,currentPosition);
    currentPosition+=speed; 
    window.scrollTo(0,currentPosition);
    clearInterval(timer);
  }, 1);}
  //使用
   
  <input  type="text" v-iosbugscroll />
   
   
   
  二、案例2 实际使用
   
  mounted(){
   
       //判断是IOS还是Android
    var userAgent = navigator.userAgent;
    var isAndroid =
    userAgent.indexOf("Android") > -1 || userAgent.indexOf("Adr") > -1; //android终端
    var isiOS = !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        // console.log(isAndroid, isiOS);
    const innerHeight = window.innerHeight;
    window.addEventListener("resize", () => {
        if (document.activeElement.tagName == "INPUT") {
            //延迟出现是因为有些 Android 手机键盘出现的比较慢
            window.setTimeout(() => {
                document.activeElement.scrollIntoViewIfNeeded();
            }, 100);
            }
            if (isAndroid) {
                const newInnerHeight = window.innerHeight;
                if (innerHeight > newInnerHeight) {
                          // 键盘弹出事件处理
                this.sureClass = "static";
                this.velCodeFocus = true;
            } else {
                      // 键盘收起事件处理
                this.sureClass = "";
                this.velCodeFocus = false;
                this.selectBlur()
            }
       }
    });
    if (isiOS) {
        window.addEventListener("focusin", () => {
            // 键盘弹出事件处理
            this.sureClass = "static"; //如果底部有按钮则改变布局 static 原先fixed
            this.velCodeFocus = true;
        });
        window.addEventListener("focusout", () => {
            // 键盘收起事件处理
            this.sureClass = ""; //如果底部有按钮则改变布局 static 原先fixed
            this.velCodeFocus = false;
            this.selectBlur()
        });
    }
   
  },
   
directives: {
    focus: {
        inserted: function(el, { value }) {
       		el[value ? "focus" : "blur"]();
        },
        //更新判断
        update: function(el, { value }) {
        	el[value ? "focus" : "blur"]();
        },
        //更新完成s
        componentUpdated: function(el, { value }) {}
    }
},
   
  // 使用
  <input placeholder="请输入" v-focus="velCodeFocus" v-model="velCode" />
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值