zoukankan      html  css  js  c++  java
  • h5 ios的软键盘会把固定定位在底部的导航栏顶上去的解决办法

    这个BUG 主要是固定在 ios里面不生效导致的,只要键盘弹起 就会把整个界面给弹上去,尝试了网上各种办法都没有很好地解决

    后来自己用代码把固定定位的元素给拽下来的 原理就是监听滚动 把固定的元素手动抓下来

    看代码

     var u = navigator.userAgent;
        var isIOS = !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    
        if (isIOS) {
          document.body.addEventListener('focusin', () => {// 软键盘弹起事件
            window.onscroll = function (e) {
              clearTimeout(that.time2)
              let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
              document.querySelector('.djq-editor-header').style.top = scrollTop + 'px';
              document.querySelector('.djq-editor-header').style.display = 'none';
              document.querySelector('.djq-editor-container').style.height = '300' + 'px';
    
              that.time2 = setTimeout(() => {// 这边是 判断键盘弹起的状态下 滚动结束后的状态
                let t2 = document.documentElement.scrollTop || document.body.scrollTop;
                that.setState({t2});
                if (t2 == scrollTop) {
                  document.querySelector('.djq-editor-header').style.display = 'flex';
                }
              }, 180);
            }
          })
    
          document.body.addEventListener('focusout', () => {// 
            window.onscroll = null;
            window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
            document.querySelector('.djq-editor-header').style.top = 0 + 'px';
            document.querySelector('.djq-editor-header').style.display = 'flex';
            document.querySelector('.djq-editor-container').style.height = '600px';
            clearTimeout(that.time2)
          })
        }
    

      原理是就是 键盘弹起时 监听页面的滚动 把弹上去的元素给拽下来,因为是实时监听,所以会有闪烁,这个时候我再滚动时候把他给隐藏了

    然后再滚动结束的时候,再把他显示出来,这个时候就要写一个监听合适滚动结束的函数。。。代码就在上面 原理是 在滚动之后某个时间段 

    用定时器去判断最后的scrolltop是否与实时滚动的scrolltop是否一致。。这个办法亲测有效。。有疑惑找我。。

    但是最后 需求还是被砍了

  • 相关阅读:
    PAT 1035. 插入与归并(25)
    PAT 1034. 有理数四则运算(20)
    PAT 1033. 旧键盘打字(20)
    PAT 1032. 挖掘机技术哪家强(20)
    PAT 1031. 查验身份证(15)
    PAT 1030. 完美数列(25)
    PAT 1029. 旧键盘(20)
    PAT 1028. 人口普查(20)
    PAT 1027. 打印沙漏(20)
    PAT 1026. 程序运行时间(15)
  • 原文地址:https://www.cnblogs.com/lisiyang/p/10573264.html
Copyright © 2011-2022 走看看