给input添加获取焦点事件 @focus='focus' 和失去焦点 @blur='blur'
//获取焦点触发
focus(){
//判断是否是ios
if (isiOS()) {
setTimeout(() => {
//this.$refs.container可视区域元素
this.$refs.container.style.height = `${window.visualViewport.height || window.innerHeight}px`;
window.scrollTo(0, 0);
document.addEventListener("touchmove", this.stopMove, {
passive: false,
});
document.addEventListener("touchend", this.scroll);
}, 100);
}
}
stopMove(e) {
// 排除可以滚动的区域
if (["content", "keyboard_center"].includes(e.target?.className)) return;
e.preventDefault();
},
scroll() {
window.scrollTo(0, 0);
},
//失去焦点触发
blur(){
setTimeout(() => {
this.container.style.height = `100vh`;
document.removeEventListener("touchmove", this.stopMove);
document.removeEventListener("touchend", this.scroll);
}, 100);
}