您的位置:首页 > Web前端 > JavaScript

【记录】用Javascript实现文本框textarea高度随内容自动适应增长收缩

2011-05-06 12:29 846 查看
// 最小高度

var minRows = 5;

// 最大高度,超过则出现滚动条

var maxRows = 12;

function autoResize(){

var t = document.getElementById('txt');

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;

}

}

}

查看DEMO-示例

Textarea高度随内容自适应地增长 http://www.msnova.net
// 最小高度
var minRows = 5;
// 最大高度,超过则出现滚动条
var maxRows = 12;
function autoResize(){
var t = document.getElementById('txt');
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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: