Anime+Vue<svg数字时钟效果>

效果图:

20200317.gif

参考效果地址:http://www.jq22.com/code3076

代码如下:
<template>
  <div class="overall">
    <div class="clock" v-for="(i,index) in 6" :key="index" data-number="0">
      <svg viewBox="0 0 50 100">
        <path d="M0,0 L50,0 L50,50 L50,100 L0,100 L0,25 L0,0" />
      </svg>
    </div>
  </div>
</template>

<script>
import anime from "animejs/lib/anime.es.js";
export default {
  data() {
    return {
      clockTags: null,
      paths: [
        // 0
        "M0,0 L50,0 L50,50 L50,100 L0,100 L0,25 L0,0",
        // 1
        "M50,0 L50,15 L50,30 L50,45 L50,60 L50,75 L50,100",
        // 2
        "M0,0 L50,0 L50,50 L25,50 L0,50 L0,100 L50,100",
        // 3
        "M0,0 L50,0 L50,100 L0,100 L50,100 L50,50 L0,50",
        // 4
        "M0,0 L0,50 L50,50 L50,0 L50,35 L50,70 L50,100",
        // 5
        "M50,0 L0,0 L0,50 L25,50 L50,50 L50,100 L0,100",
        // 6
        "M50,0 L0,0 L0,50 L0,100 L50,100, L50,50 L0,50",
        // 7
        "M0,0 L50,0 L50,20 L50,40 L50,60 L50,80 L50,100",
        // 8
        "M0,0 L50,0 L50,100 L0,100 L0,0 L0,50 L50,50",
        // 9
        "M0,100 L50,100 L50,50 L50,0 L0,0 L0,50 L50,50"
      ]
    };
  },
  mounted() {
    this.clockTags = document.getElementsByClassName("clock");
    this.setTime();
    //每隔一秒执行一次方法
    setInterval(this.setTime, 1000);
  },
  methods: {
    setTime() {
      let time_numbers = new Date()
        .toTimeString()
        .substr(0, 8)
        .split(":")
        .join("");
      // console.log(time_numbers);
      for (let i = 0; i < time_numbers.length; i++) {
        // .dataset.number 对应 html 上的 data-number
        // time_numbers.charAt(i) 下标位置
        if (this.clockTags[i].dataset.number !== time_numbers.charAt(i)) {
          this.clockTags[i].dataset.number = time_numbers.charAt(i);
          this.morphDigit(
            this.clockTags[i].querySelector("path"),
            this.paths[time_numbers.charAt(i)]
          );
        }
      }
    },
    //变换动画
    morphDigit(tag, numberPath) {
      anime({
        targets: tag,
        d: [{ value: numberPath }],
        easing: "easeInOutExpo",
        duration: 800
      });
    }
  }
};
</script>

<style  scoped>
.overall {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgb(44, 121, 221);
}
svg {
  width: 100%;
  height: 100%;
}
path {
  stroke: #ffffff;
  stroke-width: 2px;
  fill: none;
}
.clock {
  margin: 0 10px;
  width: 50px;
  height: 100px;
}
.clock:nth-child(2n) {
  margin-right: 40px;
  position: relative;
}
.clock:nth-child(2n)::after {
  content: ":";
  position: absolute;
  color: #ffffff;
  font-size: 50px;
  top: 20px;
  right: -30px;
}
.clock:last-child {
  margin-right: 0px !important;
}
.clock:last-child::after {
  display: none !important;
}
</style>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 效果图: 此篇文章是我根据效果,自己仿写的效果,并没有直接使用以上地址的代码~~(主要是下了也看不懂😂)这里大致告...
    誰在花里胡哨阅读 2,403评论 1 16
  • 明天有可能会上台去讲课,这还是第一次讲课,为了这次讲课,我已经准备来好几天了,认真的做好PPT,认真的查询资料,想...
    弗心书里阅读 231评论 0 0
  • 回顾十一月,感叹时光匆匆,也是收获多多! 在过去的十一月里,开启了早上的晨读,感觉很好,再次对《弟子规》有了深入了...
    霜霜_bb0c阅读 288评论 1 0

友情链接更多精彩内容