在 CSS 中輸入動畫超出指定限制
P粉285587590
P粉285587590 2024-03-20 00:25:06
0
1
380

打字動畫每次都會停在設定的字元數。如果我延長聲明,它就會中斷。如果我縮短語句,它會繼續執行,直到達到設定的字元數。我知道我必須更改步數 (50),但這並不是真正停止的數字,它停止於 28。

@import url('https://fonts.googleapis.com/css2?family=Courier Prime&family=Source Code Pro:wght@200&display=swap');
html {
  min-height: 100%;
  overflow: hidden;
}

body {
  height: calc(100vh - 8em);
  padding: 0;
  margin: 0;
  color: #FFF;
  font-family: 'Courier Prime', monospace;
  background-color: rgb(0, 0, 0);
}

.video-bg {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  min-height: 100%;
  opacity: .5;
}

.line {
  position: relative;
  top: 75%;
  width: 24em;
  margin: auto;
  border-right: 2px solid #FFF;
  font-size: 18px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  transform: translateY(-50);
}

.anim-typewriter {
  animation: typewriter 5s steps(50) 1s 1 normal both, blinkTextCursor 750ms steps(40) infinite normal;
}

@keyframes typewriter {
  from {
    width: 0;
  }
  to {
    width: 16em;
  }
}

@keyframes blinkTextCursor {
  from {
    border-right-color: transparent;
  }
  to {
    border-right-color: rgb(155, 211, 71);
  }
}
<p class="line anim-typewriter">Under construction...</p>

P粉285587590
P粉285587590

全部回覆(1)
P粉725827686

您可以透過減少此處的寬度參數來調整關鍵影格的結束位置:

@keyframes typewriter {
  from {
    width: 0;
  }
  to {
    width: 16em;
  }
}

將寬度更改為 13em,遊標將在字串末尾閃爍:

@import url('https://fonts.googleapis.com/css2?family=Courier Prime&family=Source Code Pro:wght@200&display=swap');
html {
  min-height: 100%;
  overflow: hidden;
}

body {
  height: calc(100vh - 8em);
  padding: 0;
  margin: 0;
  color: #FFF;
  font-family: 'Courier Prime', monospace;
  background-color: rgb(0, 0, 0);
}

.video-bg {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  min-height: 100%;
  opacity: .5;
}

.line {
  position: relative;
  top: 75%;
  width: 24em;
  margin: auto;
  border-right: 2px solid #FFF;
  font-size: 18px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  transform: translateY(-50);
}

.anim-typewriter {
  animation: typewriter 5s steps(50) 1s 1 normal both, blinkTextCursor 750ms steps(40) infinite normal;
}

@keyframes typewriter {
  from {
    width: 0;
  }
  to {
    width: 13em;
  }
}

@keyframes blinkTextCursor {
  from {
    border-right-color: transparent;
  }
  to {
    border-right-color: rgb(155, 211, 71);
  }
}
<p class="line anim-typewriter">Under construction...</p>

這是最簡單的方法,但將來如果您有動態大小的字串,您需要根據字串長度(字元數)更新寬度。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!