效果图
项目demo链接
实习思路
<div>
<div class="demo"></div>
<div class="btn" onclick="reload()">again</div>
</div>
<style>
@import url("https://fonts.googleapis.com/css?family=Raleway:400,400i,700");
body {
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
text-align: center;
background: #222;
}
.btn {
color: #333;
background: #fff;
padding: 5px;
width: 50px;
border-radius: 10px;
margin: 5px auto;
}
.demo {
position: relative;
display: flex;
color: #6ee1f5;
font-size: 2em;
font-family: Raleway, sans-serif;
letter-spacing: 3px;
text-transform: uppercase;
white-space: pre;
}
.demo span {
opacity: 0;
transform: scale(0);
animation: fadeIn 2.4s forwards;
}
.demo span::before {
left: 50%;
animation: slideLeft 1.5s cubic-bezier(0.7, -0.6, 0.3, 1.5) forwards;
}
.demo span::after {
right: 50%;
animation: slideRight 1.5s cubic-bezier(0.7, -0.6, 0.3, 1.5) forwards;
}
@keyframes fadeIn {
to {
opacity: 1;
transform: scale(1);
}
}
@keyframes slideLeft {
to {
left: -6%;
opacity: 1;
transform: scale(0.9);
}
}
@keyframes slideRight {
to {
right: -6%;
opacity: 1;
transform: scale(0.9);
}
}
</style>
<script>
function start() {
const delay = 0.3;
let text = 'need for speed';
let letters = text.split('');
let target = document.getElementsByClassName('demo')[0];
let middle = letters.filter((e) => e !== ' ').length / 2;
letters.forEach((item, index) => {
let span = document.createElement('span');
span.textContent = item;
span.style.animationDelay = `${delay + Math.abs(index - middle) * 0.1}s`;
target.append(span);
});
}
function clear() {
let target = document.getElementsByClassName('demo')[0];
const childs = target.childNodes;
while (target.hasChildNodes()) {
target.removeChild(target.firstChild);
}
}
function reload() {
clear();
start()
}
start();
</script>
|