首頁  >  問答  >  主體

為序列中的每一行建立基於 CSS 的打字動畫

<div class='screen'>
  <div class='text-container'>
    <p class='text'>A problem has been detected and system has been shut down to prevent damage to your computer.
    </p>
    <br />
    <p class='text'>IRQL_NOT_LESS_OR_EQUAL</p>
    <br />
    <p class='text'>If this is the first time you've seen this Stop error screen, restart your computer.</p>
    <p class='text'>If this screen appears again, follow these steps:</p>
    <br />
    <p class='text'>Check to make sure any new hardware or soFtware is properly installed.</p>
    <p class='text'>If this is a new installation,</p>
    <p class='text'>ask your hardware or soFtware manufacturer for any system updates you might need.</p>
    <br />
    <p class='text'>If problems continue, disable or remove any newly installed hardware or soFtware.</p>
    <p class='text'>Disable BIOS memory options such as caching or shadowing.</p>
    <p class='text'>If you need to use Safe Mode to remove or disable components, restart your computer.</p>
    <p class='text'>press F8 to select Advanced Startup options, and then select Safe Mode.</p>
    <br />
    <p class='text'>Technical information:</p>
    <br />
    <p class='text'>*** STOP: 0x0000000A /0x0000000000004A,</p>
    <p class='text'>0x0000000000000002, 0x0000000000000001, 0xFFFFF80002B37ABF</p>
    <br />
    <p class='text'>Collecting data for crash dump ...</p>
    <p class='text'>Initializing disk for crash dump ...</p>
    <p class='text'>Beginning dump of physical memory.</p>
  </div>
</div>
*{
  padding: 0;
  margin:0;
  box-sizing: border-box;
}

@keyframes typing {
  from { width: 0; }
  to { width: 100%; }
}

div.screen {
    background-color: #032f7c;

    div.text-container {
      padding: 20px;
      
      p.text {
        font-family: monospace;
        color: #ebf6ff;
        overflow: hidden;
        white-space: nowrap;
        letter-spacing: 1.5px;
        animation: typing 5s steps(90, end);
      }
    }
  }

我想要做的是在這裡用打字動畫向用戶顯示文本,逐行、從上到下逐行顯示。我嘗試了很多方法,但除了為每個 p.text 元素定義不同的 keyframe 之外,我找不到其他解決方案。這樣我的程式碼就變成了義大利麵條,因為有 17 個 p.text 元素。我該怎麼辦?

這是此問題的 codepen 連結:https://codepen.io/mehmetguduk/pen/RwJqzpN

P粉403804844P粉403804844184 天前383

全部回覆(1)我來回復

  • P粉903969231

    P粉9039692312024-04-01 09:34:09

    您需要手動對每行套用延遲。對於 SCSS,執行此操作的範例程式碼片段是使用 @for 循環,然後將索引乘以動畫的持續時間。

    @for $i from 0 through 20 {
          p.text:nth-of-type(#{$i + 1}) {
            animation-delay: #{$i * 4}s;
          }
        }

    當然,這需要一些額外的調整來實現完美的時機,但應該能讓您大致了解如何解決這個問題。

    確保您的動畫狀態也設定為 both,因此文字在開始時已隱藏,並在動畫結束後保持可見。

    編輯:

    您也可以使用 JavaScript 來設定動畫延遲,如下所示:

    document.querySelectorAll('p.text').forEach((el, i) => el.style.animationDelay = `${i * 5}s`)

    回覆
    0
  • 取消回覆