是否可能使文本区域的scrollHeight减少少于3行?

huangapple go评论98阅读模式
英文:

is it possible to make textarea scrollHeight decrease less than 3 row

问题

以下是您提供的代码的翻译:

  1. 我对CSSHTML是个新手我正在使用textarea来让用户输入多行内容这是我制作以显示我面临的问题的最小代码
  2. import React, { useState } from "react";
  3. const App: React.FC = () => {
  4. const [promptLines, setPromptLines] = useState<number>(1);
  5. const handleChange = (e: any) => {
  6. const inputContent = e.target.value;
  7. if (inputContent && inputContent.length > 0) {
  8. const talkInput = document.getElementById("talkInput");
  9. if(!talkInput) return;
  10. const lineHeight = parseInt(window.getComputedStyle(talkInput).getPropertyValue('line-height'));
  11. talkInput.style.cssText = 'line-height: 23px;';
  12. const scrlht = talkInput.scrollHeight;
  13. const lines = Math.floor(scrlht / lineHeight);
  14. console.log(lines);
  15. }
  16. }
  17. return (
  18. <div className="outerdiv">
  19. <textarea
  20. id="talkInput"
  21. rows={promptLines ? promptLines : 1}
  22. onChange={handleChange}></textarea>
  23. </div>
  24. );
  25. }
  26. export default App;
  1. 这是`App.css`CSS文件内容
  2. .outerdiv{
  3. display: flex;
  4. align-items: center;
  5. padding: 10px;
  6. border-radius: 25px;
  7. border: 2px solid #ccc;
  8. transition: border-color 0.5s ease;
  9. width: 80%;
  10. margin: auto;
  11. margin-bottom: 15px;
  12. margin-top: 10px;
  13. box-sizing: border-box;
  14. min-height: 40px;
  15. }
  16. .outerdiv textarea{
  17. flex: 1;
  18. border: none;
  19. border-radius: 25px;
  20. outline: none;
  21. font-size: 16px;
  22. font-family: sans-serif;
  23. resize: none;
  24. padding: 6px;
  25. min-height: 23px;
  26. line-height: 23px;
  27. height: 1rem;
  28. }

当我在textarea中输入内容时,行数会增加;当我从textarea中删除内容时,行数会减少。一切都按预期工作,但最小行数是3。它不会减少到少于3行。为什么scrollHeight在3行高度时停止减少?是否可能使scrollHeight继续减少直到1行?

英文:

I am a newbie with the css and html. I am using textarea to make user input multi line content, this is the minimal code I have made to show the issue I am facing, the App.tsx code look like this:

  1. import React, { useState } from &quot;react&quot;;
  2. const App: React.FC = () =&gt; {
  3. const [promptLines, setPromptLines] = useState&lt;number&gt;(1);
  4. const handleChange = (e: any) =&gt; {
  5. const inputContent = e.target.value;
  6. if (inputContent &amp;&amp; inputContent.length &gt; 0) {
  7. const talkInput = document.getElementById(&quot;talkInput&quot;);
  8. if(!talkInput) return;
  9. const lineHeight = parseInt(window.getComputedStyle(talkInput).getPropertyValue(&#39;line-height&#39;));
  10. talkInput.style.cssText = &#39;line-height: 23px;&#39;;
  11. const scrlht = talkInput.scrollHeight;
  12. const lines = Math.floor(scrlht / lineHeight);
  13. console.log(lines);
  14. }
  15. }
  16. return (
  17. &lt;div className=&quot;outerdiv&quot;&gt;
  18. &lt;textarea
  19. id=&quot;talkInput&quot;
  20. rows={promptLines ? promptLines : 1}
  21. onChange={handleChange}&gt;&lt;/textarea&gt;
  22. &lt;/div&gt;
  23. );
  24. }
  25. export default App;

and this is the App.css css file look like:

  1. .outerdiv{
  2. display: flex;
  3. align-items: center;
  4. padding: 10px;
  5. border-radius: 25px;
  6. border: 2px solid #ccc;
  7. transition: border-color 0.5s ease;
  8. width: 80%;
  9. margin: auto;
  10. margin-bottom: 15px;
  11. margin-top: 10px;
  12. box-sizing: border-box;
  13. min-height: 40px;
  14. }
  15. .outerdiv textarea{
  16. flex: 1;
  17. border: none;
  18. border-radius: 25px;
  19. outline: none;
  20. font-size: 16px;
  21. font-family: sans-serif;
  22. resize: none;
  23. padding: 6px;
  24. min-height: 23px;
  25. line-height: 23px;
  26. height: 1rem;
  27. }

when I input the content into textarea, the lines will increase, when I delete the contnet from textarea, the lines will decrease, all things work as expect, but the minimal lines are 3. It will not decrease less than 3 rows. why scrollHeight stop decrease in 3 rows height? is it possible to make the scrollHeight continue decrease until to 1 row?

答案1

得分: 1

这是一个示例,使用 div 替代 textarea 并使用 contentEditable="true"。这符合您的需求吗?

  1. <!-- begin snippet: js hide: false console: true babel: false -->
  2. <!-- language: lang-css -->
  3. .my-div {
  4. width: 200px;
  5. border: 1px solid black;
  6. min-height: 20px;
  7. max-height: 100px;
  8. overflow-y: auto;
  9. }
  10. <!-- language: lang-html -->
  11. <div contentEditable="true" class="my-div"></div>
  12. <!-- end snippet -->

如果需要进一步的信息或帮助,请告诉我。

英文:

Here is an example using div instead of textarea with contentEditable=&quot;true&quot;. Is that what you need?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. .my-div {
  2. width: 200px;
  3. border:1px solid black;
  4. min-height: 20px;
  5. max-height:100px;
  6. overflow-y: auto
  7. }

<!-- language: lang-html -->

  1. &lt;div contentEditable=&quot;true&quot; class=&quot;my-div&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 01:09:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定