Udemyセール!最大95%オフ!1,200円~Udemy公式サイト >

この記事にはプロモーションが含まれています。

【コピペOK】タイプライターアニメーション|6種類【Web制作】

【コピペOK】タイプライターアニメーション|6種類【Web制作】

ケケンタ

タイプライターアニメーションを実装したい……

ケケンタ

文字が一文字ずつ表示される効果を作りたい……

今回はこのようなお悩みをお持ちの方へ向けて

Web制作において必須のUI要素
タイプライターアニメーション

をご紹介します。

6種類のアニメーション効果を完全網羅した実装なので、いままさに「タイプライターアニメーションを実装しないといけない!」という方は丸っとコピペしてどうぞご活用ください!

この記事で紹介するタイプライターアニメーション
  • 基本タイプライター(一文字ずつ表示)
  • 高度なカーソル効果(タイピング中と停止中の異なるカーソル表示)
  • 削除効果(文字の削除)
  • 速度変化(表示速度の変化)
  • カラー変化(文字色の変化)
  • フォント変化
ケケンタ

特にヒーローセクションタイトル表示には、タイプライターアニメーションが非常に効果的です。この記事のコードをご活用いただきWeb制作の効率化に繋がれば何よりです。

なお、今回ご紹介するアニメーションはCSSとJavaScriptで実装できるので、基本的なコーディング知識があれば安心してご利用いただけます。

あわせて読みたい

Webアニメーションの引き出しを増やすのにおすすめの書籍





ケケンタ

ケケンタのITブログでは、WebアプリPHPLaravel)やWeb制作WordPressコーディング)について情報を発信しています。
学習中の方や実務をされている方など多くの方にアクセスいただいていますので、ぜひほかの記事も参考にしてみてください!


運動不足、気になっていませんか?

もしプログラミング学習やお仕事で運動不足が気になっているなら

連続屈伸運動がおすすめです!

ボタンにカーソルを合わせるだけ
カウントダウンが始まるタイマーをご用意してみました!

ケケンタ

無理のない範囲で、ぜひ隙間時間に屈伸運動を取り入れてみて下さい!

タイマースタート

3:00

※運動不足だと連続3分で取り組んでもかなり息が切れます
(僕は加えて気分もちょっと悪くなりました……)
絶対にご無理の無い範囲でお取り組みください!



目次

タイプライターアニメーションとは

タイプライターアニメーションは、文字が一文字ずつ表示されることで、タイプライターで文章を打っているような効果を再現するアニメーションです。ユーザーの注目を集め、ストーリー性のある表現を実現する効果的な手法です。

効果的な使用場面

適している場面

  • ヒーローセクションのタイトル
  • ストーリーテリング
  • プロダクト紹介
  • ウェルカムメッセージ
  • エラーメッセージ
  • ローディング画面

避けるべき場面

  • 長い文章の表示
  • パフォーマンスを重視する場面
  • アクセシビリティを重視する場面

実装方法の比較

アニメーション難易度視覚的インパクトパフォーマンスブラウザ対応
基本タイプライター⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
高度なカーソル効果⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
削除効果⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
速度変化⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
カラー変化⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
フォント変化⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

基本タイプライター

文字が一文字ずつ表示される基本的なタイプライター効果です。シンプルで分かりやすい実装です。

① デモ

See the Pen タイプライター文字表示 by ケケンタ (@lgshifbg-the-looper) on CodePen.

この基本タイプライターの特徴
  • シンプルで分かりやすい実装
  • 軽量でパフォーマンスが良い
  • 基本的なタイプライター効果

② HTML

<div class="typewriter-container">
  <div class="typewriter-text" id="typewriter-text">
    <span class="text-content"></span>
  </div>
  <button class="restart-btn" id="restart-btn">再開</button>
</div>

③ CSS

/* タイプライターコンテナ */
.typewriter-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  padding: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  font-family: 'Courier New', monospace;
}

/* タイプライターテキスト */
.typewriter-text {
  font-size: 2rem;
  font-weight: 600;
  line-height: 1.5;
  text-align: center;
  margin-bottom: 20px;
}

.text-content {
  display: inline-block;
}

/* 基本タイプライター用のスタイル */
.text-content {
  border-right: 2px solid white;
  animation: blink 1s infinite;
}

@keyframes blink {
  0%, 50% {
    border-color: white;
  }
  51%, 100% {
    border-color: transparent;
  }
}

/* 再開ボタン */
.restart-btn {
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.restart-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.restart-btn:active {
  transform: translateY(0);
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
  .typewriter-text {
    font-size: 1.5rem;
  }

  .cursor {
    height: 1.5rem;
  }
}

④ JavaScript

class Typewriter {
  constructor(element, text, speed = 100) {
    this.element = element;
    this.text = text;
    this.speed = speed;
    this.currentIndex = 0;
    this.isTyping = false;
  }

  start() {
    if (this.isTyping) return;

    this.isTyping = true;
    this.currentIndex = 0;
    this.element.textContent = '';
    this.type();
  }

  type() {
    if (this.currentIndex < this.text.length) {
      this.element.textContent += this.text.charAt(this.currentIndex);
      this.currentIndex++;
      setTimeout(() => this.type(), this.speed);
    } else {
      this.isTyping = false;
    }
  }

  stop() {
    this.isTyping = false;
  }

  reset() {
    this.stop();
    this.element.textContent = '';
    this.currentIndex = 0;
  }
}

document.addEventListener('DOMContentLoaded', function() {
  const textElement = document.querySelector('.text-content');
  const restartBtn = document.getElementById('restart-btn');

  const typewriter = new Typewriter(
    textElement, 
    'Welcome to our amazing website! This is a typewriter animation that creates an engaging user experience.',
    100
  );

  // 自動開始
  typewriter.start();

  // 再開ボタン
  restartBtn.addEventListener('click', function() {
    typewriter.reset();
    setTimeout(() => {
      typewriter.start();
    }, 500);
  });
});

⑤ カスタマイズ例

/* 異なるフォント */
.typewriter-text {
  font-family: 'Roboto Mono', monospace;
}

/* 異なるカラー */
.typewriter-container {
  background: linear-gradient(135deg, #ff6b6b, #ee5a52);
}

/* より大きなフォント */
.typewriter-text {
  font-size: 3rem;
}

/* カスタムカーソル */
.text-content {
  border-right-color: #ff6b6b;
  border-right-width: 3px;
}

高度なカーソル効果

タイピング中と停止中で異なるカーソルアニメーションを表示する高度なタイプライター効果です。

① デモ

See the Pen Untitled by ケケンタ (@lgshifbg-the-looper) on CodePen.

この高度なカーソル効果の特徴
  • タイピング中と停止中で異なるカーソル表示
  • よりリアルなタイプライター感
  • 視覚的なフィードバックが豊富

② HTML

<div class="typewriter-container">
  <div class="typewriter-text" id="typewriter-text2">
    <span class="text-content"></span>
    <span class="blinking-cursor"></span>
  </div>
  <button class="restart-btn" id="restart-btn2">再開</button>
</div>

③ CSS

/* タイプライターコンテナ */
.typewriter-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  padding: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  font-family: 'Courier New', monospace;
}

/* タイプライターテキスト */
.typewriter-text {
  font-size: 2rem;
  font-weight: 600;
  line-height: 1.5;
  text-align: center;
  margin-bottom: 20px;
}

.text-content {
  display: inline-block;
}

/* 高度なカーソル */
.blinking-cursor {
  display: inline-block;
  width: 3px;
  height: 2rem;
  background-color: #ff6b6b;
  margin-left: 2px;
  animation: blinkCursor 0.8s infinite;
  box-shadow: 0 0 8px rgba(255, 107, 107, 0.6);
  border-radius: 1px;
  position: relative;
}

.blinking-cursor::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: inherit;
  border-radius: inherit;
}

@keyframes blinkCursor {
  0%, 50% {
    opacity: 1;
    transform: scaleY(1);
    background-color: #ff6b6b;
  }
  51%, 100% {
    opacity: 0.3;
    transform: scaleY(0.3);
    background-color: #ff8e8e;
  }
}

/* タイピング中のカーソル */
.blinking-cursor.typing {
  animation: typingCursor 0.6s infinite;
  background-color: #4facfe;
  box-shadow: 0 0 12px rgba(79, 172, 254, 0.8);
}

@keyframes typingCursor {
  0%, 50% {
    opacity: 1;
    transform: scaleY(1);
    background-color: #4facfe;
  }
  51%, 100% {
    opacity: 0.7;
    transform: scaleY(0.8);
    background-color: #6bb6ff;
  }
}

/* 再開ボタン */
.restart-btn {
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.restart-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.restart-btn:active {
  transform: translateY(0);
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
  .typewriter-text {
    font-size: 1.5rem;
  }

  .blinking-cursor {
    height: 1.5rem;
  }
}

④ JavaScript

class TypewriterWithCursor {
  constructor(element, text, speed = 100, cursorElement) {
    this.element = element;
    this.text = text;
    this.speed = speed;
    this.currentIndex = 0;
    this.isTyping = false;
    this.cursor = cursorElement;
  }

  start() {
    if (this.isTyping) return;

    this.isTyping = true;
    this.currentIndex = 0;
    this.element.textContent = '';
    this.cursor.classList.add('typing');
    this.type();
  }

  type() {
    if (this.currentIndex < this.text.length) {
      this.element.textContent += this.text.charAt(this.currentIndex);
      this.currentIndex++;
      setTimeout(() => this.type(), this.speed);
    } else {
      this.isTyping = false;
      this.cursor.classList.remove('typing');
    }
  }

  stop() {
    this.isTyping = false;
    this.cursor.classList.remove('typing');
  }

  reset() {
    this.stop();
    this.element.textContent = '';
    this.currentIndex = 0;
  }
}

document.addEventListener('DOMContentLoaded', function() {
  const textElement2 = document.querySelector('#typewriter-text2 .text-content');
  const restartBtn2 = document.getElementById('restart-btn2');
  const cursor2 = document.querySelector('#typewriter-text2 .blinking-cursor');

  const typewriter2 = new TypewriterWithCursor(
    textElement2, 
    'Creating amazing animations with smooth cursor effects!',
    80,
    cursor2
  );

  // 自動開始
  typewriter2.start();

  // 再開ボタン
  restartBtn2.addEventListener('click', function() {
    typewriter2.reset();
    setTimeout(() => {
      typewriter2.start();
    }, 500);
  });
});

⑤ カスタマイズ例

/* 異なるカーソルスタイル */
.blinking-cursor {
  background-color: #4facfe;
  border-radius: 2px;
}

/* より速い点滅 */
.blinking-cursor {
  animation: blinkCursor 0.5s infinite;
}

/* カスタムカーソル形状 */
.blinking-cursor {
  width: 2px;
  height: 1.5rem;
  background-color: #28a745;
}

削除効果

文字を削除するタイプライター効果です。

① デモ

See the Pen タイプライター削除効果 by ケケンタ (@lgshifbg-the-looper) on CodePen.

この削除効果の特徴
  • 動的な文字削除
  • 視覚的インパクト大
  • 注目を集める効果

② HTML

<div class="typewriter-container">
  <div class="typewriter-text" id="typewriter-text3">
    <span class="text-content"></span>
    <span class="delete-cursor"></span>
  </div>
  <button class="restart-btn" id="restart-btn3">再開</button>
</div>

③ CSS

/* タイプライターコンテナ */
.typewriter-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  padding: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  font-family: 'Courier New', monospace;
}

/* タイプライターテキスト */
.typewriter-text {
  font-size: 2rem;
  font-weight: 600;
  line-height: 1.5;
  text-align: center;
  margin-bottom: 20px;
}

.text-content {
  display: inline-block;
}

/* 削除カーソル */
.delete-cursor {
  display: inline-block;
  width: 2px;
  height: 2rem;
  background-color: #dc3545;
  margin-left: 2px;
  animation: deleteBlink 0.6s infinite;
}

@keyframes deleteBlink {
  0%, 50% {
    opacity: 1;
    background-color: #dc3545;
  }
  51%, 100% {
    opacity: 0.3;
    background-color: #ff6b6b;
  }
}

/* 削除中のアニメーション */
.delete-cursor.deleting {
  animation: deleteAnimation 0.3s infinite;
}

@keyframes deleteAnimation {
  0%, 50% {
    opacity: 1;
    transform: scaleX(1);
  }
  51%, 100% {
    opacity: 0.5;
    transform: scaleX(0.5);
  }
}

/* 再開ボタン */
.restart-btn {
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.restart-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.restart-btn:active {
  transform: translateY(0);
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
  .typewriter-text {
    font-size: 1.5rem;
  }

  .delete-cursor {
    height: 1.5rem;
  }
}

④ JavaScript

class TypewriterWithDelete {
  constructor(element, texts, speed = 100, cursorElement) {
    this.element = element;
    this.texts = Array.isArray(texts) ? texts : [texts];
    this.speed = speed;
    this.currentTextIndex = 0;
    this.currentCharIndex = 0;
    this.isTyping = false;
    this.isDeleting = false;
    this.cursor = cursorElement;
  }

  start() {
    if (this.isTyping || this.isDeleting) return;

    this.isTyping = true;
    this.type();
  }

  type() {
    const currentText = this.texts[this.currentTextIndex];

    if (this.currentCharIndex < currentText.length) {
      this.element.textContent += currentText.charAt(this.currentCharIndex);
      this.currentCharIndex++;
      setTimeout(() => this.type(), this.speed);
    } else {
      this.isTyping = false;
      setTimeout(() => this.delete(), 2000);
    }
  }

  delete() {
    if (this.element.textContent.length > 0) {
      this.isDeleting = true;
      this.cursor.classList.add('deleting');
      this.element.textContent = this.element.textContent.slice(0, -1);
      setTimeout(() => this.delete(), this.speed / 2);
    } else {
      this.isDeleting = false;
      this.cursor.classList.remove('deleting');
      this.currentCharIndex = 0;
      this.currentTextIndex = (this.currentTextIndex + 1) % this.texts.length;
      setTimeout(() => this.start(), 1000);
    }
  }

  stop() {
    this.isTyping = false;
    this.isDeleting = false;
    this.cursor.classList.remove('deleting');
  }

  reset() {
    this.stop();
    this.element.textContent = '';
    this.currentCharIndex = 0;
    this.currentTextIndex = 0;
  }
}

document.addEventListener('DOMContentLoaded', function() {
  const textElement3 = document.querySelector('#typewriter-text3 .text-content');
  const restartBtn3 = document.getElementById('restart-btn3');
  const cursor3 = document.querySelector('#typewriter-text3 .delete-cursor');

  const typewriter3 = new TypewriterWithDelete(
    textElement3, 
    [
      'Hello World!',
      'Welcome to our site!',
      'Amazing animations!',
      'Thank you for visiting!'
    ],
    100,
    cursor3
  );

  // 自動開始
  typewriter3.start();

  // 再開ボタン
  restartBtn3.addEventListener('click', function() {
    typewriter3.reset();
    setTimeout(() => {
      typewriter3.start();
    }, 500);
  });
});

⑤ カスタマイズ例

/* 異なる削除速度 */
.delete-cursor.deleting {
  animation: deleteAnimation 0.2s infinite;
}

/* カスタム削除カーソル */
.delete-cursor {
  background-color: #ff6b6b;
  box-shadow: 0 0 5px rgba(255, 107, 107, 0.5);
}

速度変化

表示速度が変化するタイプライター効果です。

① デモ

See the Pen タイプライター速度変化 by ケケンタ (@lgshifbg-the-looper) on CodePen.

この速度変化効果の特徴
  • 動的な速度変化
  • 視覚的インパクト
  • 注目を集める効果

② HTML

<div class="typewriter-container">
  <div class="typewriter-text" id="typewriter-text4">
    <span class="text-content"></span>
    <span class="speed-cursor"></span>
  </div>
  <button class="restart-btn" id="restart-btn4">再開</button>
</div>

③ CSS

/* タイプライターコンテナ */
.typewriter-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  padding: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  font-family: 'Courier New', monospace;
}

/* タイプライターテキスト */
.typewriter-text {
  font-size: 2rem;
  font-weight: 600;
  line-height: 1.5;
  text-align: center;
  margin-bottom: 20px;
}

.text-content {
  display: inline-block;
}

/* 速度変化カーソル */
.speed-cursor {
  display: inline-block;
  width: 2px;
  height: 2rem;
  background-color: #28a745;
  margin-left: 2px;
  animation: speedBlink 0.8s infinite;
}

@keyframes speedBlink {
  0%, 50% {
    opacity: 1;
    background-color: #28a745;
  }
  51%, 100% {
    opacity: 0.3;
    background-color: #20c997;
  }
}

/* 高速タイピング */
.speed-cursor.fast {
  animation: fastBlink 0.3s infinite;
}

@keyframes fastBlink {
  0%, 50% {
    opacity: 1;
    background-color: #ffc107;
  }
  51%, 100% {
    opacity: 0.5;
    background-color: #fd7e14;
  }
}

/* 再開ボタン */
.restart-btn {
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.restart-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.restart-btn:active {
  transform: translateY(0);
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
  .typewriter-text {
    font-size: 1.5rem;
  }

  .speed-cursor {
    height: 1.5rem;
  }
}

④ JavaScript

class TypewriterWithSpeed {
  constructor(element, text, baseSpeed = 100, cursorElement) {
    this.element = element;
    this.text = text;
    this.baseSpeed = baseSpeed;
    this.currentIndex = 0;
    this.isTyping = false;
    this.cursor = cursorElement;
  }

  start() {
    if (this.isTyping) return;

    this.isTyping = true;
    this.currentIndex = 0;
    this.element.textContent = '';
    this.type();
  }

  type() {
    if (this.currentIndex < this.text.length) {
      this.element.textContent += this.text.charAt(this.currentIndex);
      this.currentIndex++;

      // 速度変化のロジック
      let speed = this.baseSpeed;
      if (this.currentIndex % 10 === 0) {
        speed = this.baseSpeed / 2; // 高速
        this.cursor.classList.add('fast');
        setTimeout(() => {
          this.cursor.classList.remove('fast');
        }, 500);
      } else if (this.currentIndex % 5 === 0) {
        speed = this.baseSpeed * 1.5; // 低速
      }

      setTimeout(() => this.type(), speed);
    } else {
      this.isTyping = false;
    }
  }

  stop() {
    this.isTyping = false;
    this.cursor.classList.remove('fast');
  }

  reset() {
    this.stop();
    this.element.textContent = '';
    this.currentIndex = 0;
  }
}

document.addEventListener('DOMContentLoaded', function() {
  const textElement4 = document.querySelector('#typewriter-text4 .text-content');
  const restartBtn4 = document.getElementById('restart-btn4');
  const cursor4 = document.querySelector('#typewriter-text4 .speed-cursor');

  const typewriter4 = new TypewriterWithSpeed(
    textElement4, 
    'This is a typewriter animation with dynamic speed changes! Watch how the speed varies as we type...',
    80,
    cursor4
  );

  // 自動開始
  typewriter4.start();

  // 再開ボタン
  restartBtn4.addEventListener('click', function() {
    typewriter4.reset();
    setTimeout(() => {
      typewriter4.start();
    }, 500);
  });
});

⑤ カスタマイズ例

/* より大きな速度変化 */
.speed-cursor.fast {
  animation: fastBlink 0.2s infinite;
}

/* カスタム速度カーソル */
.speed-cursor {
  background-color: #6f42c1;
  border-radius: 1px;
}

カラー変化

文字色が変化するタイプライター効果です。

① デモ

See the Pen タイプライターカラー変化 by ケケンタ (@lgshifbg-the-looper) on CodePen.

このカラー変化効果の特徴
  • 美しい色の変化
  • 視覚的インパクト大
  • 注目を集める効果

② HTML

<div class="typewriter-container">
  <div class="typewriter-text" id="typewriter-text5">
    <span class="text-content"></span>
    <span class="color-cursor"></span>
  </div>
  <button class="restart-btn" id="restart-btn5">再開</button>
</div>

③ CSS

/* タイプライターコンテナ */
.typewriter-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  padding: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  font-family: 'Courier New', monospace;
}

/* タイプライターテキスト */
.typewriter-text {
  font-size: 2rem;
  font-weight: 600;
  line-height: 1.5;
  text-align: center;
  margin-bottom: 20px;
}

.text-content {
  display: inline-block;
}

/* カラー変化カーソル */
.color-cursor {
  display: inline-block;
  width: 2px;
  height: 2rem;
  background-color: #ff6b6b;
  margin-left: 2px;
  animation: colorBlink 1s infinite;
}

@keyframes colorBlink {
  0%, 50% {
    opacity: 1;
    background-color: #ff6b6b;
  }
  51%, 100% {
    opacity: 0.3;
    background-color: #4facfe;
  }
}

/* カラー変化テキスト */
.color-text {
  background: linear-gradient(45deg, #ff6b6b, #4facfe, #43e97b, #f093fb);
  background-size: 400% 400%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: colorShift 3s ease-in-out infinite;
}

@keyframes colorShift {
  0%, 100% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
}

/* 再開ボタン */
.restart-btn {
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.restart-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.restart-btn:active {
  transform: translateY(0);
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
  .typewriter-text {
    font-size: 1.5rem;
  }

  .color-cursor {
    height: 1.5rem;
  }
}

④ JavaScript

class TypewriterWithColor {
  constructor(element, text, speed = 100, cursorElement) {
    this.element = element;
    this.text = text;
    this.speed = speed;
    this.currentIndex = 0;
    this.isTyping = false;
    this.cursor = cursorElement;
    this.colors = ['#ff6b6b', '#4facfe', '#43e97b', '#f093fb', '#667eea'];
  }

  start() {
    if (this.isTyping) return;

    this.isTyping = true;
    this.currentIndex = 0;
    this.element.textContent = '';
    this.type();
  }

  type() {
    if (this.currentIndex < this.text.length) {
      const char = this.text.charAt(this.currentIndex);
      const span = document.createElement('span');
      span.textContent = char;
      span.style.color = this.colors[this.currentIndex % this.colors.length];
      span.style.transition = 'color 0.3s ease';
      this.element.appendChild(span);

      this.currentIndex++;
      setTimeout(() => this.type(), this.speed);
    } else {
      this.isTyping = false;
    }
  }

  stop() {
    this.isTyping = false;
  }

  reset() {
    this.stop();
    this.element.textContent = '';
    this.currentIndex = 0;
  }
}

document.addEventListener('DOMContentLoaded', function() {
  const textElement5 = document.querySelector('#typewriter-text5 .text-content');
  const restartBtn5 = document.getElementById('restart-btn5');
  const cursor5 = document.querySelector('#typewriter-text5 .color-cursor');

  const typewriter5 = new TypewriterWithColor(
    textElement5, 
    'Colorful typewriter animation with beautiful color transitions!',
    100,
    cursor5
  );

  // 自動開始
  typewriter5.start();

  // 再開ボタン
  restartBtn5.addEventListener('click', function() {
    typewriter5.reset();
    setTimeout(() => {
      typewriter5.start();
    }, 500);
  });
});

⑤ カスタマイズ例

/* 異なるカラーパレット */
.color-text {
  background: linear-gradient(45deg, #667eea, #764ba2, #f093fb, #f5576c);
}

/* より大きなカラー変化 */
.color-cursor {
  animation: colorBlink 0.5s infinite;
}

フォント変化

フォントが変化するタイプライター効果です。

① デモ

See the Pen タイプライターフォント変化 by ケケンタ (@lgshifbg-the-looper) on CodePen.

このフォント変化効果の特徴
  • 動的なフォント変化
  • 視覚的インパクト大
  • 注目を集める効果

② HTML

<div class="typewriter-container">
  <div class="typewriter-text" id="typewriter-text6">
    <span class="text-content"></span>
    <span class="font-cursor"></span>
  </div>
  <button class="restart-btn" id="restart-btn6">再開</button>
</div>

③ CSS

/* タイプライターコンテナ */
.typewriter-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  padding: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  font-family: 'Courier New', monospace;
}

/* タイプライターテキスト */
.typewriter-text {
  font-size: 2rem;
  font-weight: 600;
  line-height: 1.5;
  text-align: center;
  margin-bottom: 20px;
}

.text-content {
  display: inline-block;
}

/* フォント変化カーソル */
.font-cursor {
  display: inline-block;
  width: 2px;
  height: 2rem;
  background-color: #6f42c1;
  margin-left: 2px;
  animation: fontBlink 0.8s infinite;
}

@keyframes fontBlink {
  0%, 50% {
    opacity: 1;
    background-color: #6f42c1;
  }
  51%, 100% {
    opacity: 0.3;
    background-color: #e83e8c;
  }
}

/* フォント変化テキスト */
.font-text {
  font-family: 'Courier New', monospace;
  transition: font-family 0.3s ease;
}

.font-text.serif {
  font-family: 'Times New Roman', serif;
}

.font-text.sans {
  font-family: 'Arial', sans-serif;
}

.font-text.fancy {
  font-family: 'Brush Script MT', cursive;
}

/* 再開ボタン */
.restart-btn {
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.restart-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.restart-btn:active {
  transform: translateY(0);
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
  .typewriter-text {
    font-size: 1.5rem;
  }

  .font-cursor {
    height: 1.5rem;
  }
}

④ JavaScript

class TypewriterWithFont {
  constructor(element, text, speed = 100, cursorElement) {
    this.element = element;
    this.text = text;
    this.speed = speed;
    this.currentIndex = 0;
    this.isTyping = false;
    this.cursor = cursorElement;
    this.fonts = [
      'Courier New, monospace',
      'Times New Roman, serif',
      'Arial, sans-serif',
      'Brush Script MT, cursive',
      'Georgia, serif'
    ];
  }

  start() {
    if (this.isTyping) return;

    this.isTyping = true;
    this.currentIndex = 0;
    this.element.textContent = '';
    this.type();
  }

  type() {
    if (this.currentIndex < this.text.length) {
      const char = this.text.charAt(this.currentIndex);
      const span = document.createElement('span');
      span.textContent = char;
      span.style.fontFamily = this.fonts[this.currentIndex % this.fonts.length];
      span.style.transition = 'font-family 0.3s ease';
      this.element.appendChild(span);

      this.currentIndex++;
      setTimeout(() => this.type(), this.speed);
    } else {
      this.isTyping = false;
    }
  }

  stop() {
    this.isTyping = false;
  }

  reset() {
    this.stop();
    this.element.textContent = '';
    this.currentIndex = 0;
  }
}

document.addEventListener('DOMContentLoaded', function() {
  const textElement6 = document.querySelector('#typewriter-text6 .text-content');
  const restartBtn6 = document.getElementById('restart-btn6');
  const cursor6 = document.querySelector('#typewriter-text6 .font-cursor');

  const typewriter6 = new TypewriterWithFont(
    textElement6, 
    'Dynamic font changes create amazing visual effects!',
    120,
    cursor6
  );

  // 自動開始
  typewriter6.start();

  // 再開ボタン
  restartBtn6.addEventListener('click', function() {
    typewriter6.reset();
    setTimeout(() => {
      typewriter6.start();
    }, 500);
  });
});

⑤ カスタマイズ例

/* 異なるフォントセット */
.font-text {
  font-family: 'Roboto', sans-serif;
}

/* より大きなフォント変化 */
.font-cursor {
  animation: fontBlink 0.5s infinite;
}

まとめ

今回ご紹介した6種類のタイプライターアニメーションは、それぞれ異なる特徴と用途があります。

用途別おすすめ

  • ヒーローセクション: 基本タイプライター・高度なカーソル効果
  • ストーリーテリング: 削除効果・速度変化
  • プロダクト紹介: カラー変化・フォント変化
  • エラーメッセージ: 文字表示・カーソル点滅

実装のポイント

  1. アクセシビリティを重視: スクリーンリーダー対応
  2. パフォーマンスを考慮: 軽量なアニメーション
  3. ブラウザ対応: 幅広いブラウザで動作するように
  4. ユーザビリティ: 直感的な操作を可能に
ケケンタ

タイプライターアニメーションは、ユーザーの注目を集め、ストーリー性のある表現を実現する重要な要素です。この記事のコードを参考に、プロジェクトに最適なアニメーションを実装してください!

あわせて読みたい

もっと効率的にWeb制作を学びたい方へ

Web制作の学習は楽しいものですが、一人で進めていると「これで合っているのかな?」と不安になることもありますよね。

僕も独学でWeb制作を学んできましたが、今思うと「もっと早く知りたかった」と思うことがたくさんあります。

特に以下のような方は、一度プログラミングスクールの利用を検討してみることをおすすめします。

  • 学習の方向性に迷いがある方
  • 効率的にスキルを身につけたい方
  • 転職や副業でWeb制作を活用したい方
  • 挫折経験がある方

忍者CODEなら、業界最安値で24時間サポート付きの学習環境が整っています。

ご興味のある方は、こちらの記事で詳しくご紹介しています。

関連記事

基本的なアニメーション

スクロール系

メニュー・タブ・ナビゲーション

どれを読むか迷ったときのおすすめ‼/

フォーム・UI要素

どれを読むか迷ったときのおすすめ‼/

ボタン

どれを読むか迷ったときのおすすめ‼/

テキスト

スライダー

特殊効果

【コピペOK】タイプライターアニメーション完全ガイド|6種類【Web制作】のアイキャッチ画像

この記事が気に入ったら
フォローしてね!

この記事が良いと思ったらシェアしてね!

コメント

コメントする

CAPTCHA


目次