欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS+CSS实现滚动数字时钟效果

程序员文章站 2022-11-29 20:28:53
本篇内容我们用js配合css样式表来实现一个滚动时钟的动画效果,这个效果一般都是用在网页开场动画以及区域的时间显示中,非常好看,动画效果也很个性,我们先来看下运行后的效果:...

本篇内容我们用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用来控制数字的大小等内容。

本篇内容是的原创,转载请留网址,感谢你对的支持。