zoukankan      html  css  js  c++  java
  • 移动端判断键盘弹出和收起

    根据键盘的展开和收起我们可以判断页面的可视区域的高度来操作,具体代码是这样的

        const originHeight = document.documentElement.clientHeight || document.body.clientHeight;
        window.addEventListener('resize', () => {
          const resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;
          if (resizeHeight < originHeight) {
            console.log('键盘弹起')
          } else {
            this.$refs.input.blur()
              console.log('键盘关闭')
          }
        }, false);

    通过判断页面的高度我可以可以处理一下问题

    1. 当键盘收起的时候让表单失去焦点(注:iOS键盘收起表单会自动失去焦点,但是安卓键盘收起并不会让表单失去焦点)

    2. 当键盘展开时让页面滚动,防止内容被遮挡起来导致页面无法操作

    解决了表单的失去焦点问题,其实表单还有一个问题就是,需要让input滚动到可视区域,方便用户正常输入,可以使用下面代码解决

        window.addEventListener('resize', function() { 
          if( 
            document.activeElement.tagName === 'INPUT' || 
            document.activeElement.tagName === 'TEXTAREA' 
          ) { 
            window.setTimeout(function() { 
              if('scrollIntoView' in document.activeElement) { 
                document.activeElement.scrollIntoView(); 
              } else { 
                document.activeElement.scrollIntoViewIfNeeded(); 
              } 
            }, 0); 
          } 
        });
  • 相关阅读:
    Dockerfile 指令 VOLUME 介绍
    Spring boot(4)-应用打包部署
    Docker Dockerfile详解
    poj-1045(数学不好怪我咯)
    poj-3185-开关问题
    poj-1163-The Triangle
    归并排序(Merge Sort)
    交换排序—快速排序(Quick Sort)
    交换排序—冒泡排序(Bubble Sort)
    选择排序—堆排序(Heap Sort) 没看明白,不解释
  • 原文地址:https://www.cnblogs.com/wuxianqiang/p/10538757.html
Copyright © 2011-2022 走看看