学完了transform这个课程,还是来一个案例吧,利用transform的旋转rotate打造一个时钟,再结合Javascript的定时器让它走起来。
截一个动图:

案例知识点分析:
1、利用定位完成时钟的绘制。
2、背景使用了放射性渐变。
3、利用Javascript完成刻度和时间数字的旋转。
4、利用Date()对象获取系统时间,并让时针指向对应的刻度。
5、利用定时器不断更新时间,完成时针的运动。
一、HTML源代码
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
二、CSS样式
body,ul{ margin:0; padding:0;} body{ font:1em "microsoft Yahei"; color:#666; background-color:#333;} h1{ text-align:center; color:#eee; font-size:3rem;} li{ list-style:none;} p{ text-align:center; color:#ddd; position:relative; top:100px; } a{ color:#999; text-decoration:none; transition:0.2s;} a:hover{ color:#ddd;} #clock-wrap{ width:400px; height:400px; border:10px solid #fff; border-radius:50%; margin:80px auto 0; position:relative; box-shadow:0 0 40px rgba(0,0,0,1)} #clock ul{ width:400px; height:400px; position:relative; border-radius:50%; background:radial-gradient(circle at center,#667eea,#764ba2); box-shadow:0 0 50px rgba(0,0,0,0.5) inset; } #clock ul li{ position:absolute; left:50%; margin-left:-2px; top:0; width:4px; height:10px; background:rgba(255,255,255,.5); transform-origin:center 200px; } #clock li:nth-child(5n+1){ height:18px; } #num{ position:absolute; width:360px; height:360px; left:0; right:0; top:0; bottom:0; margin:auto; } #num li{ position:absolute; left:50%; margin-left:-10px; top:0; color:rgba(255,255,255,.5); font:2em Arial, Helvetica, sans-serif; transform-origin:center 180px;} #hour,#min,#sec{ background:#fff; position:absolute; left:50%; top:50%; transform-origin:bottom; box-shadow:0 0 6px rgba(0,0,0,.5) } #hour{ width:14px; height:100px; margin-left:-7px; margin-top:-100px; border-radius:3px; } #min{ width:10px; height:150px; margin-left:-5px; margin-top:-150px; border-radius:2px; } #sec{ width:4px; height:180px; margin-left:-2px; margin-top:-180px; border-radius:1px; } #circle{ width:40px; height:40px; border-radius:50%; background:#fff; position:absolute; left:50%; margin-left:-20px; top:50%; margin-top:-20px; box-shadow:0 0 20px rgba(0,0,0,.4)}
三、Javascript代码
以上就是css3 利用transform打造走动的2D时钟的详细内容,更多关于CSS3 transform的资料请关注考高分网其它相关文章!


