您好,登錄后才能下訂單哦!
這篇文章主要介紹了JS+CSS怎么實現滾動數字時鐘效果,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
用js配合css樣式表來實現一個滾動時鐘的動畫效果,這個效果一般都是用在網頁開場動畫以及區域的時間顯示中,非常好看,動畫效果也很個性,我們先來看下運行后的效果:
下面就是這個滾動時鐘動畫的全部代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>CSS3+JS滾動數字時鐘代碼-億速云</title> <style> body{text-align: center;background-color: #0e141b;color: rgba(224, 230, 235, 0.89);font-family: 'Roboto Condensed', sans-serif;font-weight: 300;overflow: hidden;} .column,.colon{display: inline-block;vertical-align: top;font-size: 86px;line-height: 86px;} .column{-webkit-transition: -webkit-transform 300ms;transition: -webkit-transform 300ms;transition: transform 300ms;transition: transform 300ms, -webkit-transform 300ms;} .colon{-webkit-transition: -webkit-transform 300ms;transition: -webkit-transform 300ms;transition: transform 300ms;transition: transform 300ms, -webkit-transform 300ms;-webkit-transform: translateY(calc(50vh - 43px));transform: translateY(calc(50vh - 43px));} .colon:after{content: ':';} .num{-webkit-transition: opacity 500ms, text-shadow 100ms;transition: opacity 500ms, text-shadow 100ms;opacity: 0.025;} .num.visible{opacity: 1.0;text-shadow: 1px 1px 0px #336699;} .num.close{opacity: 0.35;} .num.far{opacity: 0.15;} .num.distant{opacity: 0.1;} </style> </head> <body> <div class="column"> <div class="num">0</div> <div class="num">1</div> <div class="num">2</div> </div> <div class="column"> <div class="num">0</div> <div class="num">1</div> <div class="num">2</div> <div class="num">3</div> <div class="num">4</div> <div class="num">5</div> <div class="num">6</div> <div class="num">7</div> <div class="num">8</div> <div class="num">9</div> </div> <div class="colon"></div> <div class="column"> <div class="num">0</div> <div class="num">1</div> <div class="num">2</div> <div class="num">3</div> <div class="num">4</div> <div class="num">5</div> </div> <div class="column"> <div class="num">0</div> <div class="num">1</div> <div class="num">2</div> <div class="num">3</div> <div class="num">4</div> <div class="num">5</div> <div class="num">6</div> <div class="num">7</div> <div class="num">8</div> <div class="num">9</div> </div> <div class="colon"></div> <div class="column"> <div class="num">0</div> <div class="num">1</div> <div class="num">2</div> <div class="num">3</div> <div class="num">4</div> <div class="num">5</div> </div> <div class="column"> <div class="num">0</div> <div class="num">1</div> <div class="num">2</div> <div class="num">3</div> <div class="num">4</div> <div class="num">5</div> <div class="num">6</div> <div class="num">7</div> <div class="num">8</div> <div class="num">9</div> </div> <script> 'use strict'; var size = 86; var columns = Array.from(document.getElementsByClassName('column')); var d = undefined, c = undefined; var classList = ['visible', 'close', 'far', 'far', 'distant', 'distant']; var use24HourClock = true; function padClock(p, n) { return p + ('0' + n).slice(-2); } function getClock() { d = new Date(); return [use24HourClock ? d.getHours() : d.getHours() % 12 || 12, d.getMinutes(), d.getSeconds()].reduce(padClock, ''); } function getClass(n, i2) { return classList.find(function (class_, classIndex) { return i2 - classIndex === n || i2 + classIndex === n; }) || ''; } var loop = setInterval(function () { c = getClock(); columns.forEach(function (ele, i) { var n = +c[i]; var offset = -n * size; ele.style.transform = 'translateY(calc(50vh + ' + offset + 'px - ' + size / 2 + 'px))'; Array.from(ele.children).forEach(function (ele2, i2) { ele2.className = 'num ' + getClass(n, i2); }); }); }, 200 + Math.E * 10); </script> </body> </html>
注意:其中最上面的是CSS樣式內容,大家可以在其中進行調整顏色字體等
<style> body{text-align: center;background-color: #0e141b;color: rgba(224, 230, 235, 0.89);font-family: 'Roboto Condensed', sans-serif;font-weight: 300;overflow: hidden;} .column,.colon{display: inline-block;vertical-align: top;font-size: 86px;line-height: 86px;} .column{-webkit-transition: -webkit-transform 300ms;transition: -webkit-transform 300ms;transition: transform 300ms;transition: transform 300ms, -webkit-transform 300ms;} .colon{-webkit-transition: -webkit-transform 300ms;transition: -webkit-transform 300ms;transition: transform 300ms;transition: transform 300ms, -webkit-transform 300ms;-webkit-transform: translateY(calc(50vh - 43px));transform: translateY(calc(50vh - 43px));} .colon:after{content: ':';} .num{-webkit-transition: opacity 500ms, text-shadow 100ms;transition: opacity 500ms, text-shadow 100ms;opacity: 0.025;} .num.visible{opacity: 1.0;text-shadow: 1px 1px 0px #336699;} .num.close{opacity: 0.35;} .num.far{opacity: 0.15;} .num.distant{opacity: 0.1;} </style>
感謝你能夠認真閱讀完這篇文章,希望小編分享的“JS+CSS怎么實現滾動數字時鐘效果”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。