js插件Autosize实现textarea标签高度自适应,且取消右边滚动条

Autosize插件官方:https://github.com/jackmoore/autosize
案例代码:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>Simple Autosize for textareas</title>
<style>
    textarea {
        padding: 10px;
        vertical-align: top;
        width: 200px;
        resize:none;
        outline:none;
    }
    textarea:focus {
        outline-style: solid;
        outline-width: 2px;
    }
</style>
</head>
<body>
    <h3>max-height 300px</h3>
    <textarea style='max-height: 300px'>The coconut palm (also, cocoanut), Cocos nucifera, is a member of the family Arecaceae (palm family). It is the only accepted species in the genus Cocos.[2] The term coconut can refer to the entire coconut palm, the seed, or the fruit, which, botanically, is a drupe, not a nut. The spelling cocoanut is an archaic form of the word.[3] The term is derived from 16th-century Portuguese and Spanish coco, meaning "head" or "skull",[4] from the three small holes on the coconut shell that resemble human facial features.</textarea>

    <h3>no max-height</h3>
    <textarea>The coconut palm (also, cocoanut), Cocos nucifera, is a member of the family Arecaceae (palm family). It is the only accepted species in the genus Cocos.[2] The term coconut can refer to the entire coconut palm, the seed, or the fruit, which, botanically, is a drupe, not a nut. The spelling cocoanut is an archaic form of the word.[3] The term is derived from 16th-century Portuguese and Spanish coco, meaning "head" or "skull",[4] from the three small holes on the coconut shell that resemble human facial features.</textarea>
</body>
<script src='/autosize.min.js'></script>
<script>
    autosize(document.querySelectorAll('textarea'));
</script>
</html>

Autosize下载:autosize.zip 最好去下载官方最新的版本。

注意,以上案例代码中,还用css3样式去掉textarea标签的右下角拖动元素修改尺寸大小属性(resize:none)和轮廓线(outline:none)。

添加新评论