简体   繁体   English

使纹理的宽度适应其内容

[英]Adapt width of texture to its content

I need to adapt the width of a textarea (='#overlay') to its content. 我需要根据其内容调整文本区域的宽度(='#overlay')。 The minimum width should be 100px: 最小宽度应为100px:

'input #overlay': function(e) {
    var overlay = $('#overlay'),
        element = document.getElementById('overlay'),
        pos     = overlay.position(),
        width   = 100;

    if (element.scrollWidth > element.clientWidth) {
        var diff  = Math.ceil((element.scrollWidth - element.clientWidth)/20)*20,
            left  = pos.left - (diff/2);
            width = element.scrollWidth + diff;

        overlay.css({left: left, width: width});
    }

This code works to expand the textarea if there is a long line. 如果行很长,此代码可扩展文本区域。 But it can't be used to make it smaller if you delete some characters of the line. 但是,如果删除该行的某些字符,则不能使用它来缩小它。

You have to set the minimum value before the if-part: 您必须在if-part之前设置最小值:

overlay.css({ width: 100 });

Then the height will always be adapted to the needed height. 然后,高度将始终适应所需的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM