【组件】翻牌器效果

目录

效果

组件代码

html 复制代码
<template>
  <div
    class="card-flop"
    :style="{
      height: typeof height === 'number' ? `${height}px` : height,
      '--box-width': typeof boxWidth === 'number' ? `${boxWidth}px` : boxWidth,
      '--box-height': typeof boxHeight === 'number' ? `${boxHeight}px` : boxHeight,
      '--color': color
    }"
  >
    <div
      :class="{ 'card-flop__number': true, 'is-comma': isNotNumber(number) }"
      v-for="(number, index) in countArray"
      :key="`num-${index}`"
    >
      <div v-if="isNotNumber(number)" class="card-flop__number-comma">
        <span>{{ number }}</span>
      </div>
      <div v-else class="card-flop__number-box" :style="{ transform: getTransform(number, index) }">
        <div v-for="(item, index) in numbers" class="card-flop__number-item" :key="`item-${index}`">
          <span>{{ item }}</span>
        </div>
      </div>
    </div>
    <div class="card-flop__unit">
      <span>{{ unit }}</span>
    </div>
  </div>
</template>

<script >
export default {
  name: 'CardFlopCom',
  components: {},
  props: {
    count: {
      type: [String, Number],
      default: 0
    },
    height: {
      type: Number,
      default: 32
    },
    boxWidth: {
      type: Number,
      default: 24
    },
    boxHeight: {
      type: Number,
      default: 32
    },
    unit: {
      type: String,
      default: '条'
    },
    color: {
      type: String,
      default: '#fff'
    }
  },

  data() {
    return {
      numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    }
  },

  computed: {
    countArray() {
      // 将数字转换为带千位分隔符的字符串
      const formattedNumber = Number(this.count).toLocaleString()
      // 分割成数组,包括逗号
      return formattedNumber.split('')
    }
  },
  methods: {
    isNotNumber(number) {
      return [',', '.'].includes(number)
    },
    getTransform(number) {
      const offset = Number(number) * this.boxHeight
      return `translateY(-${offset}px)`
    }
  }
}
</script>

<style lang="scss" scoped>
.card-flop {
  display: flex;
  align-items: baseline;
  justify-content: center;

  &__number {
    height: var(--box-height);
    color: var(--color);

    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin-right: 3px;
    overflow: hidden;

    &:not(.is-comma) {
      width: var(--box-width);
      // background-image: url('@/assets/images/screen-zhidu/card_flop_bg.png');
      background-image: url('~@/assets/images/screen-zhidu/card_flop_bg.png');
    }

    &.is-comma {
      margin-right: 2px;
    }

    &-box {
      transition: transform 1s ease-in-out;
    }

    &-item {
      width: var(--box-width);
      height: var(--box-height);

      display: flex;
      align-items: center;
      justify-content: center;
      span {
        font-weight: bold;
        font-size: 26px;
      }
    }
  }

  &__unit {
    font-size: 16px;
    color: rgba(225, 239, 255, 0.6);
    margin-left: 2px;
  }
}
</style>

背景素材

相关推荐
爱吃山竹的大肚肚4 分钟前
Nginx 最核心和常用的命令
java·运维·服务器·前端·nginx
摘星编程33 分钟前
React Native for OpenHarmony 实战:Navigation 导航详解
javascript·react native·react.js
光影少年33 分钟前
next.js与纯react区别
前端·javascript·react.js
派大鑫wink36 分钟前
【Day31】Web 开发入门:HTTP 协议详解(请求 / 响应、状态码、请求头)
前端·网络协议·http
2501_9447114338 分钟前
理解 React 自定义 Hook:不只是“封装”,更是思维方式的转变
前端·javascript·react.js
紫小米44 分钟前
Function calling实践
java·前端·数据库
豌豆学姐1 小时前
Sora2 的使用与 API 获取调用实践(附开源前端和接入示例)
前端·开源
林恒smileZAZ1 小时前
前端 HTML 转 PDF
前端·pdf·html
摘星编程1 小时前
React Native for OpenHarmony 实战:ReactNavigation 导航库详解
javascript·react native·react.js
xiaoxue..1 小时前
Zustand 状态管理:轻量高效的 React 状态解决方案✨
前端·react.js·面试·状态模式·zustand