解决微信浏览器web页面键盘收起页面不回弹

在涉及的input中设置blur事件,事件里具体使用的方法如下

setTimeout(() => {
  window.scrollTo(0, 0)
}, 100)

如果当前页面有多个

inputFocus (index) {
  this.lastFocusInputIndex = index
},
inputBlur (index) {
  let self = this
  self.lastFocusInputIndex = index
  setTimeout(() => {
    self.checkInputBlur(index)
  }, 100)
},
checkInputBlur (lastIndex) {
  if (this.lastFocusInputIndex === lastIndex) {
    window.scrollTo(0, 0)
    this.lastFocusInputIndex = -1
  }
}

网上看到的方法二

(function () {
  let myFunction
  let isWXAndIos = isWeiXinAndIos()
  if (isWXAndIos) {
    document.body.addEventListener('focusin', () => {
      clearTimeout(myFunction)
    })
    document.body.addEventListener('focusout', () => {
      clearTimeout(myFunction)
      myFunction = setTimeout(function() {
        window.scrollTo({top: 0, left: 0, behavior: 'smooth'})
      }, 200)
    })
  }
})()

function isWeiXinAndIos () {
  let ua = '' + window.navigator.userAgent.toLowerCase()
  let isWeixin = /MicroMessenger/i.test(ua)
  let isIos = /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(ua)
  return isWeixin && isIos
}

最近使用的方法三

var u = navigator.userAgent
var sAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
// var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
// 安卓
if (isAndroid) {
    window.addEventListener('resize', function() {
        if (document.activeElement.tagName == "INPUT") {
            window.setTimeout(function() {
                document.activeElement.scrollIntoViewIfNeeded() // 异常时可以用 document.activeElement.scrollIntoView(false)
            }, 0)
        }
    })
}
function inputFocus (type) {
    console.log('focus type === ', type)
}
function inputBlur (type) {
    console.log('blur type === ', type)
    setTimeout(() => {
        window.scrollTo(0, 0)
    }, 100)
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,404评论 19 139
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 8,863评论 1 11
  • 1、常用标签以及适用场景 ps:前面有感叹号的是自闭合标签 标签通用属性 2、a链接 href链接地址有如下几种情...
    不是我的简书阅读 2,675评论 0 0
  • 我如同发神经一般,把微信删了,原因:日常生活已经被微信绑架。而我想获得自由,不被任何东西捆绑的自由。删除之后肯定会...
    指尖猿阅读 3,627评论 8 4
  • 今日体验:注意说话方式,说者无意,听者有心,尤其是客户听到不该听的话,影响很不好,调整好心态,减少不文明用语,别让...
    京心达阅读 838评论 0 0