欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

文本框根据输入内容自适应高度的代码

程序员文章站 2023-11-17 14:06:41
其实现代浏览器大多都支持文本框尺寸调节功能,绝大多数情况下却没有自动适应来得爽快,在网络上发现一方法比较简单的实现文本框高度自适应,于是封装了这个函数,准备以后应用到项目中。 源代码: 23:03文章...
其实现代浏览器大多都支持文本框尺寸调节功能,绝大多数情况下却没有自动适应来得爽快,在网络上发现一方法比较简单的实现文本框高度自适应,于是封装了这个函数,准备以后应用到项目中。
源代码:
23:03文章更新:
感谢alucelx同学再次给力的帮助,大大简化了方法,更新代码为0.2版本,同时解决了兼容opera浏览器,至此全兼容ie6+与现代浏览器!
在线演示: http://demo.jb51.net/js/2011/autoarea/index.htm
autotextarea.js
复制代码 代码如下:

/**
* 文本框根据输入内容自适应高度
* @author tang bin
* @version 0.3
* @see http://www.planeart.cn/?p=1489
* @param {htmlelement} 输入框元素
* @param {number} 设置光标与输入框保持的距离(默认20)
* @param {number} 设置最大高度(可选)
*/
var autotextarea = function (elem, extra, maxheight) {
extra = extra || 20;
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.maxheight = elem.style.resize = 'none';
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();
};

测试代码:
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>文本框根据输入内容自适应高度</title>
<style type="text/css">
#textarea { font: 1.4em/1.8em arial; overflow: hidden; width: 550px; height: 6em; padding:10px; }
</style>
<script src="autotextarea.js"></script>
</head>
<body style="background:#fbfcfd url(http://goo.gl/klszx);">
<textarea id="textarea"></textarea>
<script>
var text = document.getelementbyid("textarea"),
tip = '想写点什么..';
autotextarea(text);// 调用
text.value = tip;
text.onfocus = function () {
if (text.value === tip) text.value = '';
};
text.onblur = function () {
if (text.value === '') text.value = tip;
};
</script>
</body>
</html>