h5 ios的软键盘会把固定定位在底部的导航栏顶上去的解决办法

博客主要讲述了 iOS 系统中键盘弹起导致固定定位元素界面上移的 BUG。尝试多种网上方法无果后,采用监听滚动手动拽下元素的方式解决,虽实时监听有闪烁,但通过滚动时隐藏、结束时显示,并编写监听滚动结束函数,亲测有效,不过最终需求被砍。

这个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是否一致。。这个办法亲测有效。。有疑惑找我。。

但是最后 需求还是被砍了

 

转载于:https://www.cnblogs.com/lisiyang/p/10573264.html

在HTML5(H5)页面中,当输入框获得焦点并调起键盘时,页面可能会被键盘顶上去。这种情况在移动设备上尤为常见。以下是几种解决方法: 1. **使用`viewport`元标签**: 在HTML的`<head>`部分添加或修改`viewport`元标签,添加`maximum-scale`、`minimum-scale`和`user-scalable`属性,防止页面缩放。 ```html <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> ``` 2. **使用CSS固定布局**: 使用CSS的`position: fixed`属性将输入框固定在页面上,防止页面滚动。 ```css .input-container { position: fixed; bottom: 0; width: 100%; background: white; padding: 10px; box-sizing: border-box; } ``` 3. **使用JavaScript控制滚动**: 在输入框获得焦点时,记录当前的滚动位置;在输入框失去焦点时,恢复之前的滚动位置。 ```javascript const input = document.getElementById('myInput'); let scrollPosition; input.addEventListener('focus', () => { scrollPosition = window.scrollY; }); input.addEventListener('blur', () => { window.scrollTo(0, scrollPosition); }); ``` 4. **使用`input`事件监听**: 监听`input`事件,在输入框获得焦点时调整页面布局。 ```javascript const input = document.getElementById('myInput'); input.addEventListener('focus', () => { document.body.style.marginBottom = '300px'; // 根据键盘高度调整 }); input.addEventListener('blur', () => { document.body.style.marginBottom = '0'; }); ``` 这些方法可以根据具体需求和页面布局进行调整和组合使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值