【组件】翻牌器效果

目录

效果

组件代码

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>

背景素材

相关推荐
我不吃饼干7 小时前
在 React 中实现倒计时功能会有什么坑
前端·react.js
小小小小宇7 小时前
前端PerformanceObserver
前端
王者鳜錸7 小时前
PYTHON从入门到实践-18Django从零开始构建Web应用
前端·python·sqlite
拾光拾趣录7 小时前
ES6到HTTPS全链路连环拷问,99%人第3题就翻车?
前端·面试
haaaaaaarry8 小时前
Element Plus常见基础组件(二)
开发语言·前端·javascript
xyphf_和派孔明8 小时前
关于echarts的性能优化考虑
前端·性能优化·echarts
PyHaVolask9 小时前
HTML 表单进阶:用户体验优化与实战应用
前端·javascript·html·用户体验
A了LONE9 小时前
cv弹窗,退款确认弹窗
java·服务器·前端
AntBlack9 小时前
闲谈 :AI 生成视频哪家强 ,掘友们有没有推荐的工具?
前端·后端·aigc
花菜会噎住10 小时前
Vue3核心语法进阶(computed与监听)
前端·javascript·vue.js