Commit e3996abd authored by 郭志伟's avatar 郭志伟

fix: 键盘监听事件优化

parent e2dfd947
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
const LEFT_BTN_CLICK_EVENT = "leftClick"; const LEFT_BTN_CLICK_EVENT = "leftClick";
const RIGHT_BTN_CLICK_EVENT = "click"; const RIGHT_BTN_CLICK_EVENT = "click";
import settings from "@/api/settings"; import settings from "@/api/settings";
import { onKeyboardStateChange, offKeyboardStateChange } from "@/service/utils"; import { onKeyboardStateChange } from "@/service/utils";
export default { export default {
name: "GoodAction", name: "GoodAction",
props: { props: {
...@@ -67,7 +67,7 @@ export default { ...@@ -67,7 +67,7 @@ export default {
}); });
}, },
destroyed() { destroyed() {
offKeyboardStateChange(); onKeyboardStateChange(() => {}, true);
}, },
methods: { methods: {
leftClick() { leftClick() {
......
...@@ -247,10 +247,11 @@ export function getBirthDate(idCard) { ...@@ -247,10 +247,11 @@ export function getBirthDate(idCard) {
/** /**
* @description: 移动端监听软键盘弹起或收起,IOS,android * @description: 移动端监听软键盘弹起或收起,IOS,android
* @param {Boolean} off 是否销毁
* @return {Function} callback 软键盘弹起或收取回调, true => 弹起, false => 隐藏 * @return {Function} callback 软键盘弹起或收取回调, true => 弹起, false => 隐藏
*/ */
export function onKeyboardStateChange(callback = () => {}) { export function onKeyboardStateChange(callback = () => {}, off = false) {
if (isIOS) { if (isIOS) {
let isReset = true; //是否归位 let isReset = true; //是否归位
...@@ -269,8 +270,13 @@ export function onKeyboardStateChange(callback = () => {}) { ...@@ -269,8 +270,13 @@ export function onKeyboardStateChange(callback = () => {}) {
} }
}, 30); }, 30);
}; };
document.body.addEventListener("focusin", focusinHandler); if (off) {
document.body.addEventListener("focusout", focusoutHandler); document.body.removeEventListener("focusin", focusinHandler);
document.body.removeEventListener("focusout", focusoutHandler);
} else {
document.body.addEventListener("focusin", focusinHandler);
document.body.addEventListener("focusout", focusoutHandler);
}
} }
if (isAndroid) { if (isAndroid) {
...@@ -294,33 +300,10 @@ export function onKeyboardStateChange(callback = () => {}) { ...@@ -294,33 +300,10 @@ export function onKeyboardStateChange(callback = () => {}) {
callback(false); callback(false);
} }
}; };
window.addEventListener("resize", resizeHandler); if (off) {
} window.removeEventListener("resize", resizeHandler);
} } else {
window.addEventListener("resize", resizeHandler);
/** }
* @description: 销毁软键盘监听
*/
export function offKeyboardStateChange(callback = () => {}) {
if (isIOS) {
const focusinHandler = () => {
console.log("focusin已卸载");
callback();
};
const focusoutHandler = () => {
console.log("focusout已卸载");
callback();
};
document.body.removeEventListener("focusin", focusinHandler);
document.body.removeEventListener("focusout", focusoutHandler);
}
if (isAndroid) {
const resizeHandler = () => {
console.log("resize已卸载");
callback();
};
window.removeEventListener("resize", resizeHandler);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment