在網頁設計中,我們經常需要獲取時分秒的信息,這一功能可以通過CSS來實現。下面是一段CSS代碼示例,可以獲取當前時間的時、分、秒:
.clock { color: #333; font-size: 24px; width: 200px; height: 60px; line-height: 60px; text-align: center; border: 1px solid #ccc; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); } .clock::before { content: attr(data-time); position: absolute; top: 0; } .clock::after { content: ''; position: absolute; width: 10px; height: 10px; border-radius: 50%; background: #333; top: 50%; left: 50%; transform: translate(-50%, -50%); animation: blink 1s infinite alternate; } @keyframes blink { from {opacity: 1;} to {opacity: 0;} } .clock[data-time^="0"]::before { content: attr(data-time); } .clock[data-time^="1"]::before { content: attr(data-time); } .clock[data-time="00:00:00"]::after { display: none; } .clock[data-time^="0"]::before, .clock[data-time^="1"]::before { font-size: 40px; color: tomato; font-weight: 700; transition: all .2s ease; letter-spacing: .1em; text-transform: uppercase; } .clock[data-time^="0"]::before:active, .clock[data-time^="1"]::before:active { transform: scale(1.1); }
上述代碼中,我們使用了偽元素::before和::after來獲取時間的值,通過attr()來獲取data-time屬性中存儲的時分秒信息。同時,還可以通過添加類名來對時、分、秒進行不同的樣式設置,實現更多的個性化效果。