第二问:用Javascript编写一个Clock类,实现如下接口:1.构造函数Clock(HTMLDomElement dom) :在参数dom中生成上述时钟
2. setTime(hour,minute,second) :设置时钟时间,指针指定到正确位置
3. run()时钟开始工作 可以使用jQuery等任何前端框架
<div id="clock"></div>
<style>
*{ padding:0; margin:0}
.clock{ background-image:url('clock.png'); width:750px;height:728px; background-position:-202px -68px;background-repeat:no-repeat; position:relative }
.clock p{ position:absolute; top:50%;left:50%;background-image:url('clock.png'); background-repeat:no-repeat}
.clock p:first-child{background-position:-25px -465px; width:31px; height:480px; margin-top:-240px; margin-left:-15px;}
.clock p:nth-child(2){background-position:-74px -499px; width:27px; height:412px; margin-top:-206px; margin-left:-12px;}
.clock p:last-child{background-position:-120px -558px; width:31px; height:294px; margin-top:-147px; margin-left:-15px;}
</style>
<script>
function Clock(dom){
this.dom=dom;
this.hour=0;
this.minute=0;
this.second=0;
}
Clock.prototype.render=function(){
var elem = this.dom;
elem.setAttribute('class','clock');
elem.setAttribute('className','clock');
elem.innerHTML='<p></p><p></p><p></p>';
}
Clock.prototype.setCurrentTime=function(hour,minute,second){
this.hour=hour>12?hour-12:hour;
this.minute=minute;
this.second=second;
}
Clock.prototype.run=function(){
var ps = this.dom.getElementsByTagName('p');
that=this;
var timer = setInterval(function(){
var s1=that.second*6,
s2=(that.minute*60+that.second)*6/60,
s3=(that.hour*3600+that.minute*60+that.second)*30/3600;
ps[0].style.transform='rotate('+(s1+6)+'deg)';
ps[1].style.transform='rotate('+(s2+1/60)+'deg)';
ps[2].style.transform='rotate('+(s3+1/3600)+'deg)';
if(that.second<60){
that.second++;
}else{
that.second=0;
if(that.minute<60){
that.minute++;
}else{
that.minute=0;
if(that.second<12){
that.hour++;
}else{
that.hour=0;
}
}
}
},1000);
}
var dom = document.getElementById('clock');
var thisClock = new Clock(dom);
thisClock.render();
thisClock.run();
thisClock.setCurrentTime(16,58,58);
</script>
写了半天,效果出来了,不知道有没有简便方法,火狐下看效果
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>时钟</title>
<style type="text/css">
html,body,div{
margin: 0;
padding: 0;
}
.clock-wrap{
width: 300px;
height: 300px;
margin:20px auto;
background: url("clock1.png") -75px -20px no-repeat;
position: relative;
}
.clock-wrap>div{
position: absolute;
width: 12px;
height: 100px;
background-color: #ccc;
top:80px;
left: 50%;
margin-left: -1px;
/* -webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;*/
}
#sec{
top:60px;
height: 128px;
background: url('clock1.png') -10px -182px no-repeat;
-webkit-tran敏感词orm-origin: center 94px;
-moz-tran敏感词orm-origin: center 94px;
-ms-tran敏感词orm-origin: center 94px;
-o-tran敏感词orm-origin: center 94px;
tran敏感词orm-origin: center 94px;
-webkit-tran敏感词orm: rotateZ(30deg);
-moz-tran敏感词orm: rotateZ(30deg);
-ms-tran敏感词orm: rotateZ(30deg);
-o-tran敏感词orm: rotateZ(30deg);
tran敏感词orm: rotateZ(30deg);
}
#min{
height: 88px;
background: url('clock1.png') -28px -202px no-repeat;
-webkit-tran敏感词orm-origin: center 75px;
-moz-tran敏感词orm-origin: center 75px;
-ms-tran敏感词orm-origin: center 75px;
-o-tran敏感词orm-origin: center 75px;
tran敏感词orm-origin: center 75px;
-webkit-tran敏感词orm: rotateZ(145deg);
-moz-tran敏感词orm: rotateZ(145deg);
-ms-tran敏感词orm: rotateZ(145deg);
-o-tran敏感词orm: rotateZ(145deg);
tran敏感词orm: rotateZ(145deg);
}
#hour{
height: 84px;
background: url('clock1.png') -47px -202px no-repeat;
-webkit-tran敏感词orm-origin: center 75px;
-moz-tran敏感词orm-origin: center 75px;
-ms-tran敏感词orm-origin: center 75px;
-o-tran敏感词orm-origin: center 75px;
tran敏感词orm-origin: center 75px;
-webkit-tran敏感词orm: rotateZ(60deg);
-moz-tran敏感词orm: rotateZ(60deg);
-ms-tran敏感词orm: rotateZ(60deg);
-o-tran敏感词orm: rotateZ(60deg);
tran敏感词orm: rotateZ(60deg);
}
</style>
</head>
<script type="text/javascript">
var Clock=function(json){
this.hour=json.hour;
this.min=json.min;
this.sec=json.sec;
}
Clock.prototype.run=function(){
var clock=this;
var date=new Date();
var hh=date.getHours();
var mm=date.getMinutes();
var ss=date.getSeconds();
clock.setTime(hh,mm,ss);
setInterval(function(){
date=new Date();
hh=date.getHours();
mm=date.getMinutes();
ss=date.getSeconds();
clock.setTime(hh,mm,ss);
},1e3);
}
Clock.prototype.setTime=function(h,m,s){
var trans=['webkitTran敏感词orm','msTran敏感词orm','mozTran敏感词orm','oTran敏感词orm','tran敏感词orm'];
for(var i=0;i<trans.length;i++){
this.hour.style[trans[i]]='rotateZ('+((h/12)*360)+'deg)';
this.min.style[trans[i]]='rotateZ('+((m/60)*360)+'deg)';
this.sec.style[trans[i]]='rotateZ('+((s/60)*360)+'deg)';
}
}
window.onload=function(){
var oH=document.getElementById('hour');
var oM=document.getElementById('min');
var oS=document.getElementById('sec');
var c1=new Clock({
hour:oH,
min:oM,
sec:oS
});
c1.run();
}
</script>
<body>
<div class="clock-wrap">
<div id="hour"></div>
<div id="min"></div>
<div id="sec"></div>
</div>
</body>
</html>
//css
div.clock,div.pointor-h,div.pointor-m,div.pointor-s{
background:url(clock.png) no-repeat scroll;
}
div.clock{
width:770px;
height:750px;
background-position:-190px -67px;
}
div.pointor-h,div.pointor-m,div.pointor-s{
width:33px;
}
div.pointor-h{
background-position:-119px -557px;
height:300px;
position:relative;
left:370px;
top:220px;
}
div.pointor-m{
background-position:-70px -500px;
height:410px;
position:relative;
left:370px;
top:-138px;
}
div.pointor-s{
background-position:-24px -460px;
height:490px;
position:relative;
left:371px;
top:-590px;
}
//
function Clock(DOM){
this._clock = document.createElement("div"),
this._pointor_h = document.createElement("div"),
this._pointor_m = document.createElement("div"),
this._pointor_s = document.createElement("div");
this._clock.className = "clock";
this._pointor_h.className = "pointor-h";
this._pointor_m.className = "pointor-m";
this._pointor_s.className = "pointor-s";
this._clock.appendChild(this._pointor_h);
this._clock.appendChild(this._pointor_m);
this._clock.appendChild(this._pointor_s);
if(DOM){
DOM.appendChild(this._clock)
}
}
Clock.prototype.getHourse = function(){return this.hour}
Clock.prototype.getMinute = function(){return this.minute}
Clock.prototype.getSecond = function(){return this.second}
Clock.prototype.getTime = function(){return this.hour+":"+this.minute+":"+this.second}
Clock.prototype.refresh=function (){
this._pointor_h.style.transform = "rotate("+(((this.hour%12)*30)+(this.minute*30/60)+(this.second*30/3600)).toFixed(2)+"deg";
this._pointor_m.style.transform = "rotate("+((this.minute*6)+(this.second*6/60)).toFixed(2)+"deg)";
this._pointor_s.style.transform = "rotate("+(this.second*6).toFixed(2)+"deg)";
}
Clock.prototype.setTime = function(h,m,s){
this.hour =h;
this.minute=m;
this.second=s;
this.refresh();
}
Clock.prototype.run = function(){
var self=this;
setInterval(function(){
if(++self.second==60){
self.second=0;
if(++self.minute==60){
self.minute=0;
if(++self.hour==24){
self.hour=0;
}
}
}
self.refresh();
},1000)
}