在HTML中逐个显示单词而不是立即显示。

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

Display word in HTML one after another rather than at once

问题

我正在编写一个网站,以下内容将显示在页面上:

"你好。我的名字是XYZ。我正在做这个..."

我想要的是这些内容应该在页面上持续显示,就像前0.1秒是H,然后是He,然后是Hel等等。这个HTML代码是什么?

我尝试搜索相同的内容 - 在HTML中逐个显示文本,但感觉我没有正确搜索到。

英文:

I am writing a website where the following contents will be displayed on the page

"Hello there. My name is XYZ. I am doing this..."

What I want is that this should be displayed on the page continuously like first 0.1 seconds H then He then Hel etc. What is the html code for this?

I tried searching for the same - display text one after another in html but feel i am not able to search correctly.

答案1

得分: 0

If you want to use only html and css, then you can apply animation with animation-timing-function: steps();. But you need to know the number of letters, as well as wrap each one.

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

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

p {
--letters: 47;
--letter-width: .6em;
--duration: 8s;
font-family: monospace;
white-space: nowrap;
width: 0;
overflow: hidden;
animation: tape var(--duration) steps(var(--letters)) both;
display: inline-flex;
}

span {
width: var(--letter-width);
flex: none;
}

@keyframes tape {
to {
width:calc(var(--letters) * var(--letter-width));
}
}

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

<p>
<span>H</span><span>e</span><span>l</span><span>l</span><span>o</span><span> </span><span>t</span><span>h</span><span>e</span><span>r</span><span>e</span><span>.</span><span> </span><span>M</span><span>y</span><span> </span><span>n</span><span>a</span><span>m</span><span>e</span><span> </span><span>i</span><span>s</span><span> </span><span>X</span><span>Y</span><span>Z</span><span>.</span><span> </span><span>I</span><span> </span><span>a</span><span>m</span><span> </span><span>d</span><span>o</span><span>i</span><span>n</span><span>g</span><span> </span><span>t</span><span>h</span><span>i</span><span>s</span><span>.</span><span>.</span><span>.</span>

<!-- end snippet -->

P.S. Note that the proposed example still has some limitations. 😉

英文:

If you want to use only html and css, then you can apply animation with animation-timing-function: steps();. But you need to know the number of letters, as well as wrap each one.

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

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

p {
  --letters: 47;
  --letter-width: .6em;
  --duration: 8s;
  font-family: monospace;
  white-space: nowrap;
  width: 0;
  overflow: hidden;
  animation: tape var(--duration) steps(var(--letters)) both;
  display: inline-flex;
}

span {
  width: var(--letter-width);
  flex: none;
}

@keyframes tape {
  to {
    width:calc(var(--letters) * var(--letter-width));
  }
}

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

&lt;p&gt;
&lt;span&gt;H&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span&gt;l&lt;/span&gt;&lt;span&gt;l&lt;/span&gt;&lt;span&gt;o&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;t&lt;/span&gt;&lt;span&gt;h&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;M&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;m&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;X&lt;/span&gt;&lt;span&gt;Y&lt;/span&gt;&lt;span&gt;Z&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;I&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;m&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;d&lt;/span&gt;&lt;span&gt;o&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;g&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;t&lt;/span&gt;&lt;span&gt;h&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;

<!-- end snippet -->

P.S. Note that the proposed example still has some limitations. 😉

huangapple
  • 本文由 发表于 2023年6月9日 04:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435642.html
匿名

发表评论

匿名网友

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

确定