JS實現(xiàn)微信播音效果示例詳解
需要實現(xiàn)的效果

圖片切換輪播法
這個功能其實是我剛畢業(yè)的時候?qū)崿F(xiàn)的,那也是5年前的事情了,受限于當時的水平,僅僅是實現(xiàn)了,其他啥都不是。相當簡單。
當初微信的聲音條還是豎狀的,所以依舊按照但是的樣子來實現(xiàn)。
看下面的這個圖片就知道了,甚至于代碼都不用貼??:

并不是雪碧圖,當初不知道雪碧圖是啥玩意兒,就是九張獨立的圖片。上圖是使用Excalidraw來繪制。
就是九個圖片,給它們基本統(tǒng)一的命名,給一個定時器進行循環(huán)的切換,部分代碼如下:
const voiceBox = document.queryselector(ns.w('voice', 'box')) // 有在改DOM什么顯示音頻的變動
const index = [9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const num = index.length
const startVoice = () => {
timer = setInterval(() => {
num++
const _curBg = require(`@/assets/images/record_ripple-${index[num]}`.png)
voiceBox.style.background = `url('${_curBg}') no-repeat 111.2px 32px/28.8px 88px`
voiceBox.style.backgroundColor = ' rgba(0,0,0,.7)'
// 從頭來過
if (num >= index.length - 1) {
num = 0
}
}, 70)
這樣的實現(xiàn)效果,給9張圖起名字的時間超過寫代碼的時間。而且毫無靈活性,能夠?qū)崿F(xiàn)什么效果全依靠UI人好不好,你和他喝酒的時候杯子夠不夠底下。
不管是為了性能還是為了擴展,使用CSS來實現(xiàn)顯然是更好選擇。
CSS實現(xiàn)
使用anmation的steps也可以實現(xiàn)和上面一模一樣的邏輯。
如下代碼:
<div class="voice-wrap">
<div class="voice-box">
<div class="voice-item one">
</div>
<div class="voice-item two">
</div>
<div class="voice-item tree">
</div>
</div>
</div>
:root {
--vv-b-r: 10px;
--vv-a-s: 0.1s;
}
.voice-wrap {
background-color: rgba(111, 111, 111, 0.5);
border-radius: var(--vv-b-r);
width: 80px;
height: 80px;
overflow: hidden;
}
.voice-box {
display: flex;
align-items: center;
width: fit-content;
animation: sprite 0.6s steps(3, end) infinite;
}
.voice-item {
height: 10px;
border-radius: var(--vv-b-r);
width: 80px;
height: 80px;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
@keyframes sprite {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-240px);
}
}
關鍵的屬性就是animation: steps。 從命名可以看出它是一個步進相關的東西。每一個.voice-item的寬度時80px,那么它的animation的translateX為-240px。
意思就是動畫將.voice-box節(jié)點從0px位置右移到-240px的位置,steps的動畫效果,讓它移動三次完結(jié),一次移動80px。
畫一個圖來表示這個過程:

可以說和輪播圖一摸一樣。
最后給voice-item節(jié)點中添加樣式:
<div class="voice-item one">
<div class="line-item"></div>
<div class="line-item"></div>
<div class="line-item">
<div class="line-item line-one"></div>
</div>
</div>
<div class="voice-item two">
<div class="line-item"></div>
<div class="line-item">
<div class="line-item line-one"></div>
</div>
<div class="line-item">
<div class="line-item line-one"></div>
</div>
</div>
<div class="voice-item tree">
<div class="line-item">
<div class="line-item line-one"></div>
</div>
<div class="line-item">
<div class="line-item line-one"></div>
</div>
<div class="line-item">
<div class="line-item line-one"></div>
</div>
</div>
.line-item {
height: 10px;
width: 60px;
background-color: rgba(111, 111, 111, 0.6);
border-radius: var(--vv-b-r);
}
.line-one {
background-color: black;
}
最終實現(xiàn)效果如下:

以上就是JS實現(xiàn)微信播音效果示例詳解的詳細內(nèi)容,更多關于JS微信播音效果的資料請關注腳本之家其它相關文章!
相關文章
Svelte調(diào)試模式js級別差異和細化后的體積差異詳解
這篇文章主要為大家介紹了Svelte調(diào)試模式js級別差異和細化后的體積差異詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
JS網(wǎng)頁repaint與reflow?的區(qū)別及優(yōu)化方式
這篇文章主要為大家介紹了JS網(wǎng)頁repaint與reflow?的區(qū)別及優(yōu)化方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
微信小程序 scroll-view組件實現(xiàn)列表頁實例代碼
這篇文章主要介紹了微信小程序 scroll-view組件實現(xiàn)列表頁實例代碼的相關資料,scroll-view組件介紹scroll-view是微信小程序提供的可滾動視圖組件,其主要作用是可以用來做手機端經(jīng)常會看到的上拉加載 ,需要的朋友可以參考下2016-12-12

