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>

    自增前:


    自增后:


  • 相关阅读:
    2019-5-24-WPF-源代码-从零开始写一个-UI-框架
    2019-8-31-dotnet-通过-WMI-获取系统安装的驱动
    2018-12-18-WPF-一个空的-WPF-程序有多少个窗口
    2018-11-20-UWP-开发中,需要知道的1000个问题
    2019-8-31-C#-已知点和向量,求距离的点
    2018-10-31-C#-7.0-使用下划线忽略使用的变量
    2019-3-16-win10-uwp-鼠标移动到图片上切换图片
    MSP432 BSL流程(UART)
    UART串口简介
    C++ STL容器
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7071649.html
Copyright © 2011-2022 走看看