九浪九 2021-06-30 13:27 采纳率: 83.3%
浏览 447
已采纳

移动端input输入时,页面被顶起往上移?

.改为[]获取

img 想达到这个效果如何做?点输入框时,移动端键盘弹起输入框被顶起,下面是一层不动?

  • 写回答

2条回答 默认 最新

  • MisterMister 2021-06-30 17:47
    关注

    $(function () { var bottomHeight=null; var documentHeight = null; //处理输入框被输入法遮挡的问题 $(document).on('focus', 'input[type="text"],input[type="password"],TEXTAREA', function (e){// 监听输入框是否选中 if (finedo.fn.isnon(documentHeight)) { documentHeight = $(document).height(); } var scrollT = document.documentElement.scrollTop || document.body.scrollTop; //滚动条的垂直偏移 var clientH = document.documentElement.clientHeight || document.body.clientHeight; //元素的可见高度 var scrollH = ""; //元素的整体高度

        var bodyScrollHeight = 0
        var documentScrollHeight = 0
        if (document.body) {
          bodyScrollHeight = document.body.scrollHeight
        }
        if (document.documentElement) {
          documentScrollHeight = document.documentElement.scrollHeight
        }
        // 当页面内容超出浏览器可视窗口大小时,Html的高度包含body高度+margin+padding+border所以html高度可能会大于body高度
        scrollH = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
        
        if (scrollT >= scrollH - clientH) {//判断滚动条是否到底部
            bottomHeight = $(window).height() - $(this).height() - $(this).offset().top + $(document).scrollTop();// 当前选中元素距离屏幕底部距离
            if( bottomHeight < 300 ){//判断元素距离底部小于300
                /*var u = navigator.userAgent;
                var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
                if(isAndroid){*/
                if(getDeviceType() == "android"){
                  
                   // 若距离底部距离小于300
                   var a = $(document).height();
                   $('body').height( $(document).height()+300-bottomHeight);
                   var b = $(document).height();
                   
                   var height = e.delegateTarget.body.offsetHeight;
                   $('body').scrollTop(height);
                }
            }
        }    
    });
    
    $(document).on('blur', 'input[type="text"],input[type="password"],TEXTAREA', function (e){
        if( bottomHeight < 300 ){
            /*var u = navigator.userAgent;
            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            if(isAndroid){*/
            if(getDeviceType() == "android"){
               $('body').height(documentHeight);
               bottomHeight = 0;
            }
        }
    });
    

    });

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分