电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Vue-cli框架實現計時器應用

瀏覽:38日期:2023-02-16 16:15:52
技術背景

本應用使用 vue-cli 框架,并使用了自定義組件(將按鈕拆分成單獨的組件)和vue 模板。

使用說明

開始正計時:點擊工具欄的“開始正計時”按鈕即可,快捷鍵為'Enter'鍵。

開始倒計時:在輸入框內寫入時間后,點擊“開始倒計時”按鈕,即可開始倒計時。

暫停計時器:點擊“暫停計時器”按鈕即可暫停。

清空正/倒計時:點擊此按鈕,計時器便會回到初始狀態,等待新的計時。

重新再計時:點擊此按鈕,計時器便會重新開始此次計時。

恢復計時器:點擊此按鈕即可從暫停狀態中恢復。

Vue-cli框架實現計時器應用

代碼

首先初始化項目

vue init webpack <project name>

components文件夾中放入文件:CounterButton.vue

<template> <div> <button v-bind: v-on:click='$emit(’click-button’)'>{{ text }}</button> </div></template><script>export default { name: 'CounterButton', props: { text: String }, data: function() { return { styleObject: {countup: false,countdown: false,clear: false,pause: false,restart: false,resume: false } }; }, created: function() { if (this.text == '開始正計時') { this.styleObject.countup = true; } else if (this.text == '開始倒計時') { this.styleObject.countdown = true; } else if (this.text == '清空倒計時' || this.text == '清空正計時') { this.styleObject.clear = true; } else if (this.text == '暫停計時器') { this.styleObject.pause = true; } else if (this.text == '重新再計時') { this.styleObject.restart = true; } else if (this.text == '恢復計時器') { this.styleObject.resume = true; } }};</script><style>.countup { background-color: #2188e9; border-radius: 5px; border-color: #2188e9; position: absolute; left: 310px; top: 15px; width: 98px; height: 40px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #ffffff;}.countdown { background-color: #2188e9; border-radius: 5px; border-color: #2188e9; position: absolute; left: 428px; top: 15px; width: 98px; height: 40px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #ffffff;}.clear { background-color: #dd2e1d; border-radius: 5px; border-color: #dd2e1d; position: absolute; left: 964px; top: 15px; width: 98px; height: 40px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #ffffff;}.pause { background-color: #2188e9; border-radius: 5px; border-color: #2188e9; position: absolute; left: 227px; top: 15px; width: 98px; height: 40px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #ffffff;}.restart { background-color: #ffb020; border-radius: 5px; border-color: #ffb020; position: absolute; left: 1082px; top: 15px; width: 98px; height: 40px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #ffffff;}.resume { background-color: #2188e9; border-radius: 5px; border-color: #2188e9; position: absolute; left: 227px; top: 15px; width: 98px; height: 40px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #ffffff;}</style>

將App.vue改為

<template> <div id='app'> <div class='toolbar'> <div v-show='initialSeen'><input v-model='hour' /><input v-model='minute' /><input v-model='second' /><span id='hourlabel'>時</span><span id='minutelabel'>分</span><span id='secondlabel'>秒</span><counter-button text='開始正計時' v-on:click-button='startCountUp' id='countup'></counter-button><counter-button text='開始倒計時' v-on:click-button='startCountDown' id='countdown'></counter-button> </div> <span v-show='hintSeen'>{{ hint }}</span> <counter-button v-bind:text='clearText' v-on:click-button='clearCounter' v-show='clearSeen' id='clear'></counter-button> <counter-button text='暫停計時器' v-on:click-button='pauseCounter' v-show='pauseSeen' id='pause'></counter-button> <counter-button text='重新再計時' v-on:click-button='restartCounter' v-show='restartSeen' id='restart'></counter-button> <counter-button text='恢復計時器' v-on:click-button='resumeCounter' v-show='resumeSeen' id='resume'></counter-button> </div> <span id='time'>{{ time }}</span> </div></template><script>import CounterButton from './components/CounterButton';export default { name: 'App', data: function() { return { status: 1, // status---1: before start; 2: up timing; 3: down timing; 4: up pausing; // 5: down pausing; 6: down finished; hour: null, minute: null, second: null, time: '00:00:00', timer: null, Hour: 0, Minute: 0, Second: 0, Millisecond: 0, hourString: '', minuteString: '', secondString: '', recordHour: 0, recordMinute: 0, recordSecond: 0, recordMillisecond: 0, hint: '正在倒計時 12:20:00', clearText: '清空倒計時', initialSeen: true, clearSeen: false, pauseSeen: false, restartSeen: false, resumeSeen: false, hintSeen: false }; }, methods: { format: function(Hour, Minute, Second) { if (Second < 10) {this.secondString = '0' + Second; } else {this.secondString = Second; } if (Minute < 10) {this.minuteString = '0' + Minute; } else {this.minuteString = Minute; } if (Hour < 10) {this.hourString = '0' + Hour; } else {this.hourString = Hour; } return (this.hourString + ':' + this.minuteString + ':' + this.secondString ); }, changeStatus: function(aimStatus) { if (aimStatus == 1) {// before startthis.initialSeen = true;this.clearSeen = false;this.pauseSeen = false;this.restartSeen = false;this.resumeSeen = false;this.hintSeen = false; } else if (aimStatus == 2 || aimStatus == 3) {// up timing || down timingthis.initialSeen = false;this.clearSeen = true;this.pauseSeen = true;this.restartSeen = true;this.resumeSeen = false;this.hintSeen = true;if (aimStatus == 2) { this.hint = '正在正計時'; this.clearText = '清空正計時';} else if (aimStatus == 3) { this.recordHour = parseInt(this.recordMillisecond / 3600000); this.recordMinute = parseInt( (this.recordMillisecond % 3600000) / 60000 ); this.recordSecond = parseInt((this.recordMillisecond % 60000) / 1000); this.hint = '正在倒計時 ' + this.format(this.recordHour, this.recordMinute, this.recordSecond); this.clearText = '清空倒計時';} } else if (aimStatus == 4 || aimStatus == 5) {// up pausing || down pausingthis.initialSeen = false;this.clearSeen = true;this.pauseSeen = false;this.restartSeen = true;this.resumeSeen = true;this.hintSeen = true;if (aimStatus == 4) { // up pausing this.hint = '暫停正計時'; this.clearText = '清空正計時';} else if (aimStatus == 5) { // down pausing this.recordHour = parseInt(this.recordMillisecond / 3600000); this.recordMinute = parseInt( (this.recordMillisecond % 3600000) / 60000 ); this.recordSecond = parseInt((this.recordMillisecond % 60000) / 1000); this.hint = '暫停倒計時 ' + this.format(this.recordHour, this.recordMinute, this.recordSecond); this.clearText = '清空倒計時';} } else if (aimStatus == 6) {// down finishedthis.initialSeen = false;this.clearSeen = true;this.pauseSeen = false;this.restartSeen = true;this.resumeSeen = false;this.hintSeen = true;this.recordHour = parseInt(this.recordMillisecond / 3600000);this.recordMinute = parseInt( (this.recordMillisecond % 3600000) / 60000);this.recordSecond = parseInt((this.recordMillisecond % 60000) / 1000);this.hint = '倒計時 ' + this.format(this.recordHour, this.recordMinute, this.recordSecond) + ' 已結束'; } }, CountUp: function() { this.Millisecond += 50; this.Hour = parseInt(this.Millisecond / 3600000); this.Minute = parseInt((this.Millisecond % 3600000) / 60000); this.Second = parseInt((this.Millisecond % 60000) / 1000); this.time = this.format(this.Hour, this.Minute, this.Second); }, CountDown: function() { this.Millisecond -= 50; this.Hour = parseInt(this.Millisecond / 3600000); this.Minute = parseInt((this.Millisecond % 3600000) / 60000); this.Second = parseInt((this.Millisecond % 60000) / 1000); if (this.Millisecond <= 0) {clearInterval(this.timer);this.changeStatus(6); } this.time = this.format(this.Hour, this.Minute, this.Second); }, startCountUp: function() { this.status = 2; this.Millisecond = 0; this.changeStatus(this.status); this.timer = setInterval(this.CountUp, 50); }, startCountDown: function() { this.status = 3; this.Hour = this.hour; if (this.minute > 59) {this.Minute = 59; } else {this.Minute = this.minute; } if (this.second > 59) {this.Second = 59; } else {this.Second = this.second; } this.hour = null; this.minute = null; this.second = null; this.Millisecond =this.Hour * 3600000 + this.Minute * 60000 + this.Second * 1000; this.recordMillisecond = this.Millisecond; this.changeStatus(this.status); this.timer = setInterval(this.CountDown, 50); }, clearCounter: function() { this.status = 1; this.changeStatus(this.status); clearInterval(this.timer); this.time = this.format(0, 0, 0); }, pauseCounter: function() { if (this.status == 2) {// Now count upthis.status = 4;this.changeStatus(this.status);clearInterval(this.timer); } else if (this.status == 3) {// now count downthis.status = 5;this.changeStatus(this.status);clearInterval(this.timer); } }, restartCounter: function() { if (this.status == 2 || this.status == 4) {this.status = 2;this.Millisecond = 0;this.changeStatus(this.status);clearInterval(this.timer);this.timer = setInterval(this.CountUp, 50); } else if ((this.status = 3 || this.status == 5 || this.status == 6)) {this.status = 3;this.Millisecond = this.recordMillisecond;this.changeStatus(this.status);clearInterval(this.timer);this.timer = setInterval(this.CountDown, 50); } }, resumeCounter: function() { if (this.status == 4) {this.status = 2;this.changeStatus(this.status);this.timer = setInterval(this.CountUp, 50); } else if ((status = 5)) {this.status = 3;this.changeStatus(this.status);this.timer = setInterval(this.CountDown, 50); } }, // 鍵盤事件 handleKeyup(event) { const e = event || window.event || arguments.callee.caller.arguments[0]; if (!e) return; const { key, keyCode } = e; if (key == 'Enter') {if (this.status == 1) { // before start this.status = 2; this.Millisecond = 0; this.changeStatus(this.status); this.timer = setInterval(this.CountUp, 50);} } else if (keyCode == 32) {if (this.status == 2) { // Now count up this.status = 4; this.changeStatus(this.status); clearInterval(this.timer);} else if (this.status == 3) { // now count down this.status = 5; this.changeStatus(this.status); clearInterval(this.timer);} else if (this.status == 4) { this.status = 2; this.changeStatus(this.status); this.timer = setInterval(this.CountUp, 50);} else if (this.status == 5) { this.status = 3; this.changeStatus(this.status); this.timer = setInterval(this.CountDown, 50);} } } }, mounted: function() { window.addEventListener('keyup', this.handleKeyup); }, destroyed() { window.removeEventListener('keyup', this.handleKeyup); }, components: { CounterButton }};</script><style>body { margin: 0; padding: 0;}.toolbar { background-color: #97a5bc; width: 1220px; height: 70px;}#hour { background-color: white; border-radius: 5px; position: absolute; left: 40px; top: 15px; width: 69px; height: 34px; font-size: 15px;}#hourlabel { position: absolute; left: 86px; top: 24px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #222222;}#minute { background-color: white; border-radius: 5px; position: absolute; left: 130px; top: 15px; width: 69px; height: 34px; font-size: 15px;}#minutelabel { position: absolute; left: 177px; top: 24px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #222222;}#second { background-color: white; border-radius: 5px; position: absolute; left: 220px; top: 15px; width: 69px; height: 34px; font-size: 15px;}#secondlabel { position: absolute; left: 268px; top: 24px; font-family: PingFangSC-Regular, 'PingFang SC', sans-serif; font-size: 16px; color: #222222;}#time { position: absolute; left: 131px; top: 197px; font-size: 200px; font-family: PTMono-Bold, 'PT Mono', monospace; font-weight: 700; color: #333333;}#hint { position: absolute; left: 40px; top: 24px; font-family: PTMono-Bold, 'PT Mono', monospace; font-size: 16px; color: white;}</style>

最后在目錄中使用

npm run dev

即可運行該項目。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 能耗监测系统-节能监测系统-能源管理系统-三水智能化 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | 滚珠丝杆升降机_螺旋升降机_丝杠升降机-德迈传动 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 东莞市超赞电子科技有限公司 全系列直插/贴片铝电解电容,电解电容,电容器 | 医学动画公司-制作3d医学动画视频-医疗医学演示动画制作-医学三维动画制作公司 | 瓶盖扭矩测试仪-瓶盖扭力仪-全自动扭矩仪-济南三泉中石单品站 | 西门子伺服控制器维修-伺服驱动放大器-828D数控机床维修-上海涌迪 | ZHZ8耐压测试仪-上海胜绪电气有限公司 | 翰墨AI智能写作助手官网_人工智能问答在线AI写作免费一键生成 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 杭州中策电线|中策电缆|中策电线|杭州中策电缆|杭州中策电缆永通集团有限公司 | 蒸压釜-陶粒板隔墙板蒸压釜-山东鑫泰鑫智能装备有限公司 | 深圳标识制作公司-标识标牌厂家-深圳广告标识制作-玟璟广告-深圳市玟璟广告有限公司 | 冷凝水循环试验箱-冷凝水试验箱-可编程高低温试验箱厂家-上海巨为(www.juweigroup.com) | 精密冲床,高速冲床等冲压设备生产商-常州晋志德压力机厂 | 重庆中专|职高|技校招生-重庆中专招生网 | 奶茶加盟,奶茶加盟店连锁品牌-甜啦啦官网| 茶楼装修设计_茶馆室内设计效果图_云臻轩茶楼装饰公司 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | 洗瓶机厂家-酒瓶玻璃瓶冲瓶机-瓶子烘干机-封口旋盖压盖打塞机_青州惠联灌装机械 | 深圳天际源广告-形象堆头,企业文化墙,喷绘,门头招牌设计制作专家 | 杭州翻译公司_驾照翻译_专业人工翻译-杭州以琳翻译有限公司官网 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 | ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 | 冷却塔改造厂家_不锈钢冷却塔_玻璃钢冷却塔改造维修-广东特菱节能空调设备有限公司 | 济南网站建设_济南网站制作_济南网站设计_济南网站建设公司_富库网络旗下模易宝_模板建站 | 消防设施操作员考试报名时间,报名入口,报考条件 | 高压油管,液压接头,液压附件-烟台市正诚液压附件 | 铝合金脚手架厂家-专注高空作业平台-深圳腾达安全科技 | 开平机_纵剪机厂家_开平机生产厂家|诚信互赢-泰安瑞烨精工机械制造有限公司 | 818手游网_提供当下热门APP手游_最新手机游戏下载 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 合肥宠物店装修_合肥宠物美容院装修_合肥宠物医院设计装修公司-安徽盛世和居装饰 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 碳纤维布-植筋胶-灌缝胶-固特嘉加固材料公司 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 |