灵活的<textarea>高度作为新的换行符

时间:2016-07-20 04:37:50

标签: javascript jquery html css

&lt; p&gt;我以前没见过这件事,但我希望能做到。我试图在文本内部写一个文本区域的自动高度。如此处所示......&lt; / p&gt; &lt; p&gt;&lt; a href =&#34; http://i.stack.imgur.com/oxRfj.jpg"的rel =&#34; nofollow的&#34;&GT; HTTP://i.stack.imgur.com/oxRfj.jpg< / A&GT;&LT; / p为H. &lt; p&gt;&lt; a href =&#34; http://i.stack.imgur.com/XVzYT.jpg"的rel =&#34; nofollow的&#34;&GT; HTTP://i.stack.imgur.com/XVzYT.jpg< / A&GT;&LT; / p为H. &lt; p&gt;希望这些图片解释我的意思。第二幅图像也显示其理论最大高度。&lt; / p&gt; &lt; p&gt;&lt; a href =&#34; https://jsfiddle.net/nupk26Ly/"的rel =&#34; nofollow的&#34;&GT; HTTPS://jsfiddle.net/nupk26Ly/< / A&GT;&LT; / p为H. &lt; pre&gt;&lt; code&gt; .text-f { border:none; border-bottom:#183A4C solid 1px; 颜色:#183A4C; 字体:1rem / 2.4rem Georgia,serif; margin-top:40px; 调整大小:无; 身高:40px; 宽度:100%; } &LT; /代码&GT;&LT; /预&GT;

3 个答案:

答案 0 :(得分:3)

这是使用jquery的解决方案。希望它有所帮助。

function h(e) {
  $(e).css({'height':'40','overflow-y':'hidden'}).height(e.scrollHeight);
}
$('textarea').each(function () {
  h(this);
}).on('input', function () {
  h(this);
});
.text-f {
	border: none;
	border-bottom: #183A4C solid 1px;
	color: #183A4C;
	font: 1rem/2.4rem Georgia, serif;
  margin-top: 40px;
	height: 40px;
	width: 100%;
}

.form-btn {
	background: #183A4C;
	border: solid 1px #183A4C;
	color: #fff;
	cursor: pointer;
	display: block;
	font: 1rem/1.333rem Georgia, serif;
	height: 30px;
	width: 140px;
	margin: 10px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<textarea name="Message" class="text-f" placeholder="Describe your project..."></textarea>

<button class="form-btn" type="button">Submit</button>

Pure Javascript解决方案(PS:我在textarea标签中添加了ID(textarea))

var textarea = document.getElementById("textarea");

textarea.oninput = function() {
  textarea.style.height = "";
  textarea.style.height = Math.min(textarea.scrollHeight) + "px";
};
.text-f {
  border: none;
  border-bottom: #183A4C solid 1px;
  color: #183A4C;
  font: 1rem/2.4rem Georgia, serif;
  width: 100%;
  resize: none;
  height: 40px;
}
<div id="content">
  <div id="main-headline">
    <h1 class="main-header">Fill out the form or contact me alternatively.</h1>
  </div>

  <!----- content ------>
  <div class="content-container">
   <!----- row 1 ------>
    <section class="row">
      <!----- form ------>
      <form name="contact-f" action="#" method="post">
        <div class="col-3 c-right-col">
          <textarea id="textarea" name="Message" class="text-f" placeholder="Describe your project..."></textarea>
          <button class="link-btn form-btn" type="button">Submit</button>
        </div>
      </form>
    </section>
  </div>
</div>

答案 1 :(得分:3)

这是我之前做过的一个auto-expandable text area,可能适合你。也适用于手机

&#13;
&#13;
$(document)
    .one('focus.textarea', '.autoExpand', function(){
        var savedValue = this.value;
        this.value = '';
        this.baseScrollHeight = this.scrollHeight;
        this.value = savedValue;
    })
    .on('input.textarea', '.autoExpand', function(){
        var minRows = this.getAttribute('data-min-rows')|0,
            rows;
        this.rows = minRows;
        rows = Math.ceil((this.scrollHeight - this.baseScrollHeight) / 16);
        this.rows = minRows + rows;
    });

function updateTextbox(text) {
	$('#cagetextbox').val(text);
	//$('#cagetextbox').attr("rows", Math.max.apply(null, text.split("\n").map(function(a){alert(a.length);return a.length;})));
};

updateTextbox("");
&#13;
textarea{  
  display:block;
  box-sizing: padding-box;
  overflow:hidden;

  padding:10px;
  width:250px;
  font-size:14px;
  margin:50px auto;
  border-radius:8px;
  border:6px solid #556677;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<textarea id="cagetextbox" class='autoExpand' rows='3' data-min-rows='3' placeholder='Write here!! I expand!'></textarea>
</body>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

$(document)
    .one('focus.textarea', '.autoExpand', function(){
        var savedValue = this.value;
        this.value = '';
        this.baseScrollHeight = this.scrollHeight;
        this.value = savedValue;
    })
    .on('input.textarea', '.autoExpand', function(){
        var minRows = this.getAttribute('data-min-rows')|0,
             rows;
        this.rows = minRows;
    console.log(this.scrollHeight , this.baseScrollHeight);
        rows = Math.ceil((this.scrollHeight - this.baseScrollHeight) / 17);
        this.rows = minRows + rows;
    });

Check this link