zoukankan      html  css  js  c++  java
  • 文本输入框自适应高度

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
        <HTML>

        <HEAD>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <TITLE>文本框textarea高度自适应增长/伸缩</TITLE>
        </HEAD>

        <BODY>
            <textarea id="txtContent1" rows="5" cols="50" onkeyup="ResizeTextarea(1)" style="overflow-y:hidden;resize: none;">Textarea高度随内容自适应地增长,无滚动栏
            </textarea>
            <script type="text/javascript">
                 // 最小高度
                var minRows = 5;
                 // 最大高度。超过则出现滚动栏
                var maxRows = 60;

                function ResizeTextarea(index) {
                    var t = document.getElementById('txtContent' + index);
                    if (t.scrollTop == 0) t.scrollTop = 1;
                    while (t.scrollTop == 0) {
                        if (t.rows > minRows)
                            t.rows--;
                        else
                            break;
                        t.scrollTop = 1;
                        if (t.rows < maxRows)
                            t.style.overflowY = "hidden";
                        if (t.scrollTop > 0) {
                            t.rows++;
                            break;
                        }
                    }
                    while (t.scrollTop > 0) {
                        if (t.rows < maxRows) {
                            t.rows++;
                            if (t.scrollTop == 0) t.scrollTop = 1;
                        } else {
                            t.style.overflowY = "auto";
                            break;
                        }
                    }
                }
            </script>
        </BODY>

        </HTML>

    自增前:


    自增后:


  • 相关阅读:
    关于 NSTimer 和 NSRunLoop 的一些理解
    通过 CocoaPods 集成 WeexSDK 到iOS项目中
    iOS 从相册取出的图片默认 取中间部分 裁剪成方形的
    Trilynn分享了炼数成金邀请码
    highcharts分段显示不同颜色
    H5手机开发锁定表头和首列(惯性滚动)解决方案
    为移动端开发提供纯前端的路由方案
    ionic系列
    2014总结
    margin 相关 bug 系列
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7071649.html
Copyright © 2011-2022 走看看