Div宽度和高度随着textarea内容的增加而增加

时间:2012-10-26 08:26:44

标签: jquery html

Div随输入的文字而增加。精细。但是当我按下输入高度时,应根据文字大小增加,并且宽度不应增加,直到文字达到上述宽度。请帮忙

http://jsfiddle.net/shabbirrangwala/NkRYY/8/

$('textarea').keyup(function(e) {
    if ((e.keyCode || e.which) == 13) {
        $('.EDetailInset').css('height', ((this.value.length + 1) * 3) + 'px');
    }
    else {
        $('.EDetailInset').css('width', ((this.value.length + 1) * 11) + 'px');
    }
});​ 

3 个答案:

答案 0 :(得分:3)

让我知道它是否符合期望。不知何故,你需要保持宽度不变

var tWidth = $('textarea')[0].value.length;
var vWidth = 0;
var hIncr = 2; //initial line count - looks somehow?
var iheight = $('.EDetailInset').css('height').replace('px',''); //default height to height of box
$('textarea').keyup(function(e) {



if ((e.keyCode || e.which) == 13) {
$('.EDetailInset').css('height', (hIncr * iheight) + 'px'); //increase height by one line
  vWidth = 0; //so that the width does not increase
  hIncr++; //increase line number
}
else 
{

    vWidth = (vWidth+1);  //only this increase and reset to zero for new line
    if(vWidth*11 > tWidth) //if more characters than we had, increase box width
        tWidth = vWidth*11;
    console.log((vWidth*11)+':'+tWidth);

$('.EDetailInset').css('width', (tWidth) + 'px'); //no increment, width is static

}
});​

Check this JS Fiddle

答案 1 :(得分:1)

虽然远非完美,但这是我的尝试:http://jsfiddle.net/V6BSY/2/

我只是试图解决横向缩放的问题,因为很多其他人已经展示了如何进行垂直缩放。

这也减少了使用退格时容器的大小。

<强> HTML:

<div id="txt">
    <textarea></textarea>
</div>

<强> CSS:

#txt {
    background-color: rgba(208,228,254,0.3);
    border: 2px dashed #000000;   
    height: 20px;
    padding: 5px;
}

#txt textarea {
    border: none;
    background: none;
    resize: none;
    outline: none;
    overflow: hidden;
    width: 10px;
    height: 15px;
    font-size: 10px;
}
​

<强> JavaScript的:

$(document).ready(function() {
    $("#txt").css("width", $("#txt textarea").css("width"));
});

$('textarea').keydown(function(e) {

    lines = $('#txt textarea').val().split('\n');
    maxLength = getLengthOfLongestLine(lines);

    var h = parseInt($(this).get(0).scrollHeight) + 5;
    var w = maxLength * parseInt($(this).css('font-size'));

    if ((e.keyCode || e.which) == 13) {
        $("#txt").css("height", h);
        $(this).css("height", h);        
    } else {
        $("#txt").css("width", w);
        $(this).css("width", w);        
    }

});

function getLengthOfLongestLine(arrLines) {        
    var maxLength = 0;
    for(var line in arrLines) {
        if (arrLines[line].length > maxLength) {
            maxLength = arrLines[line].length;
        }
    }
    return maxLength;                          
}
​

答案 2 :(得分:-1)

试试这个:http://jsfiddle.net/NkRYY/62/

// HTML
<div id="EDetail" class="EDetail EDetailText">
    <div class="EDetailInset" >
        <span style="font-size: 17px;">

        </span>
        <textarea></textarea>
    </div>
</div>

// CSS
.EDetail, .EDetailDisabled {

    font-size: 20px;
    overflow: visible;    
    position: absolute;
}

.EDetail {
    border-radius: 3px 3px 3px 3px;

}

.EDetailInset {
    background-color: rgba(217,247,217,0.3);
    font-size: 20px;
    height : 22px;
    padding: 5px;
    background-color: rgba(208,228,254,0.3);
    border: 2px dashed #000000;

    position: absolute;
    cursor: move;
    min-width:100px;

}





.EDetailText textarea, .EDetailText span {
    font: 100%/1.1 arial,sans-serif;
    position: absolute;
    white-space: pre;
   padding: 5px;
}
.EDetailText textarea, .EDetailText textarea[disabled] {

    background: none repeat scroll 0 center transparent;
    border: 0 none;
    bottom: 6px;
    box-shadow: none;
    color: #000000;
    display: block;
    height: 90%;
    left: 0px;
    line-height: 1.1;
    outline: 0 none;
    padding: 5px;
    resize: none;
    right: 6px;
    top: 0px;
    transition: none 0s ease 0s;
    width: 90%;
    overflow:auto;
}

//JS
$('textarea').keyup(function(e) {


    if ((e.keyCode || e.which) == 13) {
    $('.EDetailInset').css('height', ((this.value.length + 10) * 3) + 'px');

    }
    //else
    //{
    //$('.EDetailInset').css('width', ((this.value.length + 10) * 11) + 'px');
    //}
    });

到达时间的极限宽度是多少?