动态倒计时翻牌器丝滑流畅,可以支持任何倒计时以及当前时间显示

2,852 阅读4分钟

如果你想要这样的效果

image.png

或者 这样的

image.png

那就走起来 详细原理在末尾

最近更新了小程序版本和H5版本,用uniapp Vue3 写的 地址 ext.dcloud.net.cn/plugin?id=2… 有需要的自取,是免费的。 2025/3/26

你可能需要用到的 技术栈 unocss/dayjs/vue3

先封装单个卡片的孙组件

image.png

// 孙组件名字 countdownSon
<template>
  <div class="M-Flipper" :class="[flipType, { go: isFlipping }]">
    <div class="digital front" :class="_textClass(frontTextFromData)"></div>
    <div class="digital back" :class="_textClass(backTextFromData)"></div>
  </div>
</template>

<script setup>
// 定义响应式数据
const isFlipping = ref(false);
const flipType = ref('down');
const frontTextFromData = ref(0);
const backTextFromData = ref(1);

// 接收props
const props = defineProps({
  // 前牌文字
  frontText: {
    type: [Number, String],
    default: 0,
  },
  // 后牌文字
  backText: {
    type: [Number, String],
    default: 1,
  },
  // 翻牌动画时间
  duration: {
    type: Number,
    default: 600,
  },
});

// 计算属性方法
const _textClass = (number) => {
  return 'number' + number;
};

// 翻牌方法
const _flip = (type, front, back) => {
  if (isFlipping.value) return false;
  frontTextFromData.value = front;
  backTextFromData.value = back;
  flipType.value = type;
  isFlipping.value = true;
  setTimeout(() => {
    isFlipping.value = false;
    frontTextFromData.value = back;
  }, props.duration);
};

// 下翻牌方法
const flipDown = (front, back) => {
  _flip('down', front, back);
};

// 上翻牌方法
const flipUp = (front, back) => {
  _flip('up', front, back);
};

// 设置前牌文字方法
const setFront = (text) => {
  frontTextFromData.value = text;
};

// 设置后牌文字方法
const setBack = (text) => {
  backTextFromData.value = text;
};

// 在组件创建时设置初始值
onMounted(() => {
  frontTextFromData.value = props.frontText;
  backTextFromData.value = props.backText;
});

// 导出方法
defineExpose({
  flipDown,
  flipUp,
  setFront,
  setBack,
});
</script>


<style scoped>
.M-Flipper {
  display: inline-block;
  position: relative;
  width: 26px;
  height: 30px;
  line-height: 30px;
  border: solid 1px #d6dae6;
  border-radius: 10px;
  background: #fff;
  font-size: 22px;
  color: #4971fe;
  box-shadow: 0 0 6px #d6dae6;
  text-align: center;
}

.M-Flipper .digital:before,
.M-Flipper .digital:after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;

  overflow: hidden;
  box-sizing: border-box;
}

.M-Flipper .digital:before {
  background: linear-gradient(180deg, #f8faff 0%, #e2e8f1 100%);
  top: 0;
  bottom: 50%;
  border-radius: 3px 3px 0 0;
  /* border-bottom: solid 1px #d6dae6; */
}

.M-Flipper .digital:after {
  background: #fff;
  top: 50%;
  bottom: 0;
  border-radius: 0 0 3px 3px;
  line-height: 0;
}

/*向下翻*/
.M-Flipper.down .front:before {
  z-index: 3;
}

.M-Flipper.down .back:after {
  z-index: 2;
  transform-origin: 50% 0%;
  transform: perspective(160px) rotateX(180deg);
}

.M-Flipper.down .front:after,
.M-Flipper.down .back:before {
  z-index: 1;
}

.M-Flipper.down.go .front:before {
  transform-origin: 50% 100%;
  animation: frontFlipDown 0.6s ease-in-out both;
  box-shadow: 0 -2px 6px rgba(255, 255, 255, 0.3);
  backface-visibility: hidden;
}

.M-Flipper.down.go .back:after {
  animation: backFlipDown 0.6s ease-in-out both;
}

/*向上翻*/
.M-Flipper.up .front:after {
  z-index: 3;
}

.M-Flipper.up .back:before {
  z-index: 2;
  transform-origin: 50% 100%;
  transform: perspective(160px) rotateX(-180deg);
}

.M-Flipper.up .front:before,
.M-Flipper.up .back:after {
  z-index: 1;
}

.M-Flipper.up.go .front:after {
  transform-origin: 50% 0;
  animation: frontFlipUp 0.6s ease-in-out both;
  box-shadow: 0 2px 6px rgba(255, 255, 255, 0.3);
  backface-visibility: hidden;
}

.M-Flipper.up.go .back:before {
  animation: backFlipUp 0.6s ease-in-out both;
}

@keyframes frontFlipDown {
  0% {
    transform: perspective(160px) rotateX(0deg);
  }

  100% {
    transform: perspective(160px) rotateX(-180deg);
  }
}

@keyframes backFlipDown {
  0% {
    transform: perspective(160px) rotateX(180deg);
  }

  100% {
    transform: perspective(160px) rotateX(0deg);
  }
}

@keyframes frontFlipUp {
  0% {
    transform: perspective(160px) rotateX(0deg);
  }

  100% {
    transform: perspective(160px) rotateX(180deg);
  }
}

@keyframes backFlipUp {
  0% {
    transform: perspective(160px) rotateX(-180deg);
  }

  100% {
    transform: perspective(160px) rotateX(0deg);
  }
}

.M-Flipper .number0:before,
.M-Flipper .number0:after {
  content: '0';
}

.M-Flipper .number1:before,
.M-Flipper .number1:after {
  content: '1';
}

.M-Flipper .number2:before,
.M-Flipper .number2:after {
  content: '2';
}

.M-Flipper .number3:before,
.M-Flipper .number3:after {
  content: '3';
}

.M-Flipper .number4:before,
.M-Flipper .number4:after {
  content: '4';
}

.M-Flipper .number5:before,
.M-Flipper .number5:after {
  content: '5';
}

.M-Flipper .number6:before,
.M-Flipper .number6:after {
  content: '6';
}

.M-Flipper .number7:before,
.M-Flipper .number7:after {
  content: '7';
}

.M-Flipper .number8:before,
.M-Flipper .number8:after {
  content: '8';
}

.M-Flipper .number9:before,
.M-Flipper .number9:after {
  content: '9';
}
</style>

接下来封装一个子级组件,用来处理中间的动态事件变化 组件名字 myCountdown

<template>
  <div class="FlipClock">
    <countdownSon ref="flipperHour" />
  </div>
</template>

<script setup >
import countdownSon from './countdownSon.vue';

const props = defineProps({
  nowTimeStr: {
    type: Number,
    default: 0,
  },
  nextTimeStr: {
    type: Number,
    default: 0,
  },
});

const flipperHour = ref(null);

watch(
  () => props.nowTimeStr,
  (newval) => {
    if (newval >= 0) {
      // 初始化数字
      flipperHour.value.setFront(newval);
    }
  }
);
watch(
  () => props.nextTimeStr,
  (newval) => {
    if (newval >= 0) {
      // 开始翻牌
      flipperHour.value.flipDown(props.nowTimeStr, newval);
    }
  }
);
// 开始计时
// const timer = ref(null);
// const run = () => {
//   timer.value = setInterval(() => {
//     nowTimeStr.value++;
//     nextTimeStr.value++;
//     flipperHour.value.flipDown(nowTimeStr.value, nextTimeStr.value);
//   }, 1000);
// };
</script>

<style scoped>
</style>

再封装一下当前组件 组件名 allCount

<template>
  <div>
    <div v-if="timeEnd">已到申报截止时间!</div>
    <div class="flex items-center" v-else>
      <div class="fw-600 text-14px color-#303136 pl-10px">
        距申报截止时间:</div
      >
      <div class="ml-10px flex items-center">
        <div
          class="flex"
          v-if="timeArray.days?.length && timeArray.days?.length > 0"
        >
          <div v-for="(item, i) in timeArray.days.length" :key="i">
            <MyCountdown
              :nowTimeStr="Number(timeArray.oldDays[i])"
              :nextTimeStr="Number(timeArray.days[i])"
            />
          </div>
        </div>
        <div class="fw-600 text-14px color-#303136 pl-10px"></div>
      </div>
      <div class="ml-10px flex items-center">
        <div
          class="flex"
          v-if="timeArray.hours?.length && timeArray.hours?.length > 0"
        >
          <div v-for="(item, i) in timeArray.hours.length" :key="i">
            <MyCountdown
              :nowTimeStr="Number(timeArray.oldHours[i])"
              :nextTimeStr="Number(timeArray.hours[i])"
            />
          </div>
        </div>
        <div class="fw-600 text-14px color-#303136 pl-10px"></div>
      </div>
      <div class="ml-10px flex items-center">
        <div
          class="flex"
          v-if="timeArray.minutes?.length && timeArray.minutes?.length > 0"
        >
          <div v-for="(item, i) in timeArray.minutes.length" :key="i">
            <MyCountdown
              :nowTimeStr="Number(timeArray.oldMinutes[i])"
              :nextTimeStr="Number(timeArray.minutes[i])"
            />
          </div>
        </div>
        <div class="fw-600 text-14px color-#303136 pl-10px"></div>
      </div>
      <div class="ml-10px flex items-center">
        <div
          class="flex"
          v-if="timeArray.seconds?.length && timeArray.seconds?.length > 0"
        >
          <div v-for="(item, i) in timeArray.seconds.length" :key="i">
            <MyCountdown
              :nowTimeStr="Number(timeArray.oldSeconds[i])"
              :nextTimeStr="Number(timeArray.seconds[i])"
            />
          </div>
        </div>
        <div class="fw-600 text-14px color-#303136 pl-10px"></div>
      </div>
    </div>
  </div>
</template>

<script setup >
import dayjs from 'dayjs';
import MyCountdown from './myCountdown.vue';

const beforeTime = ref('2026-1-04 15:12:10');

const initMys = (dateStr) => {
  // 获取当前时间
  const now = dayjs().valueOf();
  // 将传入的字符串转换为dayjs对象
  const target = dayjs(dateStr).valueOf();
  const oldDiff = target - now;
  const diff = target - now - 1000;
  const oldDays = Math.floor(oldDiff / (1000 * 60 * 60 * 24));
  const days = Math.floor(diff / (1000 * 60 * 60 * 24));
  const oldHours = Math.floor(
    (oldDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
  );
  const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  const oldMinutes = Math.floor((oldDiff % (1000 * 60 * 60)) / (1000 * 60));
  const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  const oldSeconds = Math.floor((oldDiff % (1000 * 60)) / 1000);
  const seconds = Math.floor((diff % (1000 * 60)) / 1000);
  return {
    oldDays: pasdValue(oldDays),
    days: pasdValue(days),
    oldHours: pasdValue(oldHours),
    hours: pasdValue(hours),
    oldMinutes: pasdValue(oldMinutes),
    minutes: pasdValue(minutes),
    oldSeconds: pasdValue(oldSeconds),
    seconds: pasdValue(seconds),
  };
};
const pasdValue = (values) => {
  if (values <= 0) {
    return ['0', '0'];
  }
  if (values <= 9) {
    return ('0' + values).split('');
  }
  if (values > 9) {
    return String(values).split('');
  }
};

const timeArray = ref({
  oldDays: ['0', '0'],
  days: ['0', '0'],
  oldHours: ['0', '0'],
  hours: ['0', '0'],
  oldMinutes: ['0', '0'],
  minutes: ['0', '0'],
  oldSeconds: ['0', '0'],
  seconds: ['0', '0'],
});
const timeEnd = ref(false);
const timer = ref(null);
const timerToInit = () => {
  timer.value = setInterval(() => {
    timeArray.value = initMys(beforeTime.value);
    if (timeArray.value.days[0] == '0' && timeArray.value.days[1] == '0') {
      if (timeArray.value.hours[0] == '0' && timeArray.value.hours[1] == '0') {
        if (
          timeArray.value.minutes[0] == '0' &&
          timeArray.value.minutes[1] == '0'
        ) {
          if (
            timeArray.value.seconds[0] == '0' &&
            timeArray.value.seconds[1] == '0'
          ) {
            timeEnd.value = true;
            clearInterval(timer.value);
          }
        }
      }
    }
  }, 1000);
};

onMounted(() => {
  timerToInit();
});
</script>

<style scoped>
</style>

接下来就是最外层的使用了 主页面 子页面中的 beforeTime 可以抽出来当传值就行


// js部分

import AllCount from '../components/allCount.vue';

// 页面部分
<AllCount />


最后,原理部分大佬讲的很清楚的。 react,javascript,vue2 技术栈的 链接 mp.weixin.qq.com/s/sA9S-mYAq…