
本教程详细介绍了如何使用javascript和css创建一个交互式气泡效果。用户点击气泡后,气泡会暂时消失,并在指定时间后自动重新出现。文章通过优化原始的重复代码,展示了如何利用一个通用的javascript函数结合`settimeout`机制,实现高效且可维护的气泡消失与重现逻辑,并提供了完整的代码示例和最佳实践建议。
在网页设计中,动态的视觉效果能够显著提升用户体验。气泡动画作为一种常见的背景或交互元素,常用于营造轻松活泼的氛围。本教程将指导您如何创建一个可点击的动态气泡,使其在被点击后“破裂”(消失),并在短暂延迟后“重生”(重新出现),从而形成一个持续的交互循环。我们将重点关注如何优化JavaScript代码,避免重复,并引入定时重现的机制。
最初的实现可能为每个气泡编写一个独立的JavaScript函数,例如 changeStyle1(), changeStyle2() 等,这些函数都执行相同的操作:将特定气泡的 opacity 样式设置为 0,使其消失。这种方法存在两个主要问题:
为了解决这些问题,我们需要一个更通用、更灵活的解决方案,即使用一个函数处理所有气泡的点击事件,并引入定时器使其重现。
核心优化在于创建一个通用的JavaScript函数,该函数能够接收被点击气泡的ID作为参数,并执行两步操作:首先使其消失,然后通过setTimeout函数在一定延迟后使其重新出现。
立即学习“Java免费学习笔记(深入)”;
/**
* 处理气泡点击事件,使其消失并在指定时间后重现。
* @param {string} id - 被点击气泡的HTML元素ID。
*/
const changeStyleToBubble = (id) => {
// 1. 获取目标元素
const element = document.getElementById(id);
// 2. 使元素立即消失 (设置不透明度为0)
element.style.opacity = "0";
// 3. 设置定时器,在2秒后使元素重新出现 (设置不透明度为1)
setTimeout(() => {
element.style.opacity = "1";
}, 2000); // 2000毫秒 = 2秒
};为了让优化后的JavaScript函数能够正确工作,我们需要修改HTML中气泡的 onclick 事件处理器。不再调用特定的 changeStyleX() 函数,而是调用通用的 changeStyleToBubble() 并传入当前元素的ID。
<button id="pop1" onclick="changeStyleToBubble(this.id)" class="bubble x1"> <span></span> <span></span> <span></span> <span></span> <span></span> </button> <button id="pop2" onclick="changeStyleToBubble(this.id)" class="bubble x2"> <span></span> <span></span> <span></span> <span></span> <span></span> </button> <!-- ... 其他气泡元素以此类推 ... -->
为了提供一个可运行的示例,我们将结合HTML、CSS和JavaScript。CSS部分负责气泡的视觉样式和动画,JavaScript负责交互逻辑。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>可点击气泡动态重现效果</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="background-wrap">
<button id="pop1" onclick="changeStyleToBubble(this.id)" class="bubble x1">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop2" onclick="changeStyleToBubble(this.id)" class="bubble x2">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop3" onclick="changeStyleToBubble(this.id)" class="bubble x3">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop4" onclick="changeStyleToBubble(this.id)" class="bubble x4">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop5" onclick="changeStyleToBubble(this.id)" class="bubble x5">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop6" onclick="changeStyleToBubble(this.id)" class="bubble x6">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop7" onclick="changeStyleToBubble(this.id)" class="bubble x7">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop8" onclick="changeStyleToBubble(this.id)" class="bubble x8">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop9" onclick="changeStyleToBubble(this.id)" class="bubble x9">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop10" onclick="changeStyleToBubble(this.id)" class="bubble x10">
<span></span><span></span><span></span><span></span><span></span>
</button>
<button id="pop11" onclick="changeStyleToBubble(this.id)" class="bubble x11">
<span></span><span></span><span></span><span></span><span></span>
</button>
</div>
<script src="script.js"></script>
</body>
</html>body {
background: #000;
color: #333;
font: 100% Lato, Arial, Sans Serif;
height: 100vh;
margin: 0;
padding: 0;
overflow-x: hidden;
}
button {
background: transparent;
border-color: transparent;
cursor: pointer; /* 添加光标指示,表明是可点击元素 */
padding: 0; /* 移除默认内边距 */
}
.bubble {
position: fixed;
width: 200px;
height: 200px;
border-radius: 50%;
box-shadow: inset 0 0 25px rgba(255, 255, 255, 0.25);
transition: opacity 0.3s ease-in-out; /* 添加过渡效果,使消失和出现更平滑 */
}
.bubble:after {
content: '';
position: absolute;
top: 80px;
left: 80px;
width: 20px;
height: 20px;
border-radius: 50%;
background: #fff;
z-index: 10;
filter: blur(2px);
}
.bubble::before {
content: '';
position: absolute;
top: 50px;
left: 45px;
width: 30px;
height: 30px;
border-radius: 50%;
background: #fff;
z-index: 10;
filter: blur(2px);
}
.bubble span {
position: absolute;
border-radius: 50%;
}
.bubble span:nth-child(1) {
inset: 10px;
border-left: 15px solid #0fb4ff;
filter: blur(8px);
}
.bubble span:nth-child(2) {
inset: 10px;
border-right: 15px solid #ff4484;
filter: blur(8px);
}
.bubble span:nth-child(3) {
inset: 20px;
border-top: 15px solid #ffeb3b;
filter: blur(8px);
}
.bubble span:nth-child(4) {
inset: 30px;
border-left: 15px solid #ff4484;
filter: blur(12px);
}
.bubble span:nth-child(5) {
inset: 10px;
border-bottom: 10px solid #fff;
filter: blur(8px);
transform: rotate(330deg);
}
/* 气泡动画 */
.x1 {
animation: animateBubble 25s linear infinite, sideWays 2s ease-in-out infinite alternate;
left: -5%;
top: 5%;
transform: scale(0.6);
}
.x2 {
animation: animateBubble 20s linear infinite, sideWays 4s ease-in-out infinite alternate;
left: 5%;
top: 80%;
transform: scale(0.4);
}
.x3 {
animation: animateBubble 28s linear infinite, sideWays 2s ease-in-out infinite alternate;
left: 10%;
top: 40%;
transform: scale(0.7);
}
.x4 {
animation: animateBubble 22s linear infinite, sideWays 3s ease-in-out infinite alternate;
left: 20%;
top: 0;
transform: scale(0.3);
}
.x5 {
animation: animateBubble 29s linear infinite, sideWays 4s ease-in-out infinite alternate;
left: 30%;
top: 50%;
transform: scale(0.5);
}
.x6 {
animation: animateBubble 21s linear infinite, sideWays 2s ease-in-out infinite alternate;
left: 50%;
top: 0;
transform: scale(0.8);
}
.x7 {
animation: animateBubble 20s linear infinite, sideWays 2s ease-in-out infinite alternate;
left: 65%;
top: 70%;
transform: scale(0.4);
}
.x8 {
animation: animateBubble 22s linear infinite, sideWays 3s ease-in-out infinite alternate;
left: 80%;
top: 10%;
transform: scale(0.3);
}
.x9 {
animation: animateBubble 29s linear infinite, sideWays 4s ease-in-out infinite alternate;
left: 90%;
top: 50%;
transform: scale(0.6);
}
.x10 {
animation: animateBubble 26s linear infinite, sideWays 2s ease-in-out infinite alternate;
left: 80%;
top: 80%;
transform: scale(0.3);
}
.x11 {
animation: animateBubble 19s linear infinite, sideWays 3s ease-in-out infinite alternate;
left: 90%;
top: 90%;
transform: scale(0.7);
}
/* 关键帧动画 */
@keyframes animateBubble {
0% { margin-top: 1000px; }
100% { margin-top: -100%; }
}
@keyframes sideWays {
0% { margin-left: 0px; }
100% { margin-left: 50px; }
}/**
* 处理气泡点击事件,使其消失并在指定时间后重现。
* @param {string} id - 被点击气泡的HTML元素ID。
*/
const changeStyleToBubble = (id) => {
const element = document.getElementById(id);
element.style.opacity = "0"; // 气泡消失
// 2秒后气泡重现
setTimeout(() => {
element.style.opacity = "1";
}, 2000);
};通过本教程,我们学习了如何利用JavaScript的 setTimeout 函数和简洁的HTML事件处理机制,实现一个动态且交互性强的气泡效果。这种方法不仅解决了代码冗余问题,还为气泡增添了“破裂与重生”的生命周期,使网页内容更具吸引力。掌握这种模式,您可以将其应用于各种需要定时交互和动态视觉效果的场景中。
以上就是JavaScript与CSS实现可点击气泡的动态重现效果的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号