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>
相关推荐
Booksort7 小时前
React+js环境配置(极速版)
前端·javascript·react.js
YAY_tyy7 小时前
Cesium 基础:地球场景初始化与视角控制
前端·cesium
椰羊~王小美7 小时前
前后端 格式化货币的方法
java·前端
苯酸氨酰糖化物7 小时前
HTML+CSS学信网学籍学历查询页面-支持任意修改内容信息
前端·css3·html5·娱乐
幻云20107 小时前
Next.js 之道:从入门到精通
前端·javascript·vue.js·人工智能·python
2501_944521597 小时前
Flutter for OpenHarmony 微动漫App实战:标签筛选功能实现
android·开发语言·前端·javascript·flutter
EndingCoder7 小时前
构建工具集成:Webpack 和 TypeScript
前端·webpack·typescript
卡西里弗斯奥7 小时前
【Tomcat】部署Web服务器之Tomcat
服务器·前端·tomcat
Sheldon一蓑烟雨任平生7 小时前
Sass 星空(Sass + keyframes 实现星空动画)
前端·css·vue3·sass·keyframes
多仔ヾ7 小时前
Vue.js 前端开发实战之 05-Vue 过渡和动画
vue.js