2

タイピングしているかのようにアニメーションを作ろうとしています。これを実現するために、CSS アニメーションの「ステップ」を使用しています。

アニメーション自体は問題なく動作します。ただし、複数行のテキストをアニメーション化する場合は、すべて同時に再生を開始します。これは私に望ましい効果を与えていません。(テキストを切り取った単一の で使用しようとしましたが、再びアニメーションを同時に開始しました。)<br><h1>

これに対抗するために、テキストの次の行を に置き、テキスト<h2>のすべての行にアニメーションの遅延を設定します。これは機能しますが、アニメーションが開始する前にテキストが表示されます。

「ライブタイピング」効果を実際に得るために、アニメーションの再生が始まるまでテキストを非表示にしたいと思います。

どうすればこれを達成できるかについて誰かアイデアがありますか?

HTML

<div class="content"> 
    <h1>Hi there! My name is Jeff.</h1>
    <h2>And I create cool stuff.</h2>
</div>

CSS

.content h1 {
    background:white;
    opacity:0.7;
    white-space:nowrap;
    overflow:hidden;
    border-right: 3px solid black;
    -webkit-animation: typing 2s steps(26, end), 
                        blink-caret 1s step-end 2s;
}
.content h2 {
    background:white;
    opacity:0.7;
    white-space:nowrap;
    overflow:hidden;
    border-right: 3px solid black;
    -webkit-animation: typing 2s steps(26, end), 
                        blink-caret 1s step-end infinite;
    -webkit-animation-delay:3s;
}

@-webkit-keyframes typing {
    from { width: 0; }
    to { width:400px; }
}

@-webkit-keyframes blink-caret {
    from, to { border-color: transparent }
    50% { border-color: black }
}

jsフィドル

4

4 に答える 4

1

コメントにはすでに解決策が含まれているため、おそらくこれは別の方法である可能性があります-visibility: hidden最初にタイムアウトと設定を使用することです(簡単にするために、jQueryを使用して可視性を設定しました)。

次の CSS ルールを含めます。

.content {
    visibility: hidden;
}

JavaScript の場合は次のようになります。

window.setTimeout(function() {
    $('#contentdiv h1').css('visibility', 'visible');
}, 100);

window.setTimeout(function() {
    $('#contentdiv h2').css('visibility', 'visible');
}, 3100);

jsFiddleを参照してください

于 2013-04-10T12:21:17.407 に答える
1

p
{
  font:500 22px consolas;    
  width:20ch;
  white-space:nowrap;
  overflow:hidden;
  animation:type 5s steps(20) infinite;
}
@keyframes type
{
  0%{ width:0; } 
}
<p>Text Type Animation</p>

于 2016-04-06T12:50:19.357 に答える
0

OPの質問ではありませんが、他の誰かがこれを役に立つと思った場合:

私は、テキストの段落、折り返されるテキストを含む可能性のある単一のタグを入力してアニメーション化し、不明な数の実際の行を生成できるようにしたいと考えていまし<p>。タグ自体に単純なリニア アニメーションを適用してpもうまくいかないので、代わりに、テキストの段落を覆う複数の「ハイダー」要素をそれぞれ 1 行の高さで配置し、それぞれをアニメーション化するというアプローチを採用しました。それらは縮小し、その下のテキスト行から文字が表示されます.

HTML は次のようになります。

<div class="container">
<!-- container div is required to set absolute positions within it, so that .typing and .hiders exactly overlap -->

  <p class="typing">
    This paragraph of text will be animated 
    with a "typewriter" style effect, and it 
    will continue to work even if it splits across 
    multiple lines.  Well, in this case, up to a 
    maximum of 5 lines, but you get the picture.
  </p>

  <div class="hiders">
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </div>

</div>

コンテナが必要で、.typing要素と.hidersusingabsoluteが互いに重なるように配置します。

.container {
  position: relative;
  font-family: Consolas, monospace;  
}
.typing {
  position: absolute;
  top: 0;
  margin: 0;
  z-index: -1;
}
.hiders {
  margin: 0;
  position: absolute;
  top: 0;
  width: 100%;
}

そして、アニメーションはp内のそれぞれに適用されます.hiders:

.hiders p {
  position: relative; 
  clear: both; 
  margin: 0;
  float: right; /* makes animation go left-to-right */
  width:0; /* graceful degradation: if animation doesn't work, these are invisible by default */
  background: white; /* same as page background */
  animation: typing 2s steps(30, end);
  animation-fill-mode: both;  /* load first keyframe on page load, leave on last frame at end */
}

.hiders p:nth-child(2) {
  animation-delay: 2s;
}
.hiders p:nth-child(3) {
  animation-delay: 4s;
  /* etc */

最後のフィドルは次のとおりです。

https://jsfiddle.net/hjwp/514cLzxn/

インスピレーションの元のクレジット: Lea Verou

于 2016-03-18T10:56:49.080 に答える