這個(gè)應(yīng)該是已經(jīng)有很多人做過的東西,我應(yīng)該只是算手癢,想寫一下,所以,花了點(diǎn)時(shí)間折騰了這個(gè),順便把 Dark Mode 的處理也加上了。
首先可以很明確的一點(diǎn),這個(gè)真沒技術(shù)含量存在,只是需要點(diǎn)耐心。
LED 數(shù)字包含了左右各兩條線,中間三條線,一共 7 條線。所以,為了能夠更容易辨識(shí),在寫 demo 的時(shí)候,我直接這樣寫了。
<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
這個(gè)的作用,其實(shí)很簡單,就是控制從 0 ~ 9 這十個(gè)數(shù)字的變化而存在的。
.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;}
這里使用了一個(gè) animation 動(dòng)畫,并且時(shí)間是 200ms
,主要是為了讓某些部分消失的時(shí)候,有一個(gè)過渡效果。
@keyframes changeDigital { form { opacity: 1; } to { opacity: 0; }}
那么剩下來的就是最需要耐心的地方了,調(diào)整控制那 7 條線的位置,同時(shí)還要考慮每條線是帶有斜角的,而且還稍微有點(diǎn)圓弧的感覺。帶點(diǎn)圓弧的感覺那么直接使用 border-radius
就可以了,至于那個(gè)斜角的話,如果各位知道 CSS 中用邊框畫三角的方法,那么就應(yīng)該明白怎么做這個(gè)斜角了。
當(dāng)我們要畫三角的時(shí)候,width
和 height
都是為 0
,然后用過控制 border-width
而得到最終的三角的大小,那么這里如果我們根據(jù)線條的位置,適當(dāng)選擇 width
和 height
有具體的值,是不是就有斜角的感覺了呢。
接著,需要注意橫著的中間那根線,也就是 .c2
這個(gè)元素,線條兩邊是三角突出的,可以實(shí)現(xiàn)的方式也很多,這里我選擇使用 :after
和 :before
相疊加。
最后就是對(duì)每個(gè)元素的位置做調(diào)整,控制好大小位置就可以了,因?yàn)槭鞘褂枚ㄎ坏姆绞絹聿僮?,所以,調(diào)整起來還是很簡單的。
.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;}
做完這些,剩下的就是簡單的對(duì)外層元素做點(diǎn)優(yōu)化,比如稍微傾斜一點(diǎn)。
.digital { position: relative; width: 26px; height: 46px; margin-left: 10px; transform: skew(-6deg);}
哦,忘了說了,這里的邊框顏色我選擇使用 currentColor
的方式,這樣可以在暗黑模式切換的過程中,只需要修改 body
中的文字顏色和背景色就可以了。currentColor
會(huì)選擇文字顏色來使用。
樣式處理完之后,接著就是添加點(diǎn) JS 來顯示時(shí)鐘了,只要把兩個(gè)數(shù)字整出來放到不同的 div
中就好了。這里我取個(gè)位數(shù)的時(shí)候,是用 m%10
的方式取余;取十位數(shù)的時(shí)候是通過 parseInt(m/10)
的方式取整。取出來之后分別放到想對(duì)應(yīng)的 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);}
結(jié)構(gòu):
<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)
這樣一個(gè)簡易的 LED 數(shù)字時(shí)鐘就大功告成啦。
聯(lián)系客服