vue ios 内嵌vue3 H5,在 input 聚焦时导致页面整体可向上滚动

111 阅读1分钟

在ios上,不管你顶部导航栏使用什么定位,或者正常文档流,在input 聚焦时,手指放在原本的不可滑动区域(如顶部导航栏)向下滑动就会导致导航栏整体向上滚动了,失去焦点后又恢复正常

调试是否是根节点wrapper高度发生变化,发现聚焦前后高度都没有变化,那就一定是webview的问题,找了很久方案都感觉说的很复杂,最后在某SDN发现可以通过让input元素失去焦点解决问题 在此记录一下

// input ref="inputNode" @focus="iosfocusFix" @blur="isScroll = false"
const inputNode = ref()
const isScroll = ref<boolean>(true)
const iosfocusFix = () => {
  isScroll.value = true
  setTimeout(() => {
    if (isScroll.value) {
      window.addEventListener('scroll', webviewScrol)
    }
  }, 800)
}
const webviewScrol = () => {
  if (!inputNode.value) return
  inputNode.value.blur()
  window.removeEventListener('scroll', webviewScrol)
}
onUnmounted(() => {
  window.removeEventListener('scroll', webviewScrol)
})