最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue簡單練習 桌面時鐘的實現(xiàn)代碼實例

 更新時間:2019年09月19日 14:09:27   作者:袁藝明  
這篇文章主要介紹了vue簡單練習 桌面時鐘的實現(xiàn)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值的相關資料,需要的朋友可以參考下

用vue實現(xiàn)一個簡單的網(wǎng)頁桌面時鐘,主要包括時鐘顯示、計時、暫停、重置等幾個功能。

效果圖如下,頁面剛進來的時候是一個時鐘,時鐘上顯示的時、分、秒為當前實際時間,點擊計時器按鈕后,頁面變成一個計時器,并且計時器按鈕被暫停與重置兩個按鈕替代,分別對計時器進行暫停和重置,若點擊時鐘按鈕會切換回時鐘界面。

代碼如下:

<!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表示計時器計時狀態(tài),2表示計時器暫停狀態(tài)
        hour: '00', // 頁面顯示的數(shù)值
        min: '00',
        sec: '00',
        msec: '00',
        h: 0,    // 臨時保存的數(shù)值
        m: 0,
        s: 0,
        ms: 0,
        timer: null,
        date: null
      },
      // 監(jiān)視器
      watch: {
        index(newValue, oldValue) {
          clearInterval(this.timer);
          this.timer = null;
          this.date = null;
          // 從時鐘頁面click過來 或 從計時器頁面click過來
          if (oldValue == 0 || newValue == 0) {  // index等于2時數(shù)據(jù)保留
            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>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue使用axios實現(xiàn)excel文件下載的功能

    vue使用axios實現(xiàn)excel文件下載的功能

    這篇文章主要介紹了vue中使用axios實現(xiàn)excel文件下載的功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 詳解Vue如何支持JSX語法

    詳解Vue如何支持JSX語法

    這篇文章主要介紹了詳解Vue如何支持JSX語法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue如何通過$router.push傳參數(shù)

    vue如何通過$router.push傳參數(shù)

    這篇文章主要介紹了vue如何通過$router.push傳參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Vue.js圖片滑動驗證的實現(xiàn)示例

    Vue.js圖片滑動驗證的實現(xiàn)示例

    為了防止有人惡意使用腳本進行批量操作,會設置圖片滑動驗證,本文就介紹了Vue.js圖片滑動驗證的實現(xiàn)示例,感興趣的可以了解一下
    2023-05-05
  • Vue?中v-model的完整用法及原理

    Vue?中v-model的完整用法及原理

    本文主要介紹了Vue?中v-model的完整用法及原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • vue 防止頁面加載時看到花括號的解決操作

    vue 防止頁面加載時看到花括號的解決操作

    這篇文章主要介紹了vue 防止頁面加載時看到花括號的解決操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue3 組件的開發(fā)詳情

    Vue3 組件的開發(fā)詳情

    這篇文章主要介紹了Vue3組件的開發(fā),上一篇文章我們價紹了Vue3(三)網(wǎng)站首頁布局開發(fā),今天繼續(xù)上篇內(nèi)容展開組件的開發(fā),需要的朋友可以參考一下
    2021-11-11
  • vue2.0移動端滑動事件vue-touch的實例代碼

    vue2.0移動端滑動事件vue-touch的實例代碼

    這篇文章主要介紹了vue2.0移動端滑動事件vue-touch的實例代碼,需要的朋友可以參考下
    2018-11-11
  • vue模式history下在iis中配置流程

    vue模式history下在iis中配置流程

    這篇文章主要介紹了vue模式history下在iis中配置流程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • vue頁面中使用getElementsByClassName無法獲取元素的解決

    vue頁面中使用getElementsByClassName無法獲取元素的解決

    這篇文章主要介紹了vue頁面中使用getElementsByClassName無法獲取元素的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論

玉门市| 定兴县| 萨嘎县| 武胜县| 新巴尔虎左旗| 柳江县| 齐河县| 东至县| 大庆市| 康定县| 大庆市| 烟台市| 读书| 甘德县| 连江县| 定结县| 鹰潭市| 凤山市| 盘锦市| 涟源市| 桑植县| 开阳县| 朝阳市| 吉林市| 原阳县| 云和县| 应用必备| 宁海县| 绥宁县| 夏津县| 北辰区| 福安市| 云南省| 漳平市| 阳谷县| 绥中县| 石狮市| 齐齐哈尔市| 清镇市| 九江县| 武宣县|