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>
相关推荐
WooaiJava1 分钟前
AI 智能助手项目面试技术要点总结(前端部分)
javascript·大模型·html5
LYFlied5 分钟前
从 Vue 到 React,再到 React Native:资深前端开发者的平滑过渡指南
vue.js·react native·react.js
爱喝白开水a20 分钟前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
Never_Satisfied20 分钟前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
董世昌4121 分钟前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
B站_计算机毕业设计之家35 分钟前
豆瓣电影数据采集分析推荐系统 | Python Vue Flask框架 LSTM Echarts多技术融合开发 毕业设计源码 计算机
vue.js·python·机器学习·flask·echarts·lstm·推荐算法
WeiXiao_Hyy1 小时前
成为 Top 1% 的工程师
java·开发语言·javascript·经验分享·后端
吃杠碰小鸡1 小时前
高中数学-数列-导数证明
前端·数学·算法
kingwebo'sZone1 小时前
C#使用Aspose.Words把 word转成图片
前端·c#·word
xjt_09012 小时前
基于 Vue 3 构建企业级 Web Components 组件库
前端·javascript·vue.js