移动端input输入的时候底部固定的button被键盘顶上去解决方法

问题页面:

input问题页面

解决demo:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>input输入导致的定位问题</title>
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
    <style>
        body,
        html {
            width: 100%;
            height: 100%;
            position: relative;
            padding: 0;
            margin: 0;
        }
    </style>
</head>

<body>
    <div id="index">

        <div style="width:100%;height: 100%;position: relative;overflow: auto;padding:10px;padding-bottom: 50px;box-sizing: border-box;">
            input框:<input type="text">
        </div>
        <div v-show="isShow" style="width: 100%;height: 40px;line-height: 40px;position: absolute;bottom: 0;background-color: #09d;color: #fff;text-align: center;">提交</div>
    </div>
    <script>
        var vm = new Vue({
            el: "#index",
            data: {
                screenHeight: document.body.clientHeight, // 这里是给到了一个默认值 (这个很重要)【屏幕分辨率高度】,
                clientHeight: document.body.clientHeight, //默认高度在watch里拿来做比较  【可视区高度】  
                isShow: true,
            },
            mounted() {
                const that = this
                window.onresize = () => {
                    return (() => {
                        window.screenHeight = document.body.clientHeight
                        that.screenHeight = window.screenHeight
                    })()
                }
            },
            watch: {
                screenHeight(val) {
                    if (this.clientHeight != val) {
                        this.isShow = false;
                    } else {
                        this.isShow = true;
                    }
                }
            },
        })
    </script>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题通常可以通过监听软键盘的打开和关闭事件来解决。可以使用 Vue.js 的 `$nextTick()` 方法和 `window.innerHeight` 属性来获取页面高度和视口高度。具体实现步骤如下: 1. 在 `mounted()` 钩子函数中,添加软键盘打开和关闭事件的监听函数。 ```javascript mounted() { window.addEventListener('resize', this.handleResize) window.addEventListener('scroll', this.handleScroll) window.addEventListener('focusin', this.handleFocusIn) window.addEventListener('focusout', this.handleFocusOut) } ``` 2. 实现监听函数,根据软键盘的状态计算页面高度和视口高度,并更新页面的滚动位置。 ```javascript methods: { handleResize() { if (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA') { this.isKeyboardOpened = true } else { this.isKeyboardOpened = false } this.updateViewportHeight() }, handleScroll() { if (!this.isKeyboardOpened) return this.updateViewportHeight() }, handleFocusIn() { this.isKeyboardOpened = true this.updateViewportHeight() }, handleFocusOut() { this.isKeyboardOpened = false this.updateViewportHeight() }, updateViewportHeight() { const height = window.innerHeight const input = this.$refs.input const inputHeight = input.offsetHeight const inputOffsetTop = input.getBoundingClientRect().top const offset = window.scrollY const scrollHeight = document.body.scrollHeight const keyboardHeight = height - inputHeight - inputOffsetTop + offset if (keyboardHeight > 0) { document.body.style.height = scrollHeight + keyboardHeight + 'px' document.body.style.overflow = 'hidden' window.scrollTo(0, keyboardHeight) } else { document.body.style.height = '' document.body.style.overflow = '' window.scrollTo(0, 0) } } } ``` 3. 在页面中添加输入框,并绑定 ref 属性。 ```html <template> <div> <input type="text" ref="input"> </div> </template> ``` 这样,当软键盘打开时,页面自动调整滚动位置,避免输入框被遮挡。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值