• 技术文章 >web前端 >css教程

    css3+js绘制动态时钟(附代码)

    青灯夜游青灯夜游2018-09-20 12:52:51原创3728
    本章给大家介绍如何使用css3与js实现动态时钟效果,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    先看看效果图:

    1.jpg

    首先,思考了一下页面的布局,大致需要4层div,最底层是一个表盘的背景图,然后其余3层分别是时针,分针,秒针的图层.

    html代码如下:

    <div class="dial">
    </div>
    <div class="bigdiv bigdiv1" id="secondHand">
        <div class="secondHand"></div>
    </div>
    <div class="bigdiv bigdiv2" id="minuteHand">
        <div class="minuteHand"></div>
    </div>
    <div class="bigdiv bigdiv3" id="hourHand">
        <div class="center"></div>
        <div class="hourHand"></div>
    </div>

    变量名是随便起的,不要介意; class=center的这个div是表中心那个小黑点.

    时针是60*60*60s转一圈, 分针是60*60s转一圈, 秒针是60s转一圈, 所以css代码如下:

    .dial{
        width:600px;
        height:600px;
        margin:0 auto;
        position: absolute;
        border-radius: 50%;
        overflow: hidden;
        background-color: rgba(153,50,204,0.2);
        background-image: url(img/表盘.jpg);
        background-size: 100% 100%;
    }
    .bigdiv{
        width:600px;
        height:600px;
        margin:0 auto;
        position: absolute;
        border-radius: 50%;
        overflow: hidden;
    }
    .bigdiv>div{
        position: absolute;
        left:298px;
        border-radius: 100px;
    }
    .bigdiv1{
        animation: moves 60s steps(60) infinite;
    }
    .bigdiv1 .secondHand{
        width:4px;
        height:250px;
        background-color: red;
        top:50px;
        left:298px;
    }
    .bigdiv2{
        animation: moves 3600s steps(3600) infinite;
    }
    .bigdiv2 .minuteHand{
        width:6px;
        height:180px;
        background-color: green;
        top:120px;
        left:297px;
    }
    .bigdiv3{
        animation: moves 216000s steps(216000) infinite;
    }
    .bigdiv3 .hourHand{
        width:8px;
        height:160px;
        background-color: orange;
        top:140px;
        left:296px;
        border-radius: 100px;
    }
    .bigdiv .center{
        top:290px;
        left:290px;
        width:20px;
        height:20px;
        background-color: black;
        z-index: 2;
    }
    @keyframes moves{
        from{ transform: rotateZ(0deg); }
        to{ transform: rotateZ(360deg); }
    }

    这一步做完后效果图是这个样子的:

    2.png

    然后用js计算当前时间,

    var date = new Date();
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();

    然后计算当前每个针的旋转角度

    var secondAngle = seconds;
    var minuteAngle = minutes * 60 + seconds;
    var hourAngle = (60/12) * ((hours%12) * 3600 + minuteAngle);

    现在的思路就是:每个针会按照自己定的时间转一圈,初始角度也能知道,怎么组成一个显示当前时间的动态钟表呢?

    刚开始的想法是让这3层div旋转对应的角度,然后再开始,后来一想不行,因为它还是固定的时间旋转一周,指针指向会有偏差,

    现在需要的是页面进来的第一圈旋转固定角度,其余的按照原来固定的时间旋转一周就行了,

    css3里面有一个animation-delay属性,它表示的意思是动画延迟,负数就表示提前开始(比如-5s就表示动画从第5s的时间开始),

    刚好可以用到,让这几个指针提前开始对应的角度.

    js代码如下:

    hourHand.style.cssText = "animation-delay: -"+ hourAngle +"s";
    minuteHand.style.cssText = "animation-delay: -"+ minuteAngle +"s";
    secondHand.style.cssText = "animation-delay: -"+ secondAngle +"s";

    最后自己再加了个动态时间在钟表的上面展示

    完整代码:

    <!DOCTYPE html>
    <html>
    
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    		<style>
    			body,
    			html {
    				margin: 0;
    			}
    			
    			.location {
    				position: relative;
    				width: 600px;
    				height: 600px;
    				left: calc(50% - 300px);
    			}
    			
    			.dial {
    				width: 600px;
    				height: 600px;
    				margin: 0 auto;
    				position: absolute;
    				border-radius: 50%;
    				overflow: hidden;
    				background-color: rgba(153, 50, 204, 0.2);
    				background-image: url(img/表盘.jpg);
    				background-size: 100% 100%;
    			}
    			
    			.bigdiv {
    				width: 600px;
    				height: 600px;
    				margin: 0 auto;
    				position: absolute;
    				border-radius: 50%;
    				overflow: hidden;
    			}
    			
    			.bigdiv>div {
    				position: absolute;
    				left: 298px;
    				border-radius: 100px;
    			}
    			
    			.bigdiv1 {
    				animation: moves 60s steps(60) infinite;
    			}
    			
    			.bigdiv1 .secondHand {
    				width: 4px;
    				height: 250px;
    				background-color: red;
    				top: 50px;
    				left: 298px;
    			}
    			
    			.bigdiv2 {
    				animation: moves 3600s steps(3600) infinite;
    			}
    			
    			.bigdiv2 .minuteHand {
    				width: 6px;
    				height: 180px;
    				background-color: green;
    				top: 120px;
    				left: 297px;
    			}
    			
    			.bigdiv3 {
    				animation: moves 216000s steps(216000) infinite;
    			}
    			
    			.bigdiv3 .hourHand {
    				width: 8px;
    				height: 160px;
    				background-color: orange;
    				top: 140px;
    				left: 296px;
    				border-radius: 100px;
    			}
    			
    			.bigdiv .center {
    				top: 290px;
    				left: 290px;
    				width: 20px;
    				height: 20px;
    				background-color: black;
    				z-index: 2;
    			}
    			
    			@keyframes moves {
    				from {
    					transform: rotateZ(0deg);
    				}
    				to {
    					transform: rotateZ(360deg);
    				}
    			}
    			
    			#dateshow {
    				text-align: center;
    			}
    		</style>
    
    	</head>
    
    	<body>
    		<h1 id="dateshow"></h1>
    		<div class="location">
    			<div class="dial"></div>
    			<div class="bigdiv bigdiv1" id="secondHand">
    				<div class="secondHand"></div>
    			</div>
    			<div class="bigdiv bigdiv2" id="minuteHand">
    				<div class="minuteHand"></div>
    			</div>
    			<div class="bigdiv bigdiv3" id="hourHand">
    				<div class="center"></div>
    				<div class="hourHand"></div>
    			</div>
    		</div>
    		<script>
    			var dateshow = document.getElementById("dateshow");
    			var clock = {
    				weeks: ["一", "二", "三", "四", "五", "六", "日"],
    				getDate: function() {
    					date = new Date();
    					year = date.getFullYear();
    					month = date.getMonth() + 1;
    					day = date.getDate();
    					hours = date.getHours();
    					minutes = date.getMinutes();
    					seconds = date.getSeconds();
    					week = date.getDay(); // 星期
    					dateText = year + "年" + month + "月" + clock.format(day) + "日 星期" + clock.formatnum(week) + " " +
    						clock.format(hours) + ":" + clock.format(minutes) + ":" + clock.format(seconds);
    					return dateText;
    				},
    				format: function(data) {
    					if(data.toString().length == 1) {
    						data = "0" + data;
    					};
    					return data;
    				},
    				formatnum: function(num) {
    					return clock.weeks[num - 1];
    				},
    				showdate: function() {
    					dateshow.innerText = clock.getDate();
    				},
    				go: function() {
    					var secondHand = document.getElementById("secondHand");
    					var minuteHand = document.getElementById("minuteHand");
    					var hourHand = document.getElementById("hourHand");
    					date = new Date();
    					hours = date.getHours();
    					minutes = date.getMinutes();
    					seconds = date.getSeconds();
    					var secondAngle = seconds;
    					var minuteAngle = minutes * 60 + seconds;
    					var hourAngle = (60 / 12) * ((hours % 12) * 3600 + minuteAngle);
    					hourHand.style.cssText = "animation-delay: -" + hourAngle + "s";
    					minuteHand.style.cssText = "animation-delay: -" + minuteAngle + "s";
    					secondHand.style.cssText = "animation-delay: -" + secondAngle + "s";
    				}
    
    			}
    			clock.go();
    			clock.showdate();
    			setInterval("clock.showdate()", 1000);
    		</script>
    	</body>
    
    </html>

    以上就是css3+js绘制动态时钟(附代码)的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:动态时钟 css3 js
    上一篇:有哪些方法可以将整个网页变成黑白色 下一篇:css如何实现table表格的单元格合并?colspan和rowspan合并单元格(实例)
    Web大前端开发直播班

    相关文章推荐

    • 基于javascript实现动态时钟效果_javascript技巧• HTML5的canvas绘制动态时钟,1秒刷新一次canvas,个人觉得有性能的问题,如果不这么做的话,有什么好的方法?• HTML制作网页动态时钟教程• canvas与JS实现动态时钟动画

    全部评论我要评论

  • 几多²⁰²⁰

    不能复制干鸡毛

    2020-10-29

  • 取消发布评论发送
  • 1/1

    PHP中文网