b站看到的,跟着思路敲了一遍
html部分
<section>
<p class="wang">
侯非侯,王非王,千乘万骑走北邙
</p>
</section>
css部分
body{
margin: 0;
padding: 0;
}
section{
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
background-color: #111;
overflow: hidden;
}
section .wang{
position: relative;
color: #fff;
margin: 20px;
max-width: 800px;
/* 用户不能选中 */
user-select: none;
}
section .wang span{
position: relative;
display: inline-block;
cursor: pointer;
}
section .wang span.active{
animation: smoke 4s linear forwards;
transform-origin: bottom;
}
@keyframes smoke{
0%{
opacity: 1;
filter: blur(0);
transform: translateX(0) translateY(0) rotate(0deg) scale(1);
}
30%{
opacity: 0;
filter: blur(20px);
transform: translateX(300px) translateY(-300px) rotate(720deg) scale(4);
}
100%{
opacity: 1;
transform: translateX(0) translateY(0) rotate(0deg) scale(1);
}
}
js部分
<script>
const wang = document.querySelector('.wang');
//将匹配到的每一个字符用span标签进行包裹
wang.innerHTML = wang.textContent.replace(/\S/g,"<span>$&</span>")
const xixi = document.querySelectorAll("span");
for(let i = 0; i < xixi.length; i++){
xixi[i].addEventListener('mouseover',function(){
//当鼠标经过每个span标签包裹的文字时,会自动给该字母加上active类
xixi[i].classList.add('active')
})
}
</script>