固定input框(或者div模拟的input框)在页面底部

前提

使用position:fixed;直接固定input框在页面底部,ios可能会出现位置错乱,而且在输入框失去焦点后页面无法回弹
完整代码如下:

<style>
   body {
        font-size: .26rem;
        -webkit-user-select:none;
    }

    .input {
        width: 200px;
        height: 24px;
        line-height: 24px;
        font-size: 14px;
        padding: 5px 8px;
        border: 1px solid #ddd;
        -webkit-user-select: text;
    }

    .input:empty::before {
        content: attr(placeholder);
        color: red;
    }

    #footer {
        position: fixed;
        bottom: 0;
        width: 100%;
    }
</style>

html代码:
<footer id="footer">
   <div>
     <!--  两者选其一即可 -->
       <!--  使用div模拟input框 -->
       <div class="input" contenteditable placeholder="请输入文字" onblur="temporaryRepair()"></div>
        <!--  使用input框 -->
       <input class="input" type="text" placeholder="请输入" onblur="temporaryRepair()">
    </div>
</footer>

<script>
    function temporaryRepair() {
        var currentPosition, timer;
        var speed = 1; //页面滚动距离
        timer = setInterval(function() {
            currentPosition = document.documentElement.scrollTop || document.body.scrollTop;
            currentPosition -= speed;
            window.scrollTo(0, currentPosition); //页面向上滚动
            currentPosition += speed; //speed变量
            window.scrollTo(0, currentPosition); //页面向下滚动
            clearInterval(timer);
        }, 1);
    }
</script>

代码介绍

  • -webkit-user-select:设置或检索是否允许用户选中文本。只要在当前模拟input的div上设置-webkit-user-select: text就可以输入了。
  • contenteditable:让div变成可以编辑的
  • 函数temporaryRepair():解决当输入框失去焦点时页面不回弹的问题。可参考这里
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一个简单的HTML+CSS模拟百度搜索网页的示例代码。以下是代码: ```html <!DOCTYPE html> <html> <head> <title>模拟百度搜索</title> <style> /* 设置页面样式 */ body { font-family: Arial, sans-serif; background-color: #f2f2f2; } /* 设置搜索框样式 */ .search-box { width: 500px; margin: 50px auto; background-color: #fff; border: 1px solid #ccc; border-radius: 4px; } /* 设置搜索框中的输入框样式 */ .search-box input[type="text"] { width: 400px; padding: 10px; border: none; font-size: 16px; } /* 设置搜索框中的搜索按钮样式 */ .search-box input[type="submit"] { width: 80px; height: 40px; background-color: #3385ff; color: #fff; border: none; border-radius: 4px; font-weight: bold; font-size: 16px; cursor: pointer; } /* 设置搜索框中的按钮容器样式 */ .search-box .button-container { display: inline-block; margin-left: 20px; } /* 设置搜索框中的底部文字样式 */ .search-box .bottom-text { color: #999; font-size: 14px; text-align: right; padding: 10px; } </style> </head> <body> <div class="search-box"> <form> <input type="text" placeholder="请输入搜索内容"> <div class="button-container"> <input type="submit" value="搜索"> </div> </form> <div class="bottom-text">©2021 模拟百度搜索</div> </div> </body> </html> ``` 这段代码将生成一个简单的搜索框,包括一个输入框和一个搜索按钮,以及底部的版权信息。你可以在浏览器中打开这个文件来查看它的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值