2023-11-15【技术】关于监听手机键盘弹出/隐藏事件

          //ios手机  监听键盘弹出、隐藏事件
          // 键盘弹出事件处理
            window.addEventListener('focusin',this.showMarginBottom());
            //键盘收起事件处理
            window.addEventListener('focusout',this.hideMarginBottom());

            //安卓手机
            let innerHeight = window.innerHeight;
            window.addEventListener('resize', () => {
                let newInnerHeight = window.innerHeight;
                if (innerHeight > newInnerHeight) {
                    // 键盘弹出事件处理
                    this.showMarginBottom();
                } else {
                    // 键盘收起事件处理
                    this.hideMarginBottom();
                }
            })


//具体业务代码
        showMarginBottom(){
            console.log("键盘弹出事件处理");
            this.$refs.area_ctrl.style.marginBottom="-50%";
        },
        hideMarginBottom(){
            console.log("键盘收起事件处理");
            this.$refs.area_ctrl.style.marginBottom="0";
        },

//要记得 移除监听
            window.removeEventListener('resize', () => {
                    let newInnerHeight = window.innerHeight;
                    if (innerHeight > newInnerHeight) {
                        // 键盘弹出事件处理
                        this.showMarginBottom();
                    } else {
                        // 键盘收起事件处理
                        this.hideMarginBottom();
                    }
                })

//ios手机  移除键盘弹出、隐藏事件
// 键盘弹出事件处理
window.removeEventListener('focusin',this.showMarginBottom);
//键盘收起事件处理
window.removeEventListener('focusout',this.hideMarginBottom);
···
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容