【vue】时间组件--chrome插件开发

介绍

这是一个Vue3数字时钟组件,支持多种时间显示格式。
- 主要功能包括:
- 1. 可切换12/24小时制;
- 2. 可选显示秒数;
- 3. 支持日期显示;
- 4. 可选显示年份和星期。

  • 组件使用了CSS毛玻璃效果,具有半透明背景和模糊效果。通过setInterval实现实时更新时间,并在组件销毁时清除定时器。组件采用响应式设计,通过props控制各元素的显示/隐藏状态,包括时分秒、年月日、星期等信息的显示选项。

实现效果

在这里插入图片描述

代码重点

  • 实现主要依靠Date对象的方法加上定时器。当然也可以使用moment.js,或者其他的时间插件实现。
function handlerDateAndTimeFn() {
  isShowBox.value = true;
  const now = new Date();
  curYears.value = now.getFullYear(); // 年份
  curMonth.value = now.getMonth() + 1; // 月份
  curDay.value = now.getDate(); // 日
  curHours24.value = now.getHours(); //小时:24小时制
  curMinutes.value =
    now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes(); //分钟
  curSeconds.value =
    now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds(); //秒
  dayName.value = days[now.getDay()];
}

timerObj.value = setInterval(() => {
  handlerDateAndTimeFn();
}, 1000);

全部代码

<script setup>
import {
  ref,
  defineOptions,
  computed,
  defineProps,
  onBeforeUnmount,
} from "vue";
defineOptions({
  name: "timeNum", //日期组件
});

const props = defineProps({
  isShow12: {
    //切换12/24小时制
    type: Boolean,
    default: false,
  },
  isShowSeconds: {
    //秒
    type: Boolean,
    default: true,
  },
  isShowDate: {
    //日期
    type: Boolean,
    default: true,
  },
  isShowYears: {
    //年
    type: Boolean,
    default: false,
  },
  isShowWeek: {
    //周
    type: Boolean,
    default: false,
  },
});
let days = ["天", "一", "二", "三", "四", "五", "六"];

// 当前组件涉及功能
// 上边涉及功能   -24/12 -时分秒 -年月日 -周
let isShowBox = ref(false);
let curHours24 = ref("");
let curMinutes = ref("");
let curSeconds = ref("");
let curYears = ref("");
let curMonth = ref("");
let curDay = ref("");
let dayName = ref("");
let timerObj = ref(null);
let curHours12 = computed(() => {
  let result = "00";
  result = curHours24.value > 12 ? curHours24.value - 12 : curHours24.value;
  result = result < 10 ? "0" + result : result;
  return result;
});
// 下边功能  -农历

// 通用 -加粗 -字体大小 -字体 -颜色

function handlerDateAndTimeFn() {
  isShowBox.value = true;
  const now = new Date();
  curYears.value = now.getFullYear(); // 年份
  curMonth.value = now.getMonth() + 1; // 月份
  curDay.value = now.getDate(); // 日
  curHours24.value = now.getHours(); //小时:24小时制
  curMinutes.value =
    now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes(); //分钟
  curSeconds.value =
    now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds(); //秒
  dayName.value = days[now.getDay()];
}
timerObj.value = setInterval(() => {
  handlerDateAndTimeFn();
}, 1000);

onBeforeUnmount(() => {
  if (timerObj.value) {
    clearInterval(timerObj.value);
  }
});
</script>
<template>
  <div class="time-num-wrapper glass-effect-0" v-if="isShowBox">
    <div class="time-num-content">
      <div class="time-num-top">
        <span v-if="isShow12">{{ curHours12 }} </span>
        <span v-else>{{ curHours24 }} </span>
        <span>:</span>
        <span>{{ curMinutes }} </span>
        <span v-if="isShowSeconds">
          <span>:</span>
          <span>{{ curSeconds }}</span>
        </span>
      </div>
      <div class="date-num-bottom">
        <div class="date-num-box">
          <span v-if="isShowYears">{{ curYears }}年</span>
          <span>{{ curMonth }}月</span>
          <span>{{ curDay }}日</span>
        </div>
        <div class="date-week-box" v-if="isShowWeek">星期{{ dayName }}</div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.glass-effect-0 {
  background: rgba(99, 99, 99, 0.5) !important; /* 半透明背景 */
  border-radius: 16px; /* 圆角 */
  backdrop-filter: blur(5px); /* 模糊效果 */
  border: 1px solid rgba(255, 255, 255, 0.5); /* 边框 */
  color: #fff;
}
.time-num-wrapper {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border-radius: 5px;
}
.time-num-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.time-num-top {
  font-size: 30px;
  line-height: 30px;
  font-weight: 700;
  margin-bottom: 15px;
}
.date-num-bottom {
  display: flex;
  font-size: 14px;
}
</style>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝莓味的口香糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值