本文給大家分享的是Jquery實(shí)現(xiàn)textarea根據(jù)文本內(nèi)容自適應(yīng)高度,這些在平時(shí)的項(xiàng)目中挺實(shí)用的,所以抽空封裝了一個(gè)文本框根據(jù)輸入內(nèi)容自適應(yīng)高度的插件,這里推薦給小伙伴們。
在玩微博的時(shí)候我們可能會(huì)注意到一個(gè)細(xì)節(jié)就是不管是新浪微博還是騰訊微博在轉(zhuǎn)發(fā)和評(píng)論的時(shí)候給你的默認(rèn)文本框的高度都不會(huì)很高,這可能是版面的限制和用戶通常只轉(zhuǎn)播或者評(píng)論一個(gè)短句有關(guān)。但是當(dāng)你輸入超過一行文字的時(shí)候,文本框的高度就自動(dòng)撐高了,大大改善了體驗(yàn),這樣用戶就可以看到全部的文字。不用再去拖動(dòng)文本框的滾動(dòng)條。
autoTextarea.js
- (function($){
- $.fn.autoTextarea = function(options) {
- var defaults={
- maxHeight:null,
- minHeight:$(this).height()
- };
- var opts = $.extend({},defaults,options);
- return $(this).each(function() {
- $(this).bind("paste cut keydown keyup focus blur",function(){
- var height,style=this.style;
- this.style.height = opts.minHeight + 'px';
- if (this.scrollHeight > opts.minHeight) {
- if (opts.maxHeight && this.scrollHeight > opts.maxHeight) {
- height = opts.maxHeight;
- style.overflowY = 'scroll';
- } else {
- height = this.scrollHeight;
- style.overflowY = 'hidden';
- }
- style.height = height + 'px';
- }
- });
- });
- };
- })(jQuery);
demo.js
- $(".doctable textarea").autoTextarea({
- maxHeight:400,
- minHeight:100
- });
以上所述就是本文的全部?jī)?nèi)容了,希望能夠給大家學(xué)習(xí)jQuery有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選