【组件】翻牌器效果

目录

效果

组件代码

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>

背景素材

相关推荐
未来之窗软件服务19 分钟前
未来之窗昭和仙君(六十五)Vue与跨地区多部门开发—东方仙盟练气
前端·javascript·vue.js·仙盟创梦ide·东方仙盟·昭和仙君
baidu_2474386120 分钟前
Android ViewModel定时任务
android·开发语言·javascript
嘿起屁儿整32 分钟前
面试点(网络层面)
前端·网络
VT.馒头39 分钟前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
有位神秘人1 小时前
Android中Notification的使用详解
android·java·javascript
phltxy2 小时前
Vue 核心特性实战指南:指令、样式绑定、计算属性与侦听器
前端·javascript·vue.js
Byron07073 小时前
Vue 中使用 Tiptap 富文本编辑器的完整指南
前端·javascript·vue.js
css趣多多3 小时前
地图快速上手
前端
zhengfei6113 小时前
面向攻击性安全专业人员的一体化浏览器扩展程序[特殊字符]
前端·chrome·safari
码丁_1173 小时前
为什么前端需要做优化?
前端