4种方法实现文本域textarea自适应

方法一:class复用实现textarea自适应

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en">
<html>

	<head>
		<title>demo</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<style>
			.text-adaption {
				width: 300px;
				height: 34px;
				overflow: hidden;
				padding: 5px 10px;
				resize: none;
				line-height: 24px;
				font-size: 12px;
				color: #666;
				border: 1px solid #ccc;
				outline: 0 none;
				border-radius: 3px;
				box-sizing: border-box;
			}
						
		</style>

	</head>

	<body>
		<textarea id="text-adaption" class="text-adaption" rows="1" >abcd</textarea>
		
		<textarea  class="text-adaption" rows="1" >abcd</textarea>
		
		<textarea  class="text-adaption" rows="1" >abcd</textarea>
	</body>

</html>
<script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
<script>
	/*function $(id) {
		return document.getElementById(id);
	}

	$("text-adaption").onkeyup = function() {
		this.style.height = 'auto';
		this.style.height = this.scrollHeight + "px";
	}*/
$(function(){
	function getClass(c){
		return document.getElementsByClassName(c);
	}
	var obj=getClass("text-adaption");
	var len=obj.length;
	
	for(var i=0;i<len;i++){
		obj[i].onkeyup = function() {
			this.style.height = 'auto';
			this.style.height = this.scrollHeight + "px";
		};
	}
	
});
</script>

方法二:div模拟textarea

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en">
<html>

	<head>
		<title>div模拟css实现textarea自适应高度 </title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<style>
        .tip{
            margin: 30px auto;
            text-align: center;
        }
        .test-textarea {
            width: 400px;
            min-height: 26px;
            line-height: 20px;
            _height: 30px;
            /* max-height: 150px;*/
            margin-left: auto;
            margin-right: auto;
            padding: 3px;
            outline: 0;
            border: 1px solid #ccc;
            font-size: 12px;
            word-wrap: break-word;
            overflow-x: hidden;
            overflow-y: auto;
            -webkit-user-modify: read-write-plaintext-only;
            border-radius: 4px;
        }
    </style>
	</head>

	<body>
		<h3 class="tip">div模拟css实现textarea自适应高度 </h3>
		<div class="test-textarea" contenteditable="true" ><br /></div>
	</body>

</html>

方法三:id单一实现textarea自适应

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en">
<html>

	<head>
		<title>demo</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<style>
			.text-adaption {
				width: 300px;
				height: 34px;
				overflow: hidden;
				padding: 5px 10px;
				resize: none;
				line-height: 24px;
				font-size: 12px;
				color: #666;
				border: 1px solid #ccc;
				outline: 0 none;
				border-radius: 3px;
				box-sizing: border-box;
			}
						
		</style>

	</head>

	<body>
		<textarea id="text-adaption" class="text-adaption" rows="1"></textarea>
	</body>

</html>
<script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
<script>
	function $(id) {
		return document.getElementById(id);
	}

	$("text-adaption").onkeyup = function() {
		this.style.height = 'auto';
		this.style.height = this.scrollHeight + "px";
	}
</script>

方法四:js实现textarea自适应

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en">
<html>

	<head>
		<title>原生js实现文本框根据输入内容自适应高度 </title>
		<style>
        .tip{
            margin: 30px auto;
            text-align: center;
        }

        #textarea {
            display: block;
            margin: 0 auto;
            overflow: hidden;
            width: 550px;
            font-size: 14px;
            height: 18px;
            line-height: 24px;
            padding: 2px;
            outline: 0 none;
            border-color: rgba(82, 168, 236, 0.8);
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6);
        }
    </style>
	</head>
	<script type="text/javascript">
		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 = '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();
		};
	</script>

	<body>

		<h3 class="tip">原生js实现文本框根据输入内容自适应高度</h3>
		<textarea id="textarea" placeholder="回复内容"></textarea>

		<script>
			var text = document.getElementById("textarea");
			autoTextarea(text); //调用
		</script>

	</body>

</html>

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值