张浩宸 發表於 2026-5-3 22:16:37

CSS3动画实现多个跳动小球效果(语音输入动画)

<h3>VUE使用CSS3动画实现多个跳动小球(语音输入动画)</h3>
<p>之前实习期间,有做过一个需求,安卓端嵌H5页面,实现语音输入并包含输入时动画,跳动的小球。通过查阅各种资料,根据实际需求场景,最终实现了其功能。在此便回顾记录一下吧。</p>
<h3>单个小球无限跳动</h3>
<p>首先,实现单个小球跳动。<br />分析: 小球起始位置在顶部,中间时间段到底部,最后又回到顶部,并且是无限循环的。通过相对定位与CSS3的关键帧结合实现。</p>
<div class="jb51code"><pre class="brush:css;">&lt;div class="ball"&gt;&lt;/div&gt;</pre></div>
<div class="jb51code"><pre class="brush:css;">.ball {
width: 20px;
height: 20px;
border-radius: 50%;
position: relative;
animation-name: bouncing; // 动画名称
animation-duration: 1.6s; // 单次动画持续时长
animation-iteration-count: infinite; // 动画无限循环
background: rgb(244, 7, 7);// 小球背景色
}
// 关键帧动画
@keyframes bouncing {
0% {
    top: 0px; // 初始位于顶部
}
50% {
    top: 100px; // 中间位于底部
}
100% {
    top: 0px; // 最终回到顶部
}
}</pre></div>
<h3>多个小球跳动</h3>
<p>分析: 多个小球同时跳动,相对定位需要left不相同,其次每个小球动画开始有时间差,其次就是小球颜色了。</p>
<div class="jb51code"><pre class="brush:css;">/** balls = 多个小球 */
&lt;div v-for="ball in balls" :key="ball" :class="['ball', `ball${ball}`]"&gt;&lt;/div&gt;</pre></div>
<div class="jb51code"><pre class="brush:css;">// 公共样式抽离
.ball {
width: 20px;
height: 20px;
border-radius: 50%;
position: relative;
animation-name: bouncing; // 动画名称
animation-duration: 1.6s; // 单次动画持续时长
animation-iteration-count: infinite; // 动画无限循环
}
.ball1 {
@extend .ball;
left: 0;
background: rgb(244, 7, 7);
}
.ball2 {
@extend .ball;
animation-delay: 0.25s; // 动画延迟
left: 30px;
background: rgb(16, 106, 241);
}
.ball3 {
@extend .ball;
animation-delay: 0.5s; // 动画延迟
left: 60px;
background: rgb(251, 236, 13);
}
.ball4 {
@extend .ball;
animation-delay: 0.75s; // 动画延迟
left: 90px;
background: rgb(233, 23, 233);
}
.ball5 {
@extend .ball;
animation-delay: 1.0s; // 动画延迟
left: 120px;
background: rgb(6, 247, 6);
}
// 关键帧动画
@keyframes bouncing {
0% {
    top: 0px; // 初始位于顶部
}
50% {
    top: 100px; // 中间位于底部
}
100% {
    top: 0px; // 最终回到顶部
}
}</pre></div>
<p>Demo</p>
<p>分析: 绑定事件监听,按钮长按动画显示,按钮松开动画隐藏。<br />最后,就是投入使用,看一下实现的效果了。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202408/20240829160530107.png" /></p>
<div class="jb51code"><pre class="brush:css;">&lt;el-button id="bouncingBallBtn"&gt;语音录入&lt;/el-button&gt;
&lt;bouncing-ball v-if="showBouncing" /&gt;</pre></div>
<div class="jb51code"><pre class="brush:css;">/** data showBouncing: false */
mounted() {
        let theBouncingBtn = document.getElementById("bouncingBallBtn");
        // 移动端
        theBouncingBtn.addEventListener("touchstart", this.startBouncing, false);
        theBouncingBtn.addEventListener("touchend", this.endBouncing, false);
        // pc端
        theBouncingBtn.addEventListener("mousedown", this.startBouncing, false);
        theBouncingBtn.addEventListener("mouseup", this.endBouncing, false);
}
/** 动画显示 */
startBouncing(event) {
   event.preventDefault();
   this.showBouncing = true;
},
/** 动画隐藏 */
endBouncing(event) {
   event.preventDefault();
   this.showBouncing = false;
},</pre></div>
頁: [1]
查看完整版本: CSS3动画实现多个跳动小球效果(语音输入动画)