一個簡易的 LED 數字時鐘實現方法

這個應該是已經有不少人作過的東西,我應該只是算手癢,想寫一下,因此,花了點時間折騰了這個,順便把 Dark Mode 的處理也加上了。git

首先能夠很明確的一點,這個真沒技術含量存在,只是須要點耐心。flex

LED 數字包含了左右各兩條線,中間三條線,一共 7 條線。因此,爲了可以更容易辨識,在寫 demo 的時候,我直接這樣寫了。優化

<div class="digital digital_0">
  <span class="c1"></span>
  <span class="c2"></span>
  <span class="c3"></span>
  <span class="l1"></span>
  <span class="l2"></span>
  <span class="r1"></span>
  <span class="r2"></span>
</div>

至於 digital_0 這個的做用,其實很簡單,就是控制從 0 ~ 9 這十個數字的變化而存在的。動畫

.digital_1 .c1,
.digital_1 .c2,
.digital_1 .c3,
.digital_1 .l1,
.digital_1 .l2,
.digital_2 .l1,
.digital_2 .r2,
.digital_3 .l1,
.digital_3 .l2,
.digital_4 .c1,
.digital_4 .c3,
.digital_4 .l2,
.digital_5 .l2,
.digital_5 .r1,
.digital_6 .r1,
.digital_7 .c2,
.digital_7 .c3,
.digital_7 .l1,
.digital_7 .l2,
.digital_9 .l2,
.digital_0 .c2 {
  animation: changeDigital 200ms 0ms 1 ease-in forwards;
}

這裏使用了一個 animation 動畫,而且時間是 200ms,主要是爲了讓某些部分消失的時候,有一個過渡效果。spa

@keyframes changeDigital {
  form {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

那麼剩下來的就是最須要耐心的地方了,調整控制那 7 條線的位置,同時還要考慮每條線是帶有斜角的,並且還稍微有點圓弧的感受。帶點圓弧的感受那麼直接使用 border-radius 就能夠了,至於那個斜角的話,若是各位知道 CSS 中用邊框畫三角的方法,那麼就應該明白怎麼作這個斜角了。code

當咱們要畫三角的時候,width 和 height 都是爲 0,而後用過控制 border-width 而獲得最終的三角的大小,那麼這裏若是咱們根據線條的位置,適當選擇 width 和 height 有具體的值,是否是就有斜角的感受了呢。orm

接着,須要注意橫着的中間那根線,也就是 .c2 這個元素,線條兩邊是三角突出的,能夠實現的方式也不少,這裏我選擇使用 :after 和 :before 相疊加。blog

最後就是對每一個元素的位置作調整,控制好大小位置就能夠了,由於是使用定位的方式來操做,因此,調整起來仍是很簡單的。ci

.digital span {
  position: absolute;
  border-radius: 50vh;
  box-sizing: border-box;
}
.digital .c1,
.digital .c2,
.digital .c3 {
  height: 0;
  width: 26px;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
}
.digital .c1 {
  top: 0;
  left: 0;
  border-top: 4px solid currentColor;
}
.digital .c2 {
  top: 50%;
  left: 0;
  margin-top: -2px;
}
.digital .c2:before,
.digital .c2:after {
  content: "";
  height: 0;
  width: 24px;
  border-left: 2px solid transparent;
  border-right: 2px solid transparent;
  box-sizing: border-box;
}
.digital .c2:before {
  position: absolute;
  top: 0;
  left: -3px;
  border-bottom: 2px solid currentColor;
}
.digital .c2:after {
  position: absolute;
  top: 2px;
  left: -3px;
  border-top: 2px solid currentColor;
}
.digital .c3 {
  bottom: 0;
  left: 0;
  border-bottom: 4px solid currentColor;
}
.digital .l1,
.digital .l2 {
  height: 21px;
  width: 0;
  left: 0;
  border-top: 2px solid transparent;
  border-bottom: 2px solid transparent;
  border-left: 4px solid currentColor;
}
.digital .l1 {
  top: 1px;
  border-top-width: 4px;
}
.digital .l2 {
  top: 24px;
  border-bottom-width: 4px;
}
.digital .r1,
.digital .r2 {
  height: 21px;
  width: 0;
  right: 0;
  border-top: 2px solid transparent;
  border-bottom: 2px solid transparent;
  border-right: 4px solid currentColor;
}
.digital .r1 {
  top: 1px;
  border-top-width: 4px;
}
.digital .r2 {
  top: 24px;
  border-bottom-width: 4px;
}

作完這些,剩下的就是簡單的對外層元素作點優化,好比稍微傾斜一點。get

.digital {
  position: relative;
  width: 26px;
  height: 46px;
  margin-left: 10px;
  transform: skew(-6deg);
}

哦,忘了說了,這裏的邊框顏色我選擇使用 currentColor 的方式,這樣能夠在暗黑模式切換的過程當中,只須要修改 body 中的文字顏色和背景色就能夠了。currentColor 會選擇文字顏色來使用。

樣式處理完以後,接着就是添加點 JS 來顯示時鐘了,只要把兩個數字整出來放到不一樣的 div 中就行了。這裏我取個位數的時候,是用 m%10 的方式取餘;取十位數的時候是經過 parseInt(m/10) 的方式取整。取出來以後分別放到想對應的 div 中就 ok 了。

完整代碼以下:

樣式:

body {
  color: #393e4d;
  background-color: #f5f5f5;
}
@media screen and (prefers-color-scheme: dark) {
  body {
    color: #59f6fb;
    background-color: #1c1b1e;
  }
}
.digital {
    position: relative;
    width: 26px;
    height: 46px;
  margin-left: 10px;
    transform: skew(-6deg);
}
.digital span {
    position: absolute;
    border-radius: 50vh;
    box-sizing: border-box;
}
.digital .c1,
.digital .c2,
.digital .c3 {
    height: 0;
    width: 26px;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
}
.digital .c1 {
    top: 0;
    left: 0;
    border-top: 4px solid currentColor;
}
.digital .c2 {
    top: 50%;
    left: 0;
    margin-top: -2px;
}
.digital .c2:before,
.digital .c2:after {
    content: "";
    height: 0;
    width: 24px;
    border-left: 2px solid transparent;
    border-right: 2px solid transparent;
    box-sizing: border-box;
}
.digital .c2:before {
    position: absolute;
    top: 0;
    left: -3px;
    border-bottom: 2px solid currentColor;
}
.digital .c2:after {
    position: absolute;
    top: 2px;
    left: -3px;
    border-top: 2px solid currentColor;
}
.digital .c3 {
    bottom: 0;
    left: 0;
    border-bottom: 4px solid currentColor;
}
.digital .l1,
.digital .l2 {
    height: 21px;
    width: 0;
    left: 0;
    border-top: 2px solid transparent;
    border-bottom: 2px solid transparent;
    border-left: 4px solid currentColor;
}
.digital .l1 {
    top: 1px;
    border-top-width: 4px;
}
.digital .l2 {
    top: 24px;
    border-bottom-width: 4px;
}
.digital .r1,
.digital .r2 {
    height: 21px;
    width: 0;
    right: 0;
    border-top: 2px solid transparent;
    border-bottom: 2px solid transparent;
    border-right: 4px solid currentColor;
}
.digital .r1 {
    top: 1px;
    border-top-width: 4px;
}
.digital .r2 {
    top: 24px;
    border-bottom-width: 4px;
}

.digital_1 .c1,
.digital_1 .c2,
.digital_1 .c3,
.digital_1 .l1,
.digital_1 .l2,
.digital_2 .l1,
.digital_2 .r2,
.digital_3 .l1,
.digital_3 .l2,
.digital_4 .c1,
.digital_4 .c3,
.digital_4 .l2,
.digital_5 .l2,
.digital_5 .r1,
.digital_6 .r1,
.digital_7 .c2,
.digital_7 .c3,
.digital_7 .l1,
.digital_7 .l2,
.digital_9 .l2,
.digital_0 .c2 {
    animation: changeDigital 200ms 0ms 1 ease-in forwards;
}

@keyframes changeDigital {
    form {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

.clock {
  display: flex;
  justify-content: center;
  padding-top: 20px;
}
.gap {
  height: 46px;
  padding-left: 10px;
  font-size: 50px;
  font-weight: bold;
  line-height: 0.8;
  transform: skew(-6deg);
}

結構:

<div class="clock">
  <div class="digital digital_0">
    <span class="c1"></span>
    <span class="c2"></span>
    <span class="c3"></span>
    <span class="l1"></span>
    <span class="l2"></span>
    <span class="r1"></span>
    <span class="r2"></span>
  </div>
  <div class="digital digital_0">
    <span class="c1"></span>
    <span class="c2"></span>
    <span class="c3"></span>
    <span class="l1"></span>
    <span class="l2"></span>
    <span class="r1"></span>
    <span class="r2"></span>
  </div>
  <div class="gap">:</div>
  <div class="digital digital_0">
    <span class="c1"></span>
    <span class="c2"></span>
    <span class="c3"></span>
    <span class="l1"></span>
    <span class="l2"></span>
    <span class="r1"></span>
    <span class="r2"></span>
  </div>
  <div class="digital digital_0">
    <span class="c1"></span>
    <span class="c2"></span>
    <span class="c3"></span>
    <span class="l1"></span>
    <span class="l2"></span>
    <span class="r1"></span>
    <span class="r2"></span>
  </div>
  <div class="gap">:</div>
  <div class="digital digital_0">
    <span class="c1"></span>
    <span class="c2"></span>
    <span class="c3"></span>
    <span class="l1"></span>
    <span class="l2"></span>
    <span class="r1"></span>
    <span class="r2"></span>
  </div>
  <div class="digital digital_0">
    <span class="c1"></span>
    <span class="c2"></span>
    <span class="c3"></span>
    <span class="l1"></span>
    <span class="l2"></span>
    <span class="r1"></span>
    <span class="r2"></span>
  </div>
</div>

JS:

var s1 = document.getElementsByClassName("digital")[5],
    s2 = document.getElementsByClassName("digital")[4],
    m1 = document.getElementsByClassName("digital")[3],
    m2 = document.getElementsByClassName("digital")[2],
    h1 = document.getElementsByClassName("digital")[1],
    h2 = document.getElementsByClassName("digital")[0];

setInterval(function(){
  var date = new Date();
  var h = date.getHours();
  var m = date.getMinutes();
  var s = date.getSeconds();
  if (h < 10) {
    h1.className = 'digital digital_' + h;
    h2.className = 'digital digital_0';
  } else {
    h1.className = 'digital digital_' + parseInt(h%10);
    h2.className = 'digital digital_' + parseInt(h/10);
  }
  if (m < 10) {
    m1.className = 'digital digital_' + m;
    m2.className = 'digital digital_0';
  } else {
    m1.className = 'digital digital_' + parseInt(m%10);
    m2.className = 'digital digital_' + parseInt(m/10);
  }
  if (s < 10) {
    s1.className = 'digital digital_' + s;
    s2.className = 'digital digital_0';
  } else {
    s1.className = 'digital digital_' + parseInt(s%10);
    s2.className = 'digital digital_' + parseInt(s/10);
  }
}, 1000)

這樣一個簡易的 LED 數字時鐘就大功告成啦。

相關文章
相關標籤/搜索