利用Vue實(shí)現(xiàn)一個(gè)累加向上漂浮動(dòng)畫
前言
在不久之前,看到一個(gè)比較有意思的小程序,就是靜神木魚,可以實(shí)現(xiàn)在線敲木魚,自動(dòng)敲木魚,手盤佛珠,靜心頌缽的
整個(gè)小程序功能比較小巧,大道至簡(jiǎn),曾風(fēng)靡過一陣的,無論在App應(yīng)用市場(chǎng)上,還是小程序里,一些開發(fā)者都賺得盆滿缽滿,用于緩解當(dāng)代年輕人的一個(gè)焦慮,佛系解壓,算是一個(gè)娛樂,你可能覺得這種應(yīng)用沒有什么卵用,離商業(yè)很遙遠(yuǎn),但恰恰相反,在商業(yè)拓展延伸上,依舊有人愿意付費(fèi)使用
下面就來揭秘實(shí)現(xiàn)一下這個(gè)小程序中敲木魚的示例的
具體代碼
<template> <div class="leijia-wrap"> <div class="leijia-content"> <h2>靜神木魚</h2> <div class="text">{{count}}<span class="animatetip" v-show="isTip">功德+1</span></div> <div class="btn"> <el-button type="primary" size="mini" @click="handleClick" ref="btnClick">敲擊</el-button> <el-button type="primary" size="mini" @click="handleVoince" ref="btnJinYin">{{isVoince == true?'非靜音':'靜音'}}</el-button> <el-button type="danger" size="mini" @click="handleAuto">{{onOff == true?'自動(dòng)':'非自動(dòng)'}}</el-button> <el-button type="info" size="mini" @click="handleClear">清除</el-button> </div> </div> <!--敲擊音頻---> <audio id="myaudio" src="../images/js-article-imgs/video/qiaoji.mp3" style="display:none"> Your browser does not support the audio element. </audio> </div> </template> <script> export default { data() { return { count: 0, timer: null, onOff: true, isVoince: true, isTip: false } }, mounted(){ this.count = localStorage.getItem('count') || 0; // 獲取本地存儲(chǔ)localStorage }, methods: { // 敲擊 handleClick() { this.count = parseInt(this.count)+1; localStorage.setItem('count',this.count); // 設(shè)置本地存儲(chǔ)localStorage this.isTip = true; setTimeout(()=> { this.isTip = false; }, 300); let music = document.getElementById("myaudio"); if(this.isVoince) { music.play(); }else { music.pause(); } }, // 控制靜音還是非靜音 handleVoince() { let music = document.getElementById("myaudio"); if(this.isVoince) { music.play(); }else { music.pause(); } this.isVoince = !this.isVoince; }, // 重置數(shù)據(jù),清除localStorage handleClear(){ localStorage.removeItem('count'); this.count = 0; }, // 自動(dòng)敲打,累加 handleAuto() { let music = document.getElementById("myaudio"); if(this.onOff) { this.timer = setInterval(() => { this.count = parseInt(this.count)+1; this.isTip = true; setTimeout(()=> { this.isTip = false; }, 300); music.play(); },1000) }else { clearInterval(this.timer); // 清除定時(shí)器 music.pause(); } this.onOff = !this.onOff; }, } } </script> <style lang="scss" scoped> .leijia-wrap { text-align: center; margin-top: 10px; .btn { margin-top: 20px; } .text { position:relative; } .animatetip { opacity: 0; animation: showtip 1s; position:absolute; right: 320px; top: 0px; } /* 關(guān)鍵幀動(dòng)畫 */ @keyframes showtip { 0% { opacity: 1; transform: translateY(0); } 100% { opacity: 0; transform: translateY(-15px); } } } </style>
上面的一個(gè)小例子雖然很小,但是它囊括了一些知識(shí),想要實(shí)現(xiàn)這個(gè)效果,那么需要知道的
分析
- 如何實(shí)現(xiàn)控制數(shù)字的累加,并且解決字符串
+
拼接的問題(具體解決使用parseInt()
即可) - 如何實(shí)現(xiàn)自動(dòng)的累加,需要知道設(shè)置定時(shí)器
setInterval
,以及清除定時(shí)器clearInterval
- 一個(gè)控件控制元素狀態(tài)的變化,開關(guān)的設(shè)置
- 在刷新頁面時(shí),下次進(jìn)來仍然保留上一次的狀態(tài),則需要使用本地緩存
localStorage
,以及清除指定的本地緩存localStorage.removeItem('key')
- 如何控制音頻
audio
元素的播放(play
)和暫停(pause
) - 想要實(shí)現(xiàn)累加向上漂浮動(dòng)畫,則需要使用
css3
中的動(dòng)畫關(guān)鍵幀
上面控制功德加+1
,使用了vue
當(dāng)中的v-show
結(jié)合css3
的動(dòng)畫關(guān)鍵幀,以及結(jié)合透明度去控制的
需要注意的是單純的借用v-show
并不能完全控制,還需要借用一個(gè)setTimeout
,先隱藏,然后在顯示,以及過了多長(zhǎng)時(shí)間,最后讓元素隱藏
在微信小程序當(dāng)中,實(shí)現(xiàn)的邏輯也是相似的,是使用了微信的本地存儲(chǔ)功能的,動(dòng)畫的話,使用了小程序自帶的動(dòng)畫API就可以實(shí)現(xiàn)的
到此這篇關(guān)于利用Vue實(shí)現(xiàn)一個(gè)累加向上漂浮動(dòng)畫的文章就介紹到這了,更多相關(guān)Vue累加向上漂浮動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp模仿微信實(shí)現(xiàn)聊天界面的示例代碼
這篇文章主要介紹了如何利用uniapp模仿微信,實(shí)現(xiàn)一個(gè)聊天界面。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,感興趣的可以了解一下2022-01-01Vue按時(shí)間段查詢數(shù)據(jù)組件使用詳解
這篇文章主要為大家詳細(xì)介紹了Vue按時(shí)間段查詢數(shù)據(jù)組件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08vite創(chuàng)建vue3項(xiàng)目頁面引用public下js文件失敗解決辦法
Vue3相較于之前的版本有了不少變化,如引用全局Js文件,這篇文章主要給大家介紹了關(guān)于vite創(chuàng)建vue3項(xiàng)目頁面引用public下js文件失敗的解決辦法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Vue Vine實(shí)現(xiàn)一個(gè)文件中寫多個(gè)組件的方法(最近很火)
Vue Vine提供了全新Vue組件書寫方式,主要的賣點(diǎn)是可以在一個(gè)文件里面寫多個(gè)vue組件,Vue Vine是一個(gè)vite插件,vite解析每個(gè)模塊時(shí)都會(huì)觸發(fā)插件的transform鉤子函數(shù),本文介紹Vue Vine是如何實(shí)現(xiàn)一個(gè)文件中寫多個(gè)組件,感興趣的朋友一起看看吧2024-07-07vue中使用echarts繪制雙Y軸圖表時(shí)刻度沒有對(duì)齊的兩種解決方法(最新方案)
這篇文章主要介紹了vue中使用echarts繪制雙Y軸圖表時(shí),刻度沒有對(duì)齊的兩種解決方法,主要原因是因?yàn)榭潭仍陲@示時(shí),分割段數(shù)不一樣,導(dǎo)致左右的刻度線不一致,不能重合在一起,下面給大家分享解決方法,需要的朋友可以參考下2024-03-03