1. 程式人生 > >textarea高度自適應

textarea高度自適應

area 方案 any 復用 his input extra function csdn

textarea高度自適應

有時候寫表單的時候,會有一個 備註框textarea。
因為textarea不支持自適應高度,就是定好高度或者是行數之後,超出部分就會顯示滾動條,看起來不美觀。
我們需要美觀實現的效果:默認顯示一行。當輸入的文字超過一行或者輸入Enter時,
輸入框的高度會隨著改變,直到輸入完畢。也就是要實現textarea的高度自適應

技術分享

=========================================================

方案A:用div來模擬textarea實現的,用CSS控制樣式,不用JS。

而用DIV來模擬時,首先遇到的問題是:div怎麽實現輸入功能?
一個普通的block元素上加個contenteditable="true"就實現編輯,出現光標了。
如<div contenteditable="true"></div>

<div class="test-textarea" contenteditable="true" ><br /></div>

.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;
}

===================================================

方案B:Js實現textarea自適應

由於第一種方法,改變了原有項目裏面的結構標簽 textarea需要改變為div 則,在原有基礎上進行js修改來達到輸入框自適應 <textarea id="textarea" placeholder="回復內容"></textarea> <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> ================================ 方案C:JQ實現textarea自適應(Id實現)

.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;
}

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

<script>
function $(id) {
return document.getElementById(id);
}

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

===========================================

方案D:JQ實現textarea自適應(class復用實現)

.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;
}

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

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

$(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";
};
}

});

=================================================

綜合下載地址:http://files.cnblogs.com/files/leshao/textarea%E9%AB%98%E5%BA%A6%E8%87%AA%E9%80%82%E5%BA%94.rar 參考網友地址鏈接:http://blog.csdn.net/tianyitianyi1/article/details/49923069

textarea高度自適應