解决h5安卓手机软键盘弹起布局压缩变形

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset='utf-8'>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=0" />
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="format-detection" content="telphone=no, email=no"/>
    <link rel="stylesheet" href="css/public.css"/>
    <link rel="stylesheet" href="css/index.css"/>
    <title></title>
</head>
<body>
   <div class="logon">
         <div class="logon_input">
             <div class="input_block">
                 <input id="ID" type="text" placeholder = "请输入手机号码" style="width:100%" class="inp"/>
             </div>
             <div class="input_block" style="margin-top:1.5rem;">
                 <input id="password" type="password" placeholder = "请输入密码" style="width:100%" class="inp"/>
             </div>
             <div class="logon_btn">
                 <div id="Button13" onclick ="onchick('one')">登录</div>
             </div>
         </div>
   </div>
   <div class="logon_bg">
       <img src="images/bg.png" alt=""/>
   </div>
</body>
<script src="JS/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="JS/JSindex.js" type="text/javascript"></script>
<script>
    var originalHeight=document.documentElement.clientHeight ||document.body.clientHeight;
    window.onresize=function(){
        var resizeHeight=document.documentElement.clientHeight || document.body.clientHeight;
        if(resizeHeight-0<originalHeight-0){
           $(".logon_bg").hide();
        }else{
            $(".logon_bg").show();
        }
    }
</script>
</html>
.logon{
    width: 100%;
    position: absolute;
    top: 25%;
}
.logon_input{
    width: 85%;margin:auto;
}
.input_block{
    height: 2.5rem;
    border-bottom: 1px solid #e6e6e6;
}
input::-webkit-input-placeholder {
    color: #333333;
    font-size: 1rem;
}
.input_block input{
    line-height: 2.5rem;
    text-indent: 20px;
}
.logon_btn {
    display: inline-block;
    width: 100%;
    background-color: #34C0E2;
    color: #fff;
    white-space: nowrap;
    text-align: center;
    border: none;
    cursor: pointer;
    font-size: 1rem;
    border-radius: 20px;
    padding: .6rem 0;
    margin-top: 2.5rem;
}
.logon_bg{
    position: absolute;
    width:100%;
    bottom: 0;
}

每次记录一点点,每次进步一点点

转载于:https://www.cnblogs.com/9608kai/p/10251201.html

在Vue.js开发的H5应用中,有时安卓手机上使用软键盘会覆盖网页内容,导致页面滚动区域被隐藏。这是因为当用户点击输入框时,系统默认会将焦点切换到该输入框,并显示软键盘,这通常会导致页面顶部下移。解决这个问题的方法有几种: 1. **手动调整布局**:在页面加载完成后,监听window的resize事件,检查是否需要调整页面的样式,例如通过JavaScript设置body的padding-top等于键盘的高度。 ```javascript new Vue({ mounted() { document.addEventListener('DOMContentLoaded', function () { const keyboardHeight = document.body.scrollHeight - window.innerHeight; if (keyboardHeight > 0) { window.addEventListener('resize', handleResize); } }); function handleResize() { const keyboardHeight = document.body.scrollHeight - window.innerHeight; if (keyboardHeight > 0) { // 更新页面布局,如设置scrollTop、offsetTop等 this.$refs.yourInputEl.offsetTop; // 替换为实际的输入元素ID或引用 } else { window.removeEventListener('resize', handleResize); } } }, }); ``` 2. **使用CSS Hack**:针对某些浏览器,可以尝试使用`@media screen and (-webkit-min-device-pixel-ratio:0)`来调整viewport和position属性,以适应键盘出现的情况。 ```css @media screen and (-webkit-min-device-pixel-ratio:0) { body { padding-bottom: env(safe-area-inset-bottom); // 或者设置具体的键盘高度值 } } ``` 3. **利用Vue自定义指令**:创建一个全局指令,处理键盘显示和隐藏时页面的自动调整。 ```javascript Vue.directive('hideKeyboard', { inserted: function (el) { el.addEventListener('focus', function () { // 监听输入框获取焦点时,隐藏其他元素 // 或者直接处理页面滚动 const keyboardHeight = getKeyboardHeight(); if (keyboardHeight > 0) { el.style.position = 'fixed'; el.style.top = keyboardHeight + 'px'; } }); el.addEventListener('blur', function () { // 键盘失去焦点时恢复原状 el.style.position = ''; el.style.top = ''; }); } }); function getKeyboardHeight() { /* 获取键盘高度的方法 */ } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值