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 分钟前
【Web前端】Pinia状态管理详解
前端·vue.js·typescript
java1234_小锋9 分钟前
分享一套优质的SpringBoot+Vue咖啡商城系统
vue.js·spring boot·咖啡商城
爱学习的程序媛10 分钟前
“数字孪生”详解与前端技术栈
前端·人工智能·计算机视觉·智慧城市·信息与通信
海石18 分钟前
微信小程序开发02:原始人也能看懂的着色器与视频处理
前端·微信小程序·视频编码
程序员Sunday19 分钟前
Claude Code 生态爆发:5个必知的新工具
前端·人工智能·后端
ChoSeitaku30 分钟前
NO.2|proto3语法|消息类型|通讯录|文件读取|enum类型
java·服务器·前端
小J听不清36 分钟前
CSS 边框(border)全解析:样式 / 宽度 / 颜色 / 方向取值
前端·javascript·css·html·css3
用户2557788508137 分钟前
axios全局重复请求取消
前端
前端付豪41 分钟前
实现一个用户可以有多个会话
前端·后端·llm
林古1 小时前
我在 WSL 里控制 Windows Chrome 的一次实战复盘(OpenClaw)
前端