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

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

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>时钟</title>
<style type="text/css">
.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;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div class="clock">
<span class="text" v-if="index == 0">
{{ hour }}:{{ min }}:{{ sec }}
</span>
<span class="text" v-else>
{{ min }}:{{ sec }}:{{ msec }}
</span>
<button class="btn btn-clock" @click="selectClock" :class="{'to-left': index != 0}">时钟</button>
<button class="btn btn-timer" @click="selectTimer" v-if="index == 0">
<span>计时器</span>
</button>
<button class="btn btn-suspend" @click="suspendTimer" v-if="index > 0">
<span v-if="index == 1">暂停</span>
<span v-if="index == 2">开始</span>
</button>
<button class="btn btn-reset" @click="resetTimer" v-if="index == 1 || index == 2">
<span>重置</span>
</button>
</div>
</div>
<script type="text/javascript">
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();
}
})
</script>
</body>
</html>

vue练手项目——桌面时钟的更多相关文章

  1. Vue练手项目(包含typescript版本)

    本项目的git仓库https://github.com/lznism/xiachufang-vue 对应的使用typescript实现的版本地址https://github.com/lznism/xi ...

  2. Vuex/Vue 练手项目 在线汇率转换器

    详情请点击: https://zhuanlan.zhihu.com/p/33362758

  3. web前端学习部落22群分享给需要前端练手项目

    前端学习还是很有趣的,可以较快的上手然后自己开发一些好玩的项目来练手,网上也可以一抓一大把关于前端开发的小项目,可是还是有新手在学习的时候不知道可以做什么,以及怎么做,因此,就整理了一些前端项目教程, ...

  4. Python之路【第二十四篇】:Python学习路径及练手项目合集

      Python学习路径及练手项目合集 Wayne Shi· 2 个月前 参照:https://zhuanlan.zhihu.com/p/23561159 更多文章欢迎关注专栏:学习编程. 本系列Py ...

  5. 20个Java练手项目,献给嗜学如狂的人

    给大家推荐一条由浅入深的JAVA学习路径,首先完成 Java基础.JDK.JDBC.正则表达式等基础实验,然后进阶到 J2SE 和 SSH 框架学习.最后再通过有趣的练手项目进行巩固. JAVA基础 ...

  6. Python练手项目:20行爬取全王者全英雄皮肤

    引言    王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了.我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成. ...

  7. 去哪找Java练手项目?

    经常有读者在微信上问我: 在学编程的过程中,看了不少书.视频课程,但是看完.听完之后感觉还是不会编程,想找一些项目来练手,但是不知道去哪儿找? 类似的问题,有不少读者问,估计是大部分人的困惑. 练手项 ...

  8. Python学习路径及练手项目合集

    Python学习路径及练手项目合集 https://zhuanlan.zhihu.com/p/23561159

  9. webpack练手项目之easySlide(三):commonChunks(转)

    Hello,大家好. 在之前两篇文章中: webpack练手项目之easySlide(一):初探webpack webpack练手项目之easySlide(二):代码分割 与大家分享了webpack的 ...

随机推荐

  1. HttpClient系统日志配置

    详细介绍在:http://hc.apache.org/httpclient-3.x/logging.html 一般使用context logging基本够用,context logging解释原文如下 ...

  2. [LC] 165. Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 &l ...

  3. python中编码判断

    https://www.cnblogs.com/lc-D-a/p/6074878.html python3 用isinstance()检查unicode编码报错

  4. JavaScript学习总结(七)Ajax和Http状态字

    转自:http://segmentfault.com/a/1190000000691919 Ajax及其工作原理 AJAX 是一种与服务器交换数据无需刷新网页的技术,最早由Google公司在谷歌地图里 ...

  5. vue面试总结

    https://www.kancloud.cn/hanxuming/vue-iq/728305 https://segmentfault.com/a/1190000016351284 https:// ...

  6. jmeter接口压测的反思

    jmeter接口压测的反思 1.keepalive的坑:连接数满了,导致发起的请求失败. 2.token关联?是数据库取做参数化,还是随机数生成(需要改代码) 3.签名问题如何处理? 4.压测负载机端 ...

  7. montagy

    因为只是想分享ghcjs和webgl的使用经验,所以很多地方说的很粗,因为涉及的知识确实很多, 推荐两本书,一本haskell基础的 learn you a haskell for great goo ...

  8. 吴裕雄--天生自然HTML学习笔记:HTML 段落

    HTML 可以将文档分割为若干段落. HTML 段落 段落是通过 <p> 标签定义的. 实例 <p>这是一个段落 </p> <p>这是另一个段落< ...

  9. [洛谷P4777] [模板] 扩展中国剩余定理

    扩展中国剩余定理,EXCRT. 题目传送门 重温一下中国剩余定理. 中国剩余定理常被用来解线性同余方程组: x≡a[1] (mod m[1]) x≡a[2] (mod m[2]) ...... x≡a ...

  10. Jekyll的_config文件配置报错

    8 April 2019 by IchiehPan Jekyll 搭建博客时, 修改完_config.yml文件后就收到了github的build error邮件. 大致内容如下: did not f ...