index.html
<div id="clock" class="light">
<div class="display">
<div class="weekdays"></div>
<div class="ampm"></div>
<div class="alarm"></div>
<div class="digits"></div>
</div>
</div>
主元素为#clock的div,包含.display的div,用于容纳平日列表、AM/PM标记、闹铃和时间。 下面代码为每个数字生成一个标签:
<div class="zero">
<span class="d1"></span>
<span class="d2"></span>
<span class="d3"></span>
<span class="d4"></span>
<span class="d5"></span>
<span class="d6"></span>
<span class="d7"></span>
</div>
.digits元素包含6个像这样带span的div,每个div为时钟的一个数字。就像你在上面片段中所见到的一样,这些div拥有一个从0到9的样式名称,并且包含7个带独立样式的span元素,这些span是数字的一部分,像老的数字时钟一样:
数字说明
它们完全用CSS样式渲染且默认设置为 opacity:0 。定义在它们父div上的样式将决定它们的可见性。下面是数字“0”的CSS:
assets/css/styles.css
/* 0 */
#clock .digits div.zero .d1,
#clock .digits div.zero .d3,
#clock .digits div.zero .d4,
#clock .digits div.zero .d5,
#clock .digits div.zero .d6,
#clock .digits div.zero .d7{
opacity:1
}
除了中间一个,所有的片断都是可见的,我已经向所有的这些span添加了CSS3转换属性,当在数字之间切换时出现渐变效果。
样式表里有很多其他CSS,我不再这列举。我相信最好的方式去学习CSS如何工作就是在Firebug、Chrome的审查器或你浏览器里的开发者工具里即时审查demo的代码。
黑色主题
jQuery 代码
要想要时钟工作,我们将使用jQuery生成每个数字的标签,并且设置一个定时器每秒钟更新一次样式,为了更简单,我们使用moment.js 库(快速开始) 来补偿JavaScript原生日期和时间方法的缺陷。
assets/js/script.js
$(function(){
// Cache some selectors
var clock = $('#clock'),
alarm = clock.find('.alarm'),
ampm = clock.find('.ampm')
// Map digits to their names (this will be an array)
var digit_to_name = 'zero one two three four five six seven eight nine'.split(' ')
// This object will hold the digit elements
var digits = {}
// Positions for the hours, minutes, and seconds
var positions = [
'h1', 'h2', ':', 'm1', 'm2', ':', 's1', 's2'
]
// Generate the digits with the needed markup,
// and add them to the clock
var digit_holder = clock.find('.digits')
$.each(positions, function(){
if(this == ':'){
digit_holder.append('<div class="dots">')
}
else{
var pos = $('<div>')
for(var i=1i<8i++){
pos.append('<span class="d' + i + '">')
}
// Set the digits as key:value pairs in the digits object
digits[this] = pos
// Add the digit elements to the page
digit_holder.append(pos)
}
})
// Add the weekday names
var weekday_names = 'MON TUE WED THU FRI SAT SUN'.split(' '),
weekday_holder = clock.find('.weekdays')
$.each(weekday_names, function(){
weekday_holder.append('<span>' + this + '</span>')
})
var weekdays = clock.find('.weekdays span')
// Run a timer every second and update the clock
(function update_time(){
// Use moment.js to output the current time as a string
// hh is for the hours in 12-hour format,
// mm - minutes, ss-seconds (all with leading zeroes),
// d is for day of week and A is for AM/PM
var now = moment().format("hhmmssdA")
digits.h1.attr('class', digit_to_name[now[0]])
digits.h2.attr('class', digit_to_name[now[1]])
digits.m1.attr('class', digit_to_name[now[2]])
digits.m2.attr('class', digit_to_name[now[3]])
digits.s1.attr('class', digit_to_name[now[4]])
digits.s2.attr('class', digit_to_name[now[5]])
// The library returns Sunday as the first day of the week.
// Stupid, I know. Lets shift all the days one position down,
// and make Sunday last
var dow = now[6]
dow--
// Sunday!
if(dow <0){
// Make it last
dow = 6
}
// Mark the active day of the week
weekdays.removeClass('active').eq(dow).addClass('active')
// Set the am/pm text:
ampm.text(now[7]+now[8])
// Schedule this function to be run again in 1 sec
setTimeout(update_time, 1000)
})()
// Switch the theme
$('a.button').click(function(){
clock.toggleClass('light dark')
})
})
<!DOCTYPE html><html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<title>技能训练4-5</title>
<style>
div.wrap{
margin: 0 auto
width:400px
height:110px
border:1px solid gray
}
div.wrap>#b{
text-align:center
font:bolder 30px '宋体'
color:red
background-color:gray
height:40px
}
div.wrap>#c{
text-align:center
font:normal 25px '黑体'
}
</style>
<script>
onload=function(){
var c=document.getElementById("c")
setInterval(function(){
var now=new Date
var y=now.getFullYear()
var M=now.getMonth()+1
var d=now.getDate()
var h=now.getHours()
var m=now.getMinutes()
var s=now.getSeconds()
var e=now.getDay()
var w
if(h<12){
w="上午"
}else if(w<18){
w="下午"
}else{
w="晚上"
}
h=h>12?h-12:h
h=h<10?"0"+h:h
m=m<10?"0"+m:m
s=s<10?"0"+s:s
c.innerHTML=y+"年"+M+"月"+d+"日 星期"+"日一二三四五六".split("")[e]
+"<br />"+w+" "+h+":"+m+":"+s
},1000)
}
</script>
</head>
<body>
<div class="wrap">
<div id="b">
<span>我的小时钟</span>
</div>
<div id="c">
</div>
</div>
</body>
</html>
css 有个 animation 可以实现动画,仅仅是动起来,没法实现实时与系统对时(需要js)60秒跳动60次旋转360度。(可以使用linear 线性运动)
# animation:anim_mm 60s linear infinite
animation:mm 60s steps(60) infinite
@keyframes mm{
to{ transform:rotate(360deg) }
}