您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“vue怎么實(shí)現(xiàn)數(shù)字動(dòng)態(tài)翻牌器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“vue怎么實(shí)現(xiàn)數(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, }, // 顯示的長(zhǎng)度 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):
至于樣式背景可以自定義
到此,相信大家對(duì)“vue怎么實(shí)現(xiàn)數(shù)字動(dòng)態(tài)翻牌器”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。