码迷,mamicode.com
首页 > 其他好文 > 详细

文本域自适应(textarea)

时间:2019-06-24 22:38:32      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:fir   scree   group   网址   offset   内容   query   length   def   

文本域自适应

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文本域自适应高度(模拟有道词典。。)</title>
    <link rel="stylesheet" href="../static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/my_css.css">
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">文本域自适应</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li ><a href="#">测试1 <span class="sr-only">(current)</span></a></li>
                <li><a href="#">测试2</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">选项卡 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">卡1</a></li>
                        <li><a href="#">卡2</a></li>
                        <li><a href="#">卡3</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">卡4</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">退出卡</a></li>
                    </ul>
                </li>
            </ul>
            <form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="请输入xxx">
                </div>
                <button type="submit" class="btn btn-default">搜索</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">user</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">个人中心 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">卡1</a></li>
                        <li><a href="#">卡2</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">卡3</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="container-fluid box" style="padding-right: 10px">
    <div class="row box-wrap">
        <div>
            <div class="col-md-5 fanyi__input__bg col-md-offset-1">
                <textarea id="inputOriginal" dir="auto" class="input__original__area" placeholder="请输入你要翻译的文字或网址" onchange="wenbenyuchange()"></textarea>
            </div>
        </div>
        <div>
            <div class="col-md-5 fanyi__input__bg">
                <textarea id="inputOriginal_1" dir="auto" class="input__original__area" placeholder="请输入你要翻译的文字或网址"></textarea>
            </div>
        </div>

    </div>
</div>


<div class="footer" style="margin-top: 30px">
    <div class="row">
        <div class="col-md-12 text-center">
            <span>©2019 前端</span>
        </div>
    </div>
</div>
<script type="text/javascript" src="../static/JS/jquery-3.3.1.js"></script>
<script type="text/javascript" src="../static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script type="text/javascript">
    //----------底部自适应
    function myfooter() {
        if ($(document.body).height() < $(window).height()) {
            $(".footer").css({position: fixed, bottom: 0, width: 100%});
        }
        $(window).resize(function () {
            if ($(document.body).height() < $(window).height()) {
                $(".footer").css({position: fixed, bottom: 0, width: 100%});
            } else {
                $(".footer").css({position: relative});
            }
        });
    }
    myfooter();
    //----------
    $(function () {
        // 为每一个textarea绑定事件使其高度自适应
        $.each($("textarea"), function (i, n) {
            autoTextarea($(n)[0]);
        });
    });
    /**
     * 文本框根据输入内容自适应高度
     * {HTMLElement}   输入框元素
     * {Number}        设置光标与输入框保持的距离(默认0)
     * {Number}        设置最大高度(可选)
     */
    var autoTextarea = function (elem, extra, maxHeight) {
        extra = extra || 0;
        var isFirefox = !!document.getBoxObjectFor || mozInnerScreenX in window,
            isOpera = !!window.opera && !!window.opera.toString().indexOf(Opera),
            addEvent = function (type, callback) {
                elem.addEventListener ?
                    elem.addEventListener(type, callback, false) :
                    elem.attachEvent(on + type, callback);
            },
            getStyle = elem.currentStyle ?
                function (name) {
                    var val = elem.currentStyle[name];
                    if (name === height && val.search(/px/i) !== 1) {
                        var rect = elem.getBoundingClientRect();
                        return rect.bottom - rect.top -
                            parseFloat(getStyle(paddingTop)) -
                            parseFloat(getStyle(paddingBottom)) + px;
                    }
                    return val;
                } : function (name) {
                    return getComputedStyle(elem, null)[name];
                },
            minHeight = parseFloat(getStyle(height));
        elem.style.resize = both;//如果不希望使用者可以自由的伸展textarea的高宽可以设置其他值

        var change = function () {
            var scrollTop, height,
                padding = 0,
                style = elem.style;

            if (elem._length === elem.value.length) return;
            elem._length = elem.value.length;

            if (!isFirefox && !isOpera) {
                padding = parseInt(getStyle(paddingTop)) + parseInt(getStyle(paddingBottom));
            }
            ;
            scrollTop = document.body.scrollTop || document.documentElement.scrollTop;

            elem.style.height = minHeight + px;
            if (elem.scrollHeight > minHeight) {
                if (maxHeight && elem.scrollHeight > maxHeight) {
                    height = maxHeight - padding;
                    style.overflowY = auto;
                } else {
                    height = elem.scrollHeight - padding;
                    style.overflowY = hidden;
                }
                ;
                style.height = height + extra + px;
                scrollTop += parseInt(style.height) - elem.currHeight;
                document.body.scrollTop = scrollTop;
                document.documentElement.scrollTop = scrollTop;
                elem.currHeight = parseInt(style.height);
            }
            ;
        };

        addEvent(propertychange, change);
        addEvent(input, change);
        addEvent(focus, change);
        change();


    };
    function wenbenyuchange() {
        var $textdata = $(#inputOriginal);
        $(html ,body).animate({scrollTop: 0},0);
        $textdata.css(font-size,14px);
        console.log($textdata.height());
        $(#inputOriginal_1).css(height,$textdata.height())
    }

</script>
</body>
</html>

CSS

body {
    overflow-x: hidden;
}

textarea {
    outline: none;
}

.fanyi__input__bg {
    background: #f2f2f2;
    position: relative;
    z-index: 1;
    overflow: hidden;
    border-radius: 6px;
    margin-right: 10px;
    padding: 18px 50px 18px 18px;
    border: 1px solid transparent
}

.input__original__area {
    display: block;
    font-family: ArialMT, "Microsoft YaHei", Arial, sans-serif, Helvetica, STHeiti;
    width: 100%;
    min-height: 156px;
    padding: 0;
    border: 0;
    background: 0 0;
    font-size: 24px;
    color: #333;
    line-height: 30px;
    overflow: auto
}

.input__original__area::placeholder {
    font-family: Arial-BoldMT;
    font-size: 24px;
    color: rgba(102, 102, 102, .3)
}

.input__original__area::-moz-placeholder {
    font-family: Arial-BoldMT;
    font-size: 24px;
    color: rgba(102, 102, 102, .3)
}

.input__original__area::-webkit-input-placeholder {
    font-family: Arial-BoldMT;
    font-size: 24px;
    color: rgba(102, 102, 102, .3)
}

.input__original__area:-ms-input-placeholder {
    font-family: Arial-BoldMT;
    font-size: 24px;
    color: rgba(102, 102, 102, .3)
}

.input__original__area::selection {
    background-color: #cfe5ff
}

.input__original__area:focus {
    border: none
}

.fanyi__input__bg__ar {
    padding: 18px 18px 18px 50px
}

.fanyi__input__bg:focus {
    border: 1px solid #0e7dff
}

.input__target .fanyi__input__bg {
    margin-right: 0;
    margin-left: 10px;
    padding: 0
}

结语

项目需求+搜索资料(JQ+bootstrap)

文本域自适应(textarea)

标签:fir   scree   group   网址   offset   内容   query   length   def   

原文地址:https://www.cnblogs.com/nixindecat/p/11079897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!