为什么在Chrome中输入更改时,TEXTAREA的scrollHeight错误
Why do I get wrong scrollHeight for TEXTAREA on input change in Chrome?
我正在尝试编写一个JavaScript来自动调整文本区域的文本大小。
我的问题是:在行被填充后,scrollHeight在每一秒符号上都返回错误的值(您可以在控制台中看到日志输出)。
文本区域的高度是47,我交替得到62和47(没有调整大小)。
JQuery插件:
; (function($) {
/**
* Resizes the text in the textarea
* @version 0.1
*/
$.fn.textResizer = function(options) {
options = jQuery.extend({
maxFontSize: null,
minFontSize: 8,
step: 1
}, options);
return this.each(function() {
var maxHeight = $(this).outerHeight();
$(this).bind('input propertychange', function() {
var innerElements = $(this).children(':visible');
console.log($(innerElements));
var fontSize = options.maxFontSize,
innerHeight;
console.log('inner: '+$(this).css( "height" )+ ', max: '+maxHeight, 'outer: '+$(this).outerHeight());
console.log(this.scrollHeight);
do {
innerHeight = this.scrollHeight;
console.log(this.scrollHeight);
fontSize = fontSize - options.step;
$(this).css('font-size', fontSize);
} while ((innerHeight > maxHeight) && fontSize > options.minFontSize);
});
});
};
})(jQuery);
CSS:
textarea {
position: absolute;
width:176px;
height:47px;
top: 4px;
left: 60px;
background: #666666;
border: none;
color: #fff;
overflow: hidden;
display:inline-block;
vertical-align: middle;
resize: none;
padding: 0;
}
HTML:
<script>
$(document).ready(function() {
$('textarea').textResizer({'maxFontSize':30});
});
</script>
<textarea></textarea>
下面是我的例子:http://jsfiddle.net/6nhLmcge/
这种行为的原因是什么?
你知道这个任务的其他解决方案/插件吗?
您需要在更改css之后而不是之前更新innerHeight
。http://jsfiddle.net/svt8zsx4/
do {
//innerHeight = $(this).innerHeight();
console.log(this.scrollHeight);
fontSize = fontSize - options.step;
$(this).css('font-size', fontSize);
innerHeight = this.scrollHeight;
} while ((innerHeight > maxHeight) && fontSize > options.minFontSize);
相关文章:
- 当值更改时,在servlet中自动获取textarea值,无需单击按钮
- 验证Javascript中的Textarea
- Textarea必需的attribut在javascript中不起作用
- window.on.scroll事件未启动
- 组合 2 个 JavaScript .scroll 函数
- Imageslooaded无法使用Infinite Ajax Scroll和Masonry
- .scroll()函数在上次更新后在谷歌chrome中定位闪烁
- 运行Infinite Scroll后调用函数时出现问题
- 更改代码镜像中TextArea的高度和宽度
- javascript textarea createTextNode()
- textarea autoresize出现问题
- 在Scroll上更新CSS代码:简化它就不会了'不要使用JQuery和最简单的Javascript
- 如何访问用javascript动态生成的textarea的文本内容
- 如何使用Jscript | JQuery在textArea中获取插入的|删除的字符串的索引和长度
- 使用Bootstrap3更改Scroll Jquery上导航栏的高度
- 单击TextArea'时,文本区域中的属性名称是什么;s上下箭头按钮
- jQuery 使用 .scroll(handler) 滚动到元素
- jQuery $(window).scroll事件处理程序关闭但仍在触发
- Scroll 不适用于 css()
- angularjs ng-repeat implement stick scroll