JS實(shí)現(xiàn)微信播音效果示例詳解
需要實(shí)現(xiàn)的效果
圖片切換輪播法
這個(gè)功能其實(shí)是我剛畢業(yè)的時(shí)候?qū)崿F(xiàn)的,那也是5年前的事情了,受限于當(dāng)時(shí)的水平,僅僅是實(shí)現(xiàn)了,其他啥都不是。相當(dāng)簡(jiǎn)單。
當(dāng)初微信的聲音條還是豎狀的,所以依舊按照但是的樣子來(lái)實(shí)現(xiàn)。
看下面的這個(gè)圖片就知道了,甚至于代碼都不用貼??:
并不是雪碧圖,當(dāng)初不知道雪碧圖是啥玩意兒,就是九張獨(dú)立的圖片。上圖是使用Excalidraw來(lái)繪制。
就是九個(gè)圖片,給它們基本統(tǒng)一的命名,給一個(gè)定時(shí)器進(jìn)行循環(huán)的切換,部分代碼如下:
const voiceBox = document.queryselector(ns.w('voice', 'box')) // 有在改DOM什么顯示音頻的變動(dòng) 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)' // 從頭來(lái)過 if (num >= index.length - 1) { num = 0 } }, 70)
這樣的實(shí)現(xiàn)效果,給9張圖起名字的時(shí)間超過寫代碼的時(shí)間。而且毫無(wú)靈活性,能夠?qū)崿F(xiàn)什么效果全依靠UI人好不好,你和他喝酒的時(shí)候杯子夠不夠底下。
不管是為了性能還是為了擴(kuò)展,使用CSS來(lái)實(shí)現(xiàn)顯然是更好選擇。
CSS實(shí)現(xiàn)
使用anmation的steps也可以實(shí)現(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); } }
關(guān)鍵的屬性就是animation: steps
。 從命名可以看出它是一個(gè)步進(jìn)相關(guān)的東西。每一個(gè).voice-item
的寬度時(shí)80px,那么它的animation的translateX為-240px。
意思就是動(dòng)畫將.voice-box
節(jié)點(diǎn)從0px位置右移到-240px的位置,steps的動(dòng)畫效果,讓它移動(dòng)三次完結(jié),一次移動(dòng)80px。
畫一個(gè)圖來(lái)表示這個(gè)過程:
可以說(shuō)和輪播圖一摸一樣。
最后給voice-item
節(jié)點(diǎn)中添加樣式:
<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; }
最終實(shí)現(xiàn)效果如下:
以上就是JS實(shí)現(xiàn)微信播音效果示例詳解的詳細(xì)內(nèi)容,更多關(guān)于JS微信播音效果的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
arcgis?js完整懸停效果實(shí)現(xiàn)demo
這篇文章主要為大家介紹了arcgis?js完整懸停效果實(shí)現(xiàn)demo詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Svelte調(diào)試模式j(luò)s級(jí)別差異和細(xì)化后的體積差異詳解
這篇文章主要為大家介紹了Svelte調(diào)試模式j(luò)s級(jí)別差異和細(xì)化后的體積差異詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12漂亮的仿flash菜單,來(lái)自藍(lán)色經(jīng)典
漂亮的仿flash菜單,來(lái)自藍(lán)色經(jīng)典...2006-06-06JS網(wǎng)頁(yè)repaint與reflow?的區(qū)別及優(yōu)化方式
這篇文章主要為大家介紹了JS網(wǎng)頁(yè)repaint與reflow?的區(qū)別及優(yōu)化方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09tree?shaking對(duì)打包體積優(yōu)化及作用
這篇文章主要為大家介紹了tree?shaking對(duì)打包體積優(yōu)化及作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07微信小程序 scroll-view組件實(shí)現(xiàn)列表頁(yè)實(shí)例代碼
這篇文章主要介紹了微信小程序 scroll-view組件實(shí)現(xiàn)列表頁(yè)實(shí)例代碼的相關(guān)資料,scroll-view組件介紹scroll-view是微信小程序提供的可滾動(dòng)視圖組件,其主要作用是可以用來(lái)做手機(jī)端經(jīng)常會(huì)看到的上拉加載 ,需要的朋友可以參考下2016-12-12