资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vue如何实现桌面时钟

这篇文章主要为大家展示了“vue如何实现桌面时钟”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“vue如何实现桌面时钟”这篇文章吧。

成都创新互联公司主要从事做网站、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务织金,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

用vue实现一个简单的网页桌面时钟,主要包括时钟显示、计时、暂停、重置等几个功能。

效果图如下,页面刚进来的时候是一个时钟,时钟上显示的时、分、秒为当前实际时间,点击计时器按钮后,页面变成一个计时器,并且计时器按钮被暂停与重置两个按钮替代,分别对计时器进行暂停和重置,若点击时钟按钮会切换回时钟界面。

vue如何实现桌面时钟

vue如何实现桌面时钟

代码如下:




  
  时钟
  
    .clock {
      width: 400px;
      height: 180px;
      line-height: 180px;
      border: 10px solid #aaa;
      border-radius: 10px;
      margin: 120px auto;
      background: pink;
      text-align: center;
      position: relative;
      box-shadow: 0px 5px 20px rgba(0,0,0,.6);
    }
    .clock .text {
      font-size: 70px;
      font-weight: bold;
      color: rgba(0,0,0,.7);
    }
    .clock .btn {
      position: absolute;
      /*top: -66px;*/
      bottom: -66px;
      border: none;
      outline: none;
      width: 80px;
      height: 36px;
      border-radius: 4px;
      font-size: 16px;
      background: #aaa;
      cursor: pointer;
      box-shadow: 0px 5px 20px rgba(0,0,0,.6);
    }
    .clock .btn:hover {
      opacity: 0.8;
    }
    .clock .btn-clock {
      left: 110px;
    }
    .clock .btn-clock.to-left {
      left: 60px;
    }
    .clock .btn-timer {
      right: 110px;
    }
    .clock .btn-suspend {
      right: 160px;
    }
    .clock .btn-reset {
      right: 60px;
    }
  
  


  
    
      
        {{ hour }}:{{ min }}:{{ sec }}
      
      
        {{ min }}:{{ sec }}:{{ msec }}
      
      时钟
      
        计时器
      
       0">
        暂停
        开始
      
      
        重置
      
    
  
       var app = new Vue({       el: '#app',       data: {         index: 0,  // 0表示时钟页面,1表示计时器计时状态,2表示计时器暂停状态         hour: '00', // 页面显示的数值         min: '00',         sec: '00',         msec: '00',         h: 0,    // 临时保存的数值         m: 0,         s: 0,         ms: 0,         timer: null,         date: null       },       // 监视器       watch: {         index(newValue, oldValue) {           clearInterval(this.timer);           this.timer = null;           this.date = null;           // 从时钟页面click过来 或 从计时器页面click过来           if (oldValue == 0 || newValue == 0) {  // index等于2时数据保留             this.hour = '00';              this.min = '00';             this.sec = '00';             this.msec = '00';             this.h = 0;              this.m = 0;             this.s = 0;             this.ms = 0;           }           this.autoMove();         }       },       methods: {         // 自动计时         autoMove() {           if (this.index == 0) {             this.timer = setInterval(res => {               this.date = new Date();               this.h = this.date.getHours();               this.m = this.date.getMinutes();               this.s = this.date.getSeconds();               this.hour = this.h > 9 ? this.h : '0' + this.h;               this.min = this.m > 9 ? this.m : '0' + this.m;               this.sec = this.s > 9 ? this.s : '0' + this.s;             }, 1000);           } else if (this.index == 1){             this.timer = setInterval(res => {               this.ms ++;               if (this.ms == 100) {                 this.s ++;                 this.ms = 0;               }               if (this.s == 60) {                 this.m ++;                 this.s = 0;               }               this.msec = this.ms > 9 ? this.ms : '0' + this.ms;               this.min = this.m > 9 ? this.m : '0' + this.m;               this.sec = this.s > 9 ? this.s : '0' + this.s;             }, 10);           }         },         // 选择时钟         selectClock() {           this.index = 0;         },         // 选择计时器         selectTimer() {           this.index = 1;         },         // 开始、暂停计时器         suspendTimer() {           if (this.index == 1) {             this.index = 2;           } else if (this.index == 2) {             this.index = 1;           }         },         // 重置计时器         resetTimer() {           this.index = 0;           setTimeout(res => {             this.index = 1;           }, 1);         }       },       mounted() {         this.autoMove();       }     })   

以上是“vue如何实现桌面时钟”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


文章标题:vue如何实现桌面时钟
分享URL:http://cdkjz.cn/article/pecedd.html
返回首页 了解更多建站资讯
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220