原视频:CodingStartup起码课
<!--html-->
<h1>消灭嘿嘿暴政 世界属于嘻嘻</h1>
:root{
font-size:20px;
}
body{
display:flex;
justify-content:center;
align-items:center;
min-height:100vh;
}
h1{
margin: 0;
padding: 0;
font-size:3rem;
font-family:serif;
position:relative
}
h1::after{
content:"";
display:inline-block;
position:absolute;
width:5px;
height:3rem;
background-color: #000;
border-radius:2px;
right:-10px;
bottom:50%;
transform:translatey(50%);
}
h1.ended::after{
animation:1.1s cursor steps(2,jump-none) infinite;
}
@keyframes cursor{
from{
opacity:0
}
to{
opacity:1
}
}
h1 span{
--delay:10s;
display:inline-block;
overflow:hidden;
width:0em;
height:3rem;
animation:0.1s text-in ease-in-out forwards;
animation-delay:var(--delay);
}
@keyframes text-in{
from{
width:0em;
}
to{
width:1em;
}
}
const h1=document.querySelector('h1')
h1.innerHTML=h1.textContent.replace(/\S/g,"<span>$&</span>");
let delay=0;
document.querySelectorAll("span").forEach((span,index)=>{
console.log(span,index)
delay +=0.1;
if(index==4) {delay +=.3;}
span.style.setProperty('--delay',`${delay}s`);
})
h1.addEventListener("animationend",(e)=>{
if(e.target===document.querySelector("h1 span:last-child")){
h1.classList.add("ended");
}
})