纯CSS实现字符串打字特效附带擦除重写效果(内含tailwind css版)

原生CSS实现实现的打字和擦除重写效果

HTML

<h1 aria-label="Hi! I'm a developer">
  Hi! I'm a&nbsp;<span class="typewriter"></span>
</h1>

<h1 aria-label="Hi! I'm a developer">
  Hi! I'm a&nbsp;<span class="typewriter thick"></span>
</h1>

<h1 aria-label="Hi! I'm a developer">
  Hi! I'm a&nbsp;<span class="typewriter nocaret"></span>
</h1>

CSS

h1 {
  font-size: 9vmin;
  color: #8bf;
  text-align: left;
  font-family: Lato, sans-serif;
  font-weight: 700;
  margin: 1rem 0 1rem 2rem;
}

/* Typewriter effect 1 */
@keyframes typing {
  0.0000%, 27.3488% { content: ""; }
  1.1395%, 26.2093% { content: "d"; }
  2.2791%, 25.0698% { content: "de"; }
  3.4186%, 23.9302% { content: "dev"; }
  4.5581%, 22.7907% { content: "deve"; }
  5.6977%, 21.6512% { content: "devel"; }
  6.8372%, 20.5116% { content: "develo"; }
  7.9767%, 19.3721% { content: "develop"; }
  9.1163%, 18.2326% { content: "develope"; }
  10.2558%, 17.0930% { content: "developer"; }

  30.7674%, 51.2791% { content: ""; }
  31.9070%, 50.1395% { content: "w"; }
  33.0465%, 49.0000% { content: "wr"; }
  34.1860%, 47.8605% { content: "wri"; }
  35.3256%, 46.7209% { content: "writ"; }
  36.4651%, 45.5814% { content: "write"; }
  37.6047%, 44.4419% { content: "writer"; }

  54.6977%, 75.2093% { content: ""; }
  55.8372%, 74.0698% { content: "r"; }
  56.9767%, 72.9302% { content: "re"; }
  58.1163%, 71.7907% { content: "rea"; }
  59.2558%, 70.6512% { content: "read"; }
  60.3953%, 69.5116% { content: "reade"; }
  61.5349%, 68.3721% { content: "reader"; }

  78.6279%, 96.8605% { content: ""; }
  79.7674%, 95.7209% { content: "h"; }
  80.9070%, 94.5814% { content: "hu"; }
  82.0465%, 93.4419% { content: "hum"; }
  83.1860%, 92.3023% { content: "huma"; }
  84.3256%, 91.1628% { content: "human"; }
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.typewriter {
  --caret: currentcolor;
}

.typewriter::before {
  content: "";
  animation: typing 13.5s infinite;
}

.typewriter::after {
  content: "";
  border-right: 1px solid var(--caret);
  animation: blink 0.5s linear infinite;
}

.typewriter.thick::after {
  border-right: 1ch solid var(--caret);
}

.typewriter.nocaret::after {
  border-right: 0;
}


@media (prefers-reduced-motion) {
  .typewriter::after {
    animation: none;
  }
  
  @keyframes sequencePopup {
    0%, 100% { content: "developer"; }
    25% { content: "writer"; }
    50% { content: "reader"; }
    75% { content: "human"; }
  }

  .typewriter::before {
    content: "developer";
    animation: sequencePopup 12s linear infinite;
  }
}

原生CSS打字效果(无重写)

# Option1

<style>
    .writer {
    font-family: Courier, monospace;
    display: inline-block;
    }
    .writer-text {
        display: inline-block;
        overflow: hidden;
        letter-spacing: 2px;
        animation: typing 5s steps(30, end), blink .75s step-end infinite;
        white-space: nowrap;
        font-size: 30px;
        font-weight: 700;
        border-right: 4px solid orange;
        box-sizing: border-box;
    }

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

    @keyframes blink {
        from, to { 
            border-color: transparent 
        }
        50% { 
            border-color: orange; 
        }
    }
</style>

HTML部分

          <div class="writer">
            <h1 class="writer-text overflow-hidden whitespace-nowrap border-r-4 border-r-white pr-5 text-5xl text-white font-bold">Building the Next Generation Websites</h1>
          </div>

#Option 2

<!DOCTYPE html>
<html>
<head>
    <style>
        .writer {
        font-family: Courier, monospace;
        display: inline-block;
        }
        .writer-text {
            display: inline-block;
            overflow: hidden;
            letter-spacing: 2px;
            animation: typing 5s steps(30, end), blink .75s step-end infinite;
            white-space: nowrap;
            font-size: 30px;
            font-weight: 700;
            border-right: 4px solid orange;
            box-sizing: border-box;
        }

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

        @keyframes blink {
            from, to { 
                border-color: transparent 
            }
            50% { 
                border-color: orange; 
            }
        }
    </style>
    <body>
        <div class="writer">
            <div class="writer-text">Twinkle, twinkle, little star.</div>
        </div>

    </body>
</html>

Tailwind CSS实现的打字特效(无擦除重写)

config

/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      keyframes: {
        typing: {
          "0%": {
            width: "0%",
            visibility: "hidden"
          },
          "100%": {
            width: "100%"
          }  
        },
        blink: {
          "50%": {
            borderColor: "transparent"
          },
          "100%": {
            borderColor: "white"
          }  
        }
      },
      animation: {
        typing: "typing 2s steps(20) infinite alternate, blink .7s infinite"
      }
    },
  },
  plugins: [],
}

HTML

<div class="flex min-h-screen items-center justify-center bg-gradient-to-tr to-blue-400 from-green-500 p-10">
  <div class="w-max">
    <h1 class="animate-typing overflow-hidden whitespace-nowrap border-r-4 border-r-white pr-5 text-5xl text-white font-bold">Hello World</h1>
  </div>
</div>

Post Comment