//点击键盘确认
document.getElementById("search").addEventListener("keydown", function (e) {
var self = this;
if (e.keyCode == "13") {
var search = $(self).val();
//点击键盘确认后的操作
}
});
//监听键盘收起(Android 处理) start
const originHeight = document.documentElement.clientHeight || document.body.clientHeight;
this.resizeHandler = () => {
const resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;
const activeElement = document.activeElement;
if (resizeHeight < originHeight) {
// 键盘弹起后逻辑
if (activeElement && (activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA")) {
setTimeout(()=>{
activeElement.scrollIntoView({ block: 'center' });//焦点元素滚到可视区域的问题
},0)
}
} else {
// 键盘收起后逻辑
}
};
window.addEventListener('resize', this.resizeHandler);
//监听键盘收起(Android 处理) end
//监听键盘收起(IOS 处理) start
let isReset = true; //是否归位
this.focusinHandler = () => {
isReset = false; //聚焦时键盘弹出,焦点在输入框之间切换时,会先触发上一个输入框的失焦事件,再触发下一个输入框的聚焦事件
};
this.focusoutHandler = () => {
isReset = true;
setTimeout(() => {
//当焦点在弹出层的输入框之间切换时先不归位
if (isReset) {
window.scroll(0, 0); //确定延时后没有聚焦下一元素,是由收起键盘引起的失焦,则强制让页面归位
}
}, 30);
};
document.body.addEventListener('focusin', this.focusinHandler);
document.body.addEventListener('focusout', this.focusoutHandler);
//监听键盘收起(IOS 处理) end
app对键盘的确认、收起操作
最新推荐文章于 2023-02-06 18:03:38 发布