vue實現(xiàn)數(shù)字動態(tài)翻牌器
數(shù)字動態(tài)翻牌器
最近項目里使用到了數(shù)字翻牌器,于是自己寫了一個,動態(tài)的翻牌器
第一步創(chuàng)建一個組件頁面,NumberCount.vue
思路:大概就是顯示幾位數(shù),然后從0開始滾動到當(dāng)前的數(shù)值的位置,在每一個位置都有0-9的數(shù),然后就是往上滾動當(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è)置文字滾動 ? ? setNumberTransform() { ? ? ? const numberItems = this.$refs.numberItem; // 拿到數(shù)字的ref,計算元素數(shù)量 ? ? ? // eslint-disable-next-line no-restricted-globals ? ? ? const numberArr = this.orderNum.filter(item => !isNaN(item)); ? ? ? console.log(numberItems.length, numberArr); ? ? ? // 結(jié)合CSS 對數(shù)字字符進行滾動,顯示數(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"補位 ? ? ? ? ? this.toOrderNum(numlist); // 遞歸添加"0"補位 ? ? ? ? } else if (numtext.length === num.length) { ? ? ? ? ? this.orderNum = numtext.split(''); // 將其便變成數(shù)據(jù),渲染至滾動數(shù)組 ? ? ? ? } ? ? ? } else { ? ? ? ? this.orderNum = numtext.split(''); ? ? ? } ? ? ? // 數(shù)字中加入逗號 ? ? ? // 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"> /*總量滾動數(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)逗號設(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; ? } } /*滾動數(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>
不加逗號:
加入逗號:
至于樣式背景可以自定義
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Element的el-tree控件后臺數(shù)據(jù)結(jié)構(gòu)的生成以及方法的抽取
這篇文章主要介紹了Element的el-tree控件后臺數(shù)據(jù)結(jié)構(gòu)的生成以及方法的抽取,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Vue實現(xiàn)Excel預(yù)覽功能使用場景示例詳解
這篇文章主要為大家介紹了Vue實現(xiàn)Excel預(yù)覽功能使用場景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Vue項目打包并部署nginx服務(wù)器的詳細(xì)步驟
vue項目開發(fā)好之后需要部署到服務(wù)器上進行外網(wǎng)訪問,下面這篇文章主要給大家介紹了關(guān)于Vue項目打包并部署nginx服務(wù)器的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Vue.js 實現(xiàn)地址管理頁面思路詳解(地址添加、編輯、刪除和設(shè)置默認(rèn)地址)
這篇文章主要介紹了Vue.js 實現(xiàn)地址管理頁面的思路(地址添加、編輯、刪除和設(shè)置默認(rèn)地址),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12