vue實(shí)現(xiàn)數(shù)字動(dòng)態(tài)翻牌器
數(shù)字動(dòng)態(tài)翻牌器
最近項(xiàng)目里使用到了數(shù)字翻牌器,于是自己寫了一個(gè),動(dòng)態(tài)的翻牌器
第一步創(chuàng)建一個(gè)組件頁面,NumberCount.vue
思路:大概就是顯示幾位數(shù),然后從0開始滾動(dòng)到當(dāng)前的數(shù)值的位置,在每一個(gè)位置都有0-9的數(shù),然后就是往上滾動(dòng)當(dāng)前數(shù)值的次數(shù)到當(dāng)前的數(shù),話不多說上代碼
<template> ? <div class="chartNum"> ? ? <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 { ? props: { ? ? // 顯示的數(shù)字 ? ? number: { ? ? ? type: Number, ? ? }, ? ? // 顯示的長度 ? ? length: { ? ? ? type: Number, ? ? }, ? }, ? data() { ? ? return { ? ? ? orderNum: ['0', '0', '0', '0', '0', '0', '0', '0'], // 默認(rèn)總數(shù) ? ? }; ? }, ? mounted() { ? ? setTimeout(() => { ? ? ? this.toOrderNum(this.number); // 這里輸入數(shù)字即可調(diào)用 ? ? }, 500); ? }, ? watch: { ? ? number: { ? ? ? handler(val) { ? ? ? ? this.toOrderNum(val); ? ? ? }, ? ? ? deep: true, ? ? }, ? }, ? methods: { ? ? // 設(shè)置文字滾動(dòng) ? ? setNumberTransform() { ? ? ? const numberItems = this.$refs.numberItem; // 拿到數(shù)字的ref,計(jì)算元素?cái)?shù)量 ? ? ? // eslint-disable-next-line no-restricted-globals ? ? ? const numberArr = this.orderNum.filter(item => !isNaN(item)); ? ? ? console.log(numberItems.length, numberArr); ? ? ? // 結(jié)合CSS 對(duì)數(shù)字字符進(jìn)行滾動(dòng),顯示數(shù)量 ? ? ? for (let index = 0; index < numberItems.length; index += 1) { ? ? ? ? const elem = numberItems[index]; ? ? ? ? elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`; ? ? ? } ? ? }, ? ? // 處理總數(shù)字 ? ? toOrderNum(num) { ? ? ? const numtext = num.toString(); ? ? ? if (this.length) { ? ? ? ? if (numtext.length < this.length) { ? ? ? ? ? const numlist = `0${numtext}`; // 如未滿固定數(shù),添加"0"補(bǔ)位 ? ? ? ? ? this.toOrderNum(numlist); // 遞歸添加"0"補(bǔ)位 ? ? ? ? } else if (numtext.length === num.length) { ? ? ? ? ? this.orderNum = numtext.split(''); // 將其便變成數(shù)據(jù),渲染至滾動(dòng)數(shù)組 ? ? ? ? } ? ? ? } else { ? ? ? ? this.orderNum = numtext.split(''); ? ? ? } ? ? ? // 數(shù)字中加入逗號(hào) ? ? ? // const length = numtext.length / 3; ? ? ? // let count = ''; ? ? ? // for (let i = 0; i < length; i += 1) { ? ? ? // ? if (i === 0) { ? ? ? // ? ? count += `${numtext.slice(i, i + 3)},`; ? ? ? // ? ? console.log(count); ? ? ? // ? } else if (i === length - 1) { ? ? ? // ? ? count += numtext.slice(i * 3, i * 3 + 3); ? ? ? // ? ? console.log(count); ? ? ? // ? } else { ? ? ? // ? ? count += `${numtext.slice(i * 3, i * 3 + 3)},`; ? ? ? // ? ? console.log(count); ? ? ? // ? } ? ? ? // } ? ? ? // this.orderNum = count.split(''); ? ? ? this.setNumberTransform(); ? ? }, ? }, }; </script> <style scoped lang="scss"> /*總量滾動(dòng)數(shù)字設(shè)置*/ .box-item { ? position: relative; ? height: 34px; ? font-size: 20px; ? font-family: AzonixRegular; ? color: #021c25; ? line-height: 41px; ? text-align: center; ? list-style: none; ? writing-mode: vertical-lr; ? text-orientation: upright; } /* 默認(rèn)逗號(hào)設(shè)置 */ .mark-item { ? width: 28px; ? height: 34px; ? position: relative; ? /* 背景圖片 */ ? background: url('~@/assets/images/overview/bg-chartNum.svg') no-repeat center ? ? center; ? background-size: 100% 100%; ? list-style: none; ? margin-right: 1px; ? & > span { ? ? position: absolute; ? ? width: 100%; ? ? height: 100%; ? ? bottom: 2px; ? ? left: 20%; ? ? font-size: 20px; ? ? writing-mode: vertical-rl; ? ? text-orientation: upright; ? } } /*滾動(dòng)數(shù)字設(shè)置*/ .number-item { ? width: 28px; ? height: 34px; ? /* 背景圖片 */ ? background: url('~@/assets/images/overview/bg-chartNum.svg') no-repeat center ? ? center; ? background-size: 100% 100%; ? // background: #ccc; ? list-style: none; ? margin-right: 1px; ? & > span { ? ? position: relative; ? ? display: inline-block; ? ? margin-right: 10px; ? ? width: 100%; ? ? height: 100%; ? ? writing-mode: vertical-rl; ? ? text-orientation: upright; ? ? overflow: hidden; ? ? display: flex; ? ? align-items: center; ? ? justify-content: center; ? ? & > i { ? ? ? font-style: normal; ? ? ? position: absolute; ? ? ? top: 8px; ? ? ? left: 50%; ? ? ? transform: translate(-50%, 0); ? ? ? transition: transform 1s ease-in-out; ? ? ? letter-spacing: 10px; ? ? } ? } } .number-item:last-child { ? margin-right: 0; } </style>
不加逗號(hào):
加入逗號(hào):
至于樣式背景可以自定義
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Element的el-tree控件后臺(tái)數(shù)據(jù)結(jié)構(gòu)的生成以及方法的抽取
這篇文章主要介紹了Element的el-tree控件后臺(tái)數(shù)據(jù)結(jié)構(gòu)的生成以及方法的抽取,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Vue實(shí)現(xiàn)Excel預(yù)覽功能使用場(chǎng)景示例詳解
這篇文章主要為大家介紹了Vue實(shí)現(xiàn)Excel預(yù)覽功能使用場(chǎng)景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Vue項(xiàng)目打包并部署nginx服務(wù)器的詳細(xì)步驟
vue項(xiàng)目開發(fā)好之后需要部署到服務(wù)器上進(jìn)行外網(wǎng)訪問,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目打包并部署nginx服務(wù)器的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Vue項(xiàng)目中引入外部文件的方法(css、js、less)
本篇文章主要介紹了Vue項(xiàng)目中引入外部文件的方法(css、js、less),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07vue-cli 使用vue-bus來全局控制的實(shí)例講解
今天小編就為大家分享一篇 vue-cli使用vue-bus來全局控制的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue v-for直接循環(huán)數(shù)字實(shí)例
今天小編就為大家分享一篇vue v-for直接循環(huán)數(shù)字實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue.js 實(shí)現(xiàn)地址管理頁面思路詳解(地址添加、編輯、刪除和設(shè)置默認(rèn)地址)
這篇文章主要介紹了Vue.js 實(shí)現(xiàn)地址管理頁面的思路(地址添加、編輯、刪除和設(shè)置默認(rèn)地址),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12