vue實(shí)現(xiàn)翻牌動(dòng)畫
本文實(shí)例為大家分享了vue實(shí)現(xiàn)翻牌動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下

應(yīng)用場(chǎng)景
常用于大屏訂單數(shù)量展示
原理
- 利用css writing-mode: vertical-rl 使數(shù)字垂直排列
- 利用css transform 使數(shù)字滾動(dòng)
實(shí)現(xiàn)思路
- 根據(jù)css先讓數(shù)字垂直排列,總共設(shè)置8列
- 根據(jù)組件傳遞的數(shù)值,如果不滿8位,遞歸補(bǔ)零
- 補(bǔ)零之后,循環(huán)根據(jù) translate(-50%, -${numberArr[index] * 10}%,查看動(dòng)畫
<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 {
? ? ? ? name: "hChartNum",
? ? ? ? props:{
? ? ? ? ? num:{
? ? ? ? ? ? ? type:Number,
? ? ? ? ? ? ? default:1123
? ? ? ? ? }
? ? ? ? },
? ? ? ? data() {
? ? ? ? ? ? return {
? ? ? ? ? ? ? ? orderNum: ['0', '0', '0', '0', '0', '0', '0', '0'], // 默認(rèn)訂單總數(shù)
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? mounted(){
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? this.toOrderNum(this.num) // 這里輸入數(shù)字即可調(diào)用
? ? ? ? ? ? }, 500);
? ? ? ? },
? ? ? ? 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,讓文字滾動(dòng)起來(lái)
? ? ? ? ? ? ? ? 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();
? ? ? ? ? ? ? ? if(num.length < 8){
? ? ? ? ? ? ? ? ? ? num = '0' + num; // 未滿8位,補(bǔ)零
? ? ? ? ? ? ? ? ? ? this.toOrderNum(num); // 遞歸添加"0"補(bǔ)位
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(num.length == 8){
? ? ? ? ? ? ? ? ? ? this.orderNum = num.split('')
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else{
? ? ? ? ? ? ? ? ? ? // 數(shù)據(jù)量超過(guò)8位
? ? ? ? ? ? ? ? ? ? this.$message.error('數(shù)據(jù)量過(guò)大');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? this.setNumberTransform();
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>
<style scoped lang="less">
? ? .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;
? ? }
? ? /*默認(rèn)逗號(hào)設(shè)置*/
? ? .number-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: url(./img/bg.jpg) no-repeat center center;
? ? ? ? background-size: 100% 100%;
? ? ? ? list-style: none;
? ? ? ? margin-right: 5px;
? ? ? ? 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>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳細(xì)講解如何創(chuàng)建, 發(fā)布自己的 Vue UI 組件庫(kù)
當(dāng)我們自己開發(fā)了一個(gè) _UI Component_, 需要在多個(gè)項(xiàng)目中使用的時(shí)候呢? 我們首先想到的可能是直接復(fù)制一份過(guò)去對(duì)嗎?我們?yōu)槭裁床话l(fā)布一個(gè) UI 組件庫(kù)給自己用呢?下面小編和大家來(lái)一起學(xué)習(xí)下吧2019-05-05
vue如何實(shí)時(shí)往數(shù)組里追加數(shù)據(jù)
這篇文章主要介紹了vue如何實(shí)時(shí)往數(shù)組里追加數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
項(xiàng)目部署后前端vue代理失效問(wèn)題解決辦法
這篇文章主要給大家介紹了關(guān)于項(xiàng)目部署后前端vue代理失效問(wèn)題的解決辦法,文中通過(guò)圖文以及代碼示例將解決的辦法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02
Vite?vue3多頁(yè)面入口打包以及部署踩坑實(shí)戰(zhàn)
因?yàn)槲覀児镜捻?xiàng)目是多頁(yè)面應(yīng)用,不同于傳統(tǒng)單頁(yè)面應(yīng)用,一個(gè)包就可以了,下面這篇文章主要給大家介紹了關(guān)于Vite?vue3多頁(yè)面入口打包以及部署踩坑的相關(guān)資料,需要的朋友可以參考下2022-05-05
vue路由跳轉(zhuǎn)到新頁(yè)面實(shí)現(xiàn)置頂
這篇文章主要介紹了vue路由跳轉(zhuǎn)到新頁(yè)面實(shí)現(xiàn)置頂問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue3組合式API中使用forwardRef()函數(shù)
本文主要介紹了Vue3組合式API中使用forwardRef()函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

