vue:移動端判斷鍵盤事件,相容安卓ios

Mosowe發表於2020-09-27
    if (!this.$isIos) { // 安卓端
      const innerHeight = window.innerHeight;
      window.addEventListener('resize', () => {
        const newInnerHeight = window.innerHeight;
        if (innerHeight > newInnerHeight) {
          // 鍵盤彈出事件
          this.$store.commit('setKeyboard', true);
        } else {
          // 鍵盤收起事件
          this.$store.commit('setKeyboard', false);
        }
      });
    } else { // ios端
      window.addEventListener('focusin', () => {
        // 鍵盤彈出事件處理
        this.$store.commit('setKeyboard', true);
      });
      window.addEventListener('focusout', () => {
        // 鍵盤收起事件
        this.$store.commit('setKeyboard', false);
        
      });
    }

相關文章