2

我正在编写脚本,它通过检查文本是否适合在 div 框内来限制输入的文本,如果不尝试降低字体大小,则高度和宽度是固定的,然后再次检查然后达到最小字体大小,它将文本限制在 textarea 内。我已经完成了这一点,但它有时会因高度过高而达到限制,并且它不允许覆盖然后选择的文本。

这是我的脚本:

function onEdit(e) {
    var notebox_tmp = $(".notebox_tmp");
    var maxHeight = 120;
    var minFontSize = 25;

    notebox_tmp.css("font-size","50px");
    notebox_tmp.text($(this).val());
    var currentHeight = notebox_tmp.height();
    var currentFontSize = parseInt(notebox_tmp.css("font-size"),10);
    while (currentHeight > maxHeight && currentFontSize > minFontSize) {
        notebox_tmp.css("font-size", currentFontSize);
        currentFontSize -= 0.25;
        currentHeight = notebox_tmp.height();
    }
    if (currentFontSize <= minFontSize) { 
      e.preventDefault();
    }
};

$("textarea").keypress(onEdit);

这是我当前的脚本演示:http: //jsfiddle.net/6pfCM/9/

4

0 回答 0