Vue 动态时钟案例

显示效果及源代码

js 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue动态时间显示</title>
  <script src="./js/vue.js"></script>
  <style>
    body {
      font-family: 'Arial', sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
      margin: 0;
    }
    .time-container {
      background: white;
      padding: 2rem 3rem;
      border-radius: 15px;
      box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
      text-align: center;
    }
    .time {
      font-size: 3rem;
      font-weight: bold;
      color: #2c3e50;
      margin-bottom: 0.5rem;
    }
    .date {
      font-size: 1.5rem;
      color: #7f8c8d;
    }
    .weekday {
      font-size: 1.8rem;
      color: #3498db;
      margin-top: 0.5rem;
      font-weight: 500;
    }
  </style>
</head>
<body>
  <div id="app">
    <div class="time-container">
      <div class="time">{{ formattedTime }}</div>
      <div class="date">{{ formattedDate }}</div>
      <div class="weekday">{{ weekday }}</div>
    </div>
  </div>

  <script >
    var vm = new Vue({
    el: '#app',
    data: {
      currentTime: new Date(),
      weekdays: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    },
    computed: {
      formattedTime() {
        const hours = this.padZero(this.currentTime.getHours());
        const minutes = this.padZero(this.currentTime.getMinutes());
        const seconds = this.padZero(this.currentTime.getSeconds());
        return `${hours}:${minutes}:${seconds}`;
      },
      formattedDate() {
        const year = this.currentTime.getFullYear();
        const month = this.padZero(this.currentTime.getMonth() + 1);
        const day = this.padZero(this.currentTime.getDate());
        return `${year}-${month}-${day}`;
      },
      weekday() {
        return this.weekdays[this.currentTime.getDay()];
      }
    },
    methods: {
      padZero(num) {
        return num < 10 ? '0' + num : num;
      },
      updateTime() {
        this.timer = setInterval(() => {
          this.currentTime = new Date();
        }, 1000);
      }
    },

    created() {
      this.updateTime();
    },
    beforeDestroy() {
      clearInterval(this.timer);
    }
  });
  </script>
</body>
</html>
相关推荐
崔庆才丨静觅1 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60612 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了2 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅2 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅2 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅3 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment3 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅3 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊3 小时前
jwt介绍
前端
爱敲代码的小鱼3 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax