简体   繁体   English

键入时的Google Material Design不断增长的文本区域

[英]Google Material Design Growing Textarea while typing

I'm trying to implement Google Material Design Guidelines in my forms and it's working great, until I bumped into the textarea. 我正在尝试在自己的表单中实施Google Material Design指南 ,并且在我碰到文本区域之前,它一直都很好用。 What I want is this: When you focus on the textarea there is only one line, but when you reach the end of the line (while typing) it automatically adds another line and continues typing there. 我想要的是:当您专注于textarea时,只有一行,但是当到达该行的末尾(在键入时)时,它会自动添加另一行并在那里继续键入。

I have found this on codepen, but this uses an inputfield, not a textarea. 我已经在Codepen上找到了它,但是它使用的是输入字段,而不是文本区域。 This just scrolls horizontally... Demo 这只是水平滚动... 演示

  <input type="text" required>

Anyone who has this code and is willing to share? 有此代码并愿意分享的人吗? Thanks 谢谢

You are creating all the Material Design CSS & Jquery by yourself? 您是自己创建所有Material Design CSS和Jquery吗?

Otherwise, I found Material Design textarea like you mentioned in here: 否则,我会在这里找到您所提到的“材料设计”文本区域:

Source: https://materializecss.com/text-inputs.html#textarea 资料来源: https : //materializecss.com/text-inputs.html#textarea

Check out their Textarea part. 查看他们的Textarea部分。

Actually, to obtain this level of control, and work around the fact that a textarea, on most web browsers, can be resized by hand, you'll want to use a div with the contenteditable attribute . 实际上,要获得这种级别的控制,并解决大多数Web浏览器上的textarea可以手动调整大小这一事实,您将需要使用具有contenteditable属性的div。

See the HTML doctor entry on contenteditable for more. 有关更多信息,请参见contenteditable上HTML Doctor条目

Further, to calculate font sizes and overflow, you might want to use the canvas measureText method , for example using canvas as an offscreen substitute (where you input exactly the same text that is typed inside your contenteditable element). 此外,要计算字体大小和溢出,您可能想要使用canvas measureText方法 ,例如,使用canvas作为屏幕外替代(在其中输入与contenteditable元素中键入的文本完全相同的文本)。

Finally, while the css lineHeight attribute can somewhat facilitate those calculations, there are a few javascript libraries out there that are dedicated to the purpose. 最后,虽然css lineHeight属性可以在某种程度上简化这些计算,但仍有一些专门用于此目的的javascript库。 I found Font.js , haven't tested it at the time of this writing. 我发现Font.js ,在撰写本文时尚未对其进行测试。

You can use <div contentEditable> instead of textarea and that will make a trick. 您可以使用<div contentEditable>代替textarea,这将起到一定的作用。 Also you might not use additional libraries (Material-ui, jQuery, etc.).With your code it will look like this: 另外,您可能不使用其他库(Material-ui,jQuery等),代码如下所示:

 .inputBlock { position: relative; margin-top: 20px; font-family: 'Roboto'; display: block; width: 300px; background: #FFF; } .input { font-size: 15px; padding: 0 0 6px; display: block; width: 100%; height: auto; border: none; box-sizing: border-box; resize: none } .input:focus { outline: none; } /* LABEL */ label { color: #777; font-size: 15px; font-weight: normal; position: absolute; pointer-events: none; top: 0; transition: 0.2s ease all; -moz-transition: 0.2s ease all; -webkit-transition: 0.2s ease all; } /* active state */ .input:focus~label, .input:not(:empty)~label { top: -15px; font-size: 11px; color: #00968a; } /* BOTTOM BARS */ .bar { position: relative; display: block; width: 100%; } .bar:before, .bar:after { content: ''; height: 2px; width: 0; bottom: 1px; position: absolute; background: #00968a; transition: 0.2s ease all; -moz-transition: 0.2s ease all; -webkit-transition: 0.2s ease all; } .bar:before { left: 50%; } .bar:after { right: 50%; } /* active state */ .input:focus~.bar:before, .input:focus~.bar:after { width: 50%; } /* HIGHLIGHTER */ .highlight { position: absolute; height: 73%; width: 100%; top: 25%; left: 0; pointer-events: none; opacity: 0.5; border-bottom: 1px solid #777; } /* active state */ .input:focus~.highlight { -webkit-animation: inputHighlighter 0.3s ease; -moz-animation: inputHighlighter 0.3s ease; animation: inputHighlighter 0.3s ease; border: none; } /* ANIMATIONS */ @-webkit-keyframes inputHighlighter { from { background: #5264AE; } to { width: 0; background: transparent; } } @-moz-keyframes inputHighlighter { from { background: #5264AE; } to { width: 0; background: transparent; } } @keyframes inputHighlighter { from { background: #5264AE; } to { width: 0; background: transparent; } } [class='input textarea'] height: auto !important color: #000000 !important font-size: 15px !important div color: #000000 !important font-size: 15px !important~.highlight height: 77% !important 
 <div class="inputBlock"> <div contentEditable class="input" required></div> <span class="highlight"></span> <span class="bar"></span> <label>Name</label> </div> 

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

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