bash
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>点击触发动画效果</title>
<link rel="stylesheet" href="styles.css" />
</head>
<style>
/* styles.css */
@keyframes riseAndFade {
from {
transform: translateY(0);
opacity: 1;
}
to {
transform: translateY(-100px);
opacity: 0;
}
}
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
cursor: pointer;
}
.rise-fade {
position: absolute;
font-size: 20px;
font-weight: 700;
white-space: nowrap;
animation: riseAndFade 1.5s ease forwards;
pointer-events: none;
}
</style>
<body>
<script>
// script.js
document.addEventListener("DOMContentLoaded", function () {
var phrases = [
"VUE2!",
"VUE3!",
"React",
"Node",
"Element UI",
];
document.body.addEventListener("click", function (event) {
var randomPhrase =
phrases[Math.floor(Math.random() * 6)];
var textElement = document.createElement("div");
textElement.className = "rise-fade";
textElement.textContent = randomPhrase;
textElement.style.color = "hsl(" + Math.random() * 360 + ", 50%, 50%)";
document.body.appendChild(textElement);
textElement.style.left =
event.pageX - textElement.offsetWidth / 2 + "px";
textElement.style.top = event.pageY - textElement.offsetHeight + "px";
setTimeout(function () {
textElement.remove();
}, 1100); // 100ms延迟确保动画完成
});
});
</script>
</body>
</html>