Skip to content

Instantly share code, notes, and snippets.

@YeomanYe
Last active December 24, 2016 14:10
Show Gist options
  • Save YeomanYe/066a1b7e1ad2f8d48696e8c097f708c1 to your computer and use it in GitHub Desktop.
Save YeomanYe/066a1b7e1ad2f8d48696e8c097f708c1 to your computer and use it in GitHub Desktop.
HTML:animation typewriting
<h1>CSS is awesome!</h1>
<style>
@keyframes typing {
from { width: 0 }
}
@keyframes caret {
50% { border-color: currentColor; }
}
h1 {
width: 15ch; /* 文本的宽度 */
overflow: hidden;
/*不让文本折行*/
white-space: nowrap;
/*使用边框模拟光标闪烁效果,并且在不支持时平稳回退为不显示边框*/
border-right: .05em solid transparent;
animation: typing 6s steps(15),
caret 1s steps(1) infinite;
}
</style>
<script>
/*根据文字长度动态调整动画,文本宽度*/
$$('h1').forEach(function(h1) {
var len = h1.textContent.length, s = h1.style;
s.width = len + 'ch';
s.animationTimingFunction = "steps("+len+"),steps(1)";
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment