首頁  >  文章  >  web前端  >  在JS+CSS中如何實現滾動數位時鐘

在JS+CSS中如何實現滾動數位時鐘

亚连
亚连原創
2018-06-15 16:08:491762瀏覽

這篇文章教大家用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: &#39;Roboto Condensed&#39;, 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: &#39;:&#39;;}
.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>
<p class="column">
 <p class="num">0</p>
 <p class="num">1</p>
 <p class="num">2</p>
</p>
<p class="column">
 <p class="num">0</p>
 <p class="num">1</p>
 <p class="num">2</p>
 <p class="num">3</p>
 <p class="num">4</p>
 <p class="num">5</p>
 <p class="num">6</p>
 <p class="num">7</p>
 <p class="num">8</p>
 <p class="num">9</p>
</p>
<p class="colon"></p>
<p class="column">
 <p class="num">0</p>
 <p class="num">1</p>
 <p class="num">2</p>
 <p class="num">3</p>
 <p class="num">4</p>
 <p class="num">5</p>
</p>
<p class="column">
 <p class="num">0</p>
 <p class="num">1</p>
 <p class="num">2</p>
 <p class="num">3</p>
 <p class="num">4</p>
 <p class="num">5</p>
 <p class="num">6</p>
 <p class="num">7</p>
 <p class="num">8</p>
 <p class="num">9</p>
</p>
<p class="colon"></p>
<p class="column">
 <p class="num">0</p>
 <p class="num">1</p>
 <p class="num">2</p>
 <p class="num">3</p>
 <p class="num">4</p>
 <p class="num">5</p>
</p>
<p class="column">
 <p class="num">0</p>
 <p class="num">1</p>
 <p class="num">2</p>
 <p class="num">3</p>
 <p class="num">4</p>
 <p class="num">5</p>
 <p class="num">6</p>
 <p class="num">7</p>
 <p class="num">8</p>
 <p class="num">9</p>
</p>
<script>
&#39;use strict&#39;;
var size = 86;
var columns = Array.from(document.getElementsByClassName(&#39;column&#39;));
var d = undefined,
 c = undefined;
var classList = [&#39;visible&#39;, &#39;close&#39;, &#39;far&#39;, &#39;far&#39;, &#39;distant&#39;, &#39;distant&#39;];
var use24HourClock = true;
function padClock(p, n) {
	return p + (&#39;0&#39; + n).slice(-2);
}
function getClock() {
	d = new Date();
	return [use24HourClock ? d.getHours() : d.getHours() % 12 || 12, d.getMinutes(), d.getSeconds()].reduce(padClock, &#39;&#39;);
}
function getClass(n, i2) {
	return classList.find(function (class_, classIndex) {
		return i2 - classIndex === n || i2 + classIndex === n;
	}) || &#39;&#39;;
}

var loop = setInterval(function () {
	c = getClock();

	columns.forEach(function (ele, i) {
		var n = +c[i];
		var offset = -n * size;
		ele.style.transform = &#39;translateY(calc(50vh + &#39; + offset + &#39;px - &#39; + size / 2 + &#39;px))&#39;;
		Array.from(ele.children).forEach(function (ele2, i2) {
			ele2.className = &#39;num &#39; + 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: &#39;Roboto Condensed&#39;, 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: &#39;:&#39;;}
.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用來控制數字的大小等內容。

上面是我整理給大家的,希望今後對大家有幫助。

相關文章:

javascript該如何實現取得日期段內每天不同的價格

在vue中如何實作圖片loading元件

為什麼Node.js會成為網頁應用程式開發?

在Node.js中如何取得檔案上傳進度?

在javascript中如何實作最長公共子序列

#關於vue如何實作動態載入圖片src

以上是在JS+CSS中如何實現滾動數位時鐘的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn