css 打字動畫

來源:互聯網
上載者:User

標籤:

使用 css 將文字逐字打出

 

<h1>css is awesome</h1>

要使<h1>標籤裡的文字逐字打出,對應的樣式如下:

  h1{    width: 14ch;/×文本的寬度×/    overflow: hidden;    white-space: nowrap;    border-right: .05em solid transparent;    animation: typing 4s steps(14),              caret 1s steps(1) infinite;  }  @keyframes typing{    from{ width: 0 }  }  @-webkit-keyframes typing{    from{ width: 0 }  }  @keyframes caret{    50%{      border-color: currentColor;    }  }  @-webkit-keyframes caret{    50%{      border-color: currentColor;    }  }

 

ch單位是css3引入的一個新單位,表示“0”字形的寬度,在等寬字型中,“0”字形的寬度和其他所有字形的寬度一樣。

例子中用ch表示這個標題的寬度,取值就是字元的數量,為14。

由於h1標籤裡的文字可能有變化,為此引入如下js:

var len = $("h1").text().length;$("h1").css("width",len+"ch");$("h1").css("animationTimingFunction","steps("+len+"),steps(1)");

 

css 打字動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.