IOS当输入框的地位在比拟上面的时候,会产生滚动来让出键盘对应的地位,然而有的时候就会呈现让出的地位在键盘上来时没有产生回滚,所以就会呈现页面呈现空白显示不全的问题。
解决方案:
在蕴含输入框的父元素进行事件监听,当呈现失焦时判断是否是输入框,而后管制滚动。为了避免出现从一个滚动框到另一个滚动框呈现滚动的问题,所以对滚动事件做了提早和革除解决。失焦采纳focusout事件监听,是因为blur事件不会冒泡。
handleInputBlur(e) { if ( e && e.target && e.target.tagName && (e.target.tagName.toLowerCase() === 'input' || e.target.tagName.toLowerCase() === 'textarea') && e.target.getAttribute('readonly') !== 'readonly' ) { if (this.timer) { clearTimeout(this.timer) } this.timer = setTimeout(() => { window.scrollTo(0, 0) }, 50) } }, // 取得焦点事件 handleInputIn(e) { if ( e && e.target && e.target.tagName && (e.target.tagName.toLowerCase() === 'input' || e.target.tagName.toLowerCase() === 'textarea') && e.target.getAttribute('readonly') !== 'readonly' ) { if (this.timer) { clearTimeout(this.timer) } } }
Vue父元素包裹
<div @focusout="handleInputBlur" @focusin="handleInputIn"></div>