1. 程式人生 > 其它 >uni-app監聽軟鍵盤是否彈起,ios不支援

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>

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