例1
代码如下 | 复制代码 |
<!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 > /** * 文本框根据输入内容自适应高度 * @author tang bin * @version 0.3 * @see * @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; style.height = height + extra + 'px'; text.value = tip; text.onfocus = function () { |
下面再介绍一个只是调用方法有一点不错
我们来看看代码:
代码如下 | 复制代码 |
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <style type="text/css"> *{margin:0px; padding:0px;} .dn{display:none;} .chackTextarea-area{line-height:22px; font-size:14px; font-family:Arial; overflow:auto; padding:1px 5px; border:1px solid #CDCDCD; width:400px; height:40px;} .content{width:420px; margin:0 auto; padding-top:20px;} .content h1{font-size:16px; font-family:微软雅黑; font-weight:normal;} </style> <script type="text/javascript" src="/jquery-1.7.1.min.js"></script> <script type="text/javascript"> (function($){ $.fn.autoTextarea = function(options) { var defaults={ maxHeight:null,//文本框是否自动撑高,默认: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); </script> <body> </html> |