uni-app監聽軟鍵盤是否彈起,ios不支持


<script>
    export default {
        data() {
            return {
                defaultPhoneHeight: '', //屏幕默認高度
                nowPhoneHeight: '', //屏幕現在的高度
              }
        },
        watch: {
            //軟鍵盤彈起事件
            nowPhoneHeight() {
                if (this.defaultPhoneHeight != this.nowPhoneHeight) {
                    //手機鍵盤被喚起了。
                    console.log('彈起');
                    //寫軟鍵盤喚起你需要做的操作
                    this.fixed = true
                } else {
                    console.log('收起');
                    //手機鍵盤被關閉了。
                    //寫軟鍵盤關閉你需要做的操作
                    this.fixed = false
                }
            }
        },
        mounted() {
            //監聽軟鍵盤獲取當前屏幕高度的事件
            this.defaultPhoneHeight = window.innerHeight
            console.log('this.defaultPhoneHeight:', this.defaultPhoneHeight);
            window.onresize = () => {
                this.nowPhoneHeight = window.innerHeight
                console.log('this.nowPhoneHeight:', this.nowPhoneHeight);
            }
        },
        // 頁面銷毀一定要移除onresize時間
        beforeDestroy() {
            window.onresize = null
        }
    }
</script>

采用監聽屏幕大小改變,來判斷軟鍵盤是否彈起。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM