Vue.js實(shí)現(xiàn)大屏數(shù)字滾動(dòng)翻轉(zhuǎn)效果
大屏數(shù)字滾動(dòng)翻轉(zhuǎn)效果來(lái)源于最近工作中element后臺(tái)管理頁(yè)面一張大屏的UI圖,該UI圖上有一個(gè)模塊需要有數(shù)字往上翻動(dòng)的效果,以下是最終實(shí)現(xiàn)的效果:
整體思路:
在實(shí)現(xiàn)此效果之前,我們先來(lái)捋一下思路,用思維導(dǎo)圖來(lái)設(shè)計(jì)一下我們的實(shí)現(xiàn)步驟,如下:
你可以審查元素,下載數(shù)字背景圖片,復(fù)制圖片地址,或者使用其他背景圖片、背景顏色
有了以上的設(shè)計(jì)流程,我們先來(lái)簡(jiǎn)單實(shí)現(xiàn)一下:
// CSS代碼 <style> .box-item { position: relative; display: inline-block; width: 54px; height: 82px; /* 背景圖片 */ background: url(./number-bg.png) no-repeat center center; background-size: 100% 100%; font-size: 62px; line-height: 82px; text-align: center; } </style> // htm代碼 <div class="box"> <p class="box-item"> <span>1</span> </p> </div>
實(shí)現(xiàn)以上代碼后,它的效果將是下面這樣的:
思考:
背景框中有了數(shù)字以后,我們現(xiàn)在來(lái)思考一下,背景框中的文字,一定是0-9
之前的數(shù)字,要在不打亂以上html
結(jié)構(gòu)的前提下,如何讓數(shù)字滾動(dòng)起來(lái)呢?這個(gè)時(shí)候我們的魔爪就伸向了一個(gè)CSS
屬性:writing-mode
,下面是它屬性的介紹:
- horizontal-tb:默認(rèn)值,表示水平排版,從上到下。
- vertical-lr:表示垂直排版,從左到右。
- vertical-rl:表示垂直排版,從右到左。
它的實(shí)時(shí)效果是像下面這樣:
根據(jù)以上的靈感,我們可以實(shí)現(xiàn)下面這樣的效果:
代碼如下:
// html部分 <p class="box-item"> <span>0123456789</span> </p> // style部分 .box-item { display: inline-block; width: 54px; height: 82px; background: url(./number-bg.png) no-repeat center center; background-size: 100% 100%; font-size: 62px; line-height: 82px; text-align: center; position: relative; writing-mode: vertical-lr; text-orientation: upright; /* overflow: hidden; */ } .box-item span { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); letter-spacing: 10px; }
計(jì)算滾動(dòng)
如果我們想讓數(shù)字滾動(dòng)到5
,那么滾動(dòng)的具體到底是多少?
答案是:向下滾動(dòng)-50%
那么其他的數(shù)字呢?
得益于我們特殊的實(shí)現(xiàn)方法,每一位數(shù)字的滾動(dòng)距離有一個(gè)通用的公式:
transform: `translate(-50%,-${number * 10}%)
有了以上公式,我們讓數(shù)字滾動(dòng)到5
,它的效果如下:
代碼加上 `transform: `translate(-50%,-${number * 10}%)`,示例如下
.box-item span { position: absolute; top: 10px; left: 50%; transform: translate(-50%,-50%); letter-spacing: 10px; }
滾動(dòng)動(dòng)畫(huà)的實(shí)現(xiàn)
在知道了每一個(gè)數(shù)字具體的滾動(dòng)距離后,我們來(lái)設(shè)計(jì)一下,讓數(shù)字能夠隨機(jī)滾動(dòng)起來(lái):
一下是讓數(shù)字隨機(jī)滾動(dòng)的JS
setInterval(() => { let number = document.getElementById('Number') let random = getRandomNumber(0,10) number.style.transform = `translate(-50%, -${random * 10}%)` }, 2000) function getRandomNumber (min, max) { return Math.floor(Math.random() * (max - min + 1) + min) }
至此,我們數(shù)字滾動(dòng)效果已經(jīng)初步實(shí)現(xiàn)了,在下一節(jié)中我們將會(huì)逐步完善此效果,以滿(mǎn)足業(yè)務(wù)需求。
完善
在上一節(jié)中,我們初步完成了滾動(dòng)的效果,這一節(jié)我們將根據(jù)最開(kāi)始的思維導(dǎo)圖來(lái)設(shè)計(jì)一個(gè)通用的Vue
業(yè)務(wù)組件
因?yàn)槲覀兊臉I(yè)務(wù)需要,我們最大的位數(shù)是8
位數(shù)字,所以對(duì)不足八位進(jìn)行補(bǔ)位:
假如傳遞的位數(shù)不足8
位,我們需要對(duì)它進(jìn)行補(bǔ)0
的操作,補(bǔ)0
完成以后,我們也需要把它轉(zhuǎn)換成金額的格式
toOrderNum(num) { num = num.toString() // 把訂單數(shù)變成字符串 if (num.length < 8) { num = '0' + num // 如未滿(mǎn)八位數(shù),添加"0"補(bǔ)位 this.toOrderNum(num) // 遞歸添加"0"補(bǔ)位 } else if (num.length === 8) { // 訂單數(shù)中加入逗號(hào) num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8) this.orderNum = num.split('') // 將其便變成數(shù)據(jù),渲染至滾動(dòng)數(shù)組 } else { // 訂單總量數(shù)字超過(guò)八位顯示異常 this.$message.warning('訂單總量數(shù)字過(guò)大,顯示異常,請(qǐng)聯(lián)系客服') } },
渲染
我們根據(jù)上面補(bǔ)位字符串,分隔成字符數(shù)組,在頁(yè)面中進(jìn)行渲染:
computeNumber:為字符數(shù)組,例如:['0','0',',','0','0','0',',','9','1','7']
// html代碼 <ul> <li :class="{'number-item': !isNaN(item) }" v-for="(item,index) in computeNumber" :key="index" > <span v-if="!isNaN(item)"> <i ref="numberItem">0123456789</i> </span> <span v-else>{{item}}</span> </li> </ul> // CSS代碼 .number-item { width: 50px; background: url(./number-bg.png) no-repeat center center; background-size:100% 100%; & > span { position: relative; display: inline-block; margin-right: 10px; width: 100%; height: 100%; writing-mode: vertical-rl; text-orientation: upright; overflow: hidden; & > i { position: absolute; top: 0; left: 50%; transform: translate(-50%,0); transition: transform 0.5s ease-in-out; letter-spacing: 10px; } } }
頁(yè)面渲染效果:
數(shù)字隨機(jī)增長(zhǎng),模擬輪詢(xún)效果
頁(yè)面渲染完畢后,我們來(lái)讓數(shù)字滾動(dòng)起來(lái),設(shè)計(jì)如下兩個(gè)方法,其中increaseNumber
需要在Vue
生命周期mounted
函數(shù)中調(diào)用
// 定時(shí)增長(zhǎng)數(shù)字 increaseNumber () { let self = this this.timer = setInterval(() => { self.newNumber = self.newNumber + getRandomNumber(1, 100) self.setNumberTransform() }, 3000) }, // 設(shè)置每一位數(shù)字的偏移 setNumberTransform () { let numberItems = this.$refs.numberItem let numberArr = this.computeNumber.filter(item => !isNaN(item)) for (let index = 0; index < numberItems.length; index++) { let elem = numberItems[index] elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)` } }
最終實(shí)現(xiàn)效果:
完整代碼如下:
<template> <div class="chartNum"> <h3 class="orderTitle">訂單總量</h3> <div class="box-item"> <li :class="{'number-item': !isNaN(item), 'mark-item': isNaN(item) }" v-for="(item,index) in orderNum" :key="index"> <span v-if="!isNaN(item)"> <i ref="numberItem">0123456789</i> </span> <span class="comma" v-else>{{item}}</span> </li> </div> </div> </template> <script> export default { data() { return { orderNum: ['0', '0', ',', '0', '0', '0', ',', '0', '0', '0'], // 默認(rèn)訂單總數(shù) } } mounted: { this.toOrderNum(num) // 這里輸入數(shù)字即可調(diào)用 }, methods: { // 設(shè)置文字滾動(dòng) setNumberTransform () { const numberItems = this.$refs.numberItem // 拿到數(shù)字的ref,計(jì)算元素?cái)?shù)量 const numberArr = this.orderNum.filter(item => !isNaN(item)) // 結(jié)合CSS 對(duì)數(shù)字字符進(jìn)行滾動(dòng),顯示訂單數(shù)量 for (let index = 0; index < numberItems.length; index++) { const elem = numberItems[index] elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)` } }, // 處理總訂單數(shù)字 toOrderNum(num) { num = num.toString() // 把訂單數(shù)變成字符串 if (num.length < 8) { num = '0' + num // 如未滿(mǎn)八位數(shù),添加"0"補(bǔ)位 this.toOrderNum(num) // 遞歸添加"0"補(bǔ)位 } else if (num.length === 8) { // 訂單數(shù)中加入逗號(hào) num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8) this.orderNum = num.split('') // 將其便變成數(shù)據(jù),渲染至滾動(dòng)數(shù)組 } else { // 訂單總量數(shù)字超過(guò)八位顯示異常 this.$message.warning('訂單總量數(shù)字過(guò)大,顯示異常,請(qǐng)聯(lián)系客服') } }, } } </script> <style scoped lang='scss'> /*訂單總量滾動(dòng)數(shù)字設(shè)置*/ .box-item { position: relative; height: 100px; font-size: 54px; line-height: 41px; text-align: center; list-style: none; color: #2D7CFF; writing-mode: vertical-lr; text-orientation: upright; /*文字禁止編輯*/ -moz-user-select: none; /*火狐*/ -webkit-user-select: none; /*webkit瀏覽器*/ -ms-user-select: none; /*IE10*/ -khtml-user-select: none; /*早期瀏覽器*/ user-select: none; /* overflow: hidden; */ } /* 默認(rèn)逗號(hào)設(shè)置 */ .mark-item { width: 10px; height: 100px; margin-right: 5px; line-height: 10px; font-size: 48px; position: relative; & > span { position: absolute; width: 100%; bottom: 0; writing-mode: vertical-rl; text-orientation: upright; } } /*滾動(dòng)數(shù)字設(shè)置*/ .number-item { width: 41px; height: 75px; background: #ccc; list-style: none; margin-right: 5px; background:rgba(250,250,250,1); border-radius:4px; border:1px solid rgba(221,221,221,1); & > span { position: relative; display: inline-block; margin-right: 10px; width: 100%; height: 100%; writing-mode: vertical-rl; text-orientation: upright; overflow: hidden; & > i { font-style: normal; position: absolute; top: 11px; left: 50%; transform: translate(-50%,0); transition: transform 1s ease-in-out; letter-spacing: 10px; } } } .number-item:last-child { margin-right: 0; } </style>
總結(jié)
以上所述是小編給大家介紹的Vue.js實(shí)現(xiàn)大屏數(shù)字滾動(dòng)翻轉(zhuǎn)效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Vue3+TS實(shí)現(xiàn)數(shù)字滾動(dòng)效果CountTo組件
- Vue?transition組件簡(jiǎn)單實(shí)現(xiàn)數(shù)字滾動(dòng)
- vue3數(shù)據(jù)可視化實(shí)現(xiàn)數(shù)字滾動(dòng)特效代碼
- vue3實(shí)現(xiàn)數(shù)字滾動(dòng)特效實(shí)例詳解
- Vue組件實(shí)現(xiàn)數(shù)字滾動(dòng)抽獎(jiǎng)效果
- vue實(shí)現(xiàn)數(shù)字滾動(dòng)效果
- 基于vue實(shí)現(xiàn)滾動(dòng)條滾動(dòng)到指定位置對(duì)應(yīng)位置數(shù)字進(jìn)行tween特效
- vue 實(shí)現(xiàn)數(shù)字滾動(dòng)增加效果的實(shí)例代碼
- Vue金融數(shù)字格式化(并保留小數(shù))數(shù)字滾動(dòng)效果實(shí)現(xiàn)
相關(guān)文章
Vue開(kāi)發(fā)之封裝分頁(yè)組件與使用示例
這篇文章主要介紹了Vue開(kāi)發(fā)之封裝分頁(yè)組件與使用,結(jié)合實(shí)例形式分析了vue.js封裝分頁(yè)組件操作以及使用組件進(jìn)行分頁(yè)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04Electron采集桌面共享和系統(tǒng)音頻(桌面捕獲)實(shí)例
這篇文章主要為大家介紹了Electron采集桌面共享和系統(tǒng)音頻(桌面捕獲)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10vue使用less報(bào)錯(cuò):Inline JavaScript is not ena
這篇文章主要介紹了vue使用less報(bào)錯(cuò):Inline JavaScript is not enabled問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01vue子組件通過(guò).sync修飾符修改props屬性方式
這篇文章主要介紹了vue子組件通過(guò).sync修飾符修改props屬性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue實(shí)現(xiàn)從外部修改組件內(nèi)部的變量的值
這篇文章主要介紹了vue實(shí)現(xiàn)從外部修改組件內(nèi)部的變量的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue 項(xiàng)目打包時(shí)樣式及背景圖片路徑找不到的解決方式
今天小編就為大家分享一篇vue 項(xiàng)目打包時(shí)樣式及背景圖片路徑找不到的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue路徑寫(xiě)法之關(guān)于./和@/的區(qū)別
這篇文章主要介紹了vue路徑寫(xiě)法之關(guān)于./和@/的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09