css怎么样制作钟表

html-css029

css怎么样制作钟表,第1张

代码如下:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />

<title>canvas实例--制作时钟</title>

</head>

<body>

<canvas id="canvas" width="500" height="500" >

您的浏览器版本太低啦!可以换了!

</canvas>

<script>

//获取canvas

var canvas = document.getElementById("canvas")

//设置环境

var cxt = canvas.getContext("2d")

//制作时钟的函数

function DrawClock() {

//清除画布

cxt.clearRect(0,0,500,500)

//获取当前时间的时,分,秒

var now = new Date()

var hour = now.getHours()

var min = now.getMinutes()

var sec = now.getSeconds()

//小时必须获取浮点型(小时+分数--->转化为的小时)

hour = hour + min / 60

//将24小时转换为12小时

hour = hour >12 ? hour - 12 : hour

//制作表盘

cxt.beginPath()

cxt.lineWidth = 10

cxt.strokeStyle = "#ABCDEF"

cxt.arc(250, 250, 200, 0, 360, false)

cxt.stroke()

cxt.closePath()

//刻度

//时针

for (var i = 0i <12i++) {

cxt.save()

cxt.lineWidth = 7

cxt.strokeStyle = "red"

//设置0,0点

cxt.translate(250, 250)

//再设置旋转角度

cxt.rotate(i * 30 * Math.PI / 180)

cxt.beginPath()

cxt.moveTo(0, -170)

cxt.lineTo(0, -190)

cxt.closePath()

cxt.stroke()

cxt.restore()

}

//分针

for (var i = 0i <60i++) {

//为避免不同颜色的重叠,

//在时针刻度与分针刻度重叠的位置,不画分针

if (i % 5 == 0) continue

cxt.save()

//设置刻度粗细

cxt.lineWidth = 5

cxt.strokeStyle = "purple"

//设置画布的0,0点

cxt.translate(250, 250)

//设置旋转角度

cxt.rotate(i * 6 * Math.PI / 180)

//画分针刻度

cxt.beginPath()

cxt.moveTo(0, -180)

cxt.lineTo(0, -190)

cxt.closePath()

cxt.stroke()

cxt.restore()

}

//时针

cxt.save()

//设置时针风格

cxt.lineWidth = 7

cxt.strokeStyle = "pink"

//设置异次元空间的0,0点

cxt.translate(250, 250)

//设置旋转角度

cxt.rotate(hour * 30 * Math.PI / 180)

cxt.beginPath()

cxt.moveTo(0, -140)

cxt.lineTo(0, 10)

cxt.closePath()

cxt.stroke()

cxt.restore()

//分针

cxt.save()

//设置分针风格

cxt.lineWidth = 5

cxt.strokeStyle = "orange"

//设置异次元空间的0,0点

cxt.translate(250, 250)

//设置旋转角度

cxt.rotate(min * 6 * Math.PI / 180)

cxt.beginPath()

cxt.moveTo(0, -160)

cxt.lineTo(0, 15)

cxt.closePath()

cxt.stroke()

cxt.restore()

//秒针

cxt.save()

//风格

cxt.strokeStyle = "yellow"

cxt.lineWidth = 3

//重置0,0点

cxt.translate(250, 250)

//设置旋转角度

cxt.rotate(sec*6*Math.PI/180)

//画图

cxt.beginPath()

cxt.moveTo(0, -170)

cxt.lineTo(0, 20)

cxt.stroke()

//画出时针,分针,秒针的交叉点

cxt.beginPath()

cxt.arc(0, 0, 5, 0, 360, false)

cxt.closePath()

//设置填充样式

cxt.fillStyle = "blue"

cxt.fill()

//设置笔触样式--->秒针已设置

cxt.stroke()

//设置秒针前端的小圆点

cxt.beginPath()

cxt.arc(0, -150, 5, 0, 360, false)

cxt.closePath()

//设置填充样式

cxt.fillStyle = "blue"

cxt.fill()

//设置笔触样式

cxt.stroke()

cxt.closePath()

cxt.restore()

}

//调用函数

DrawClock()

//设置时钟转动起来

setInterval(DrawClock, 1000)

</script>

</body>

</html>

以前做过一个 CSS 的钟表,你可以参考一下:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Title</title>

<link rel="stylesheet" type="text/css" href=""/>

<script type="text/javascript" src=""></script>

<style type="text/css">

#outer{

/*background-color:#bfa*/

width:300px

height:300px

position:relative

margin-left:300px

margin-top:100px

}

ul{

margin-left:150px

padding:0

list-style:none

}

ul li{

width:4px

height:8px

background-color:#000

position:absolute

transform-origin:2px 150px

}

ul li:nth-child(5n+1){

height:14px

}

ul li:nth-child(15n+1){

background-color:#00FF01

}

#second{

width:2pxheight:120pxbackground:goldposition:absolutetop:30px

left:150px

transform-origin:1px 100%

}

#min{

width:2pxheight:100pxbackground:#000position:absolutetop:50px

left:150px

transform-origin:1px 100%

}

#hour{

width:4pxheight:70pxbackground:#000position:absolutetop:80px

left:150px

transform-origin:2px 100%

}

</style>

<script type="text/javascript">

window.onload = function(){

var dail = document.getElementById("dail")

for(var i=0 i<60 i++){

var scale = document.createElement("li")

scale.style.transform = "rotate("+i*6+"deg)"

dail.appendChild(scale)

}

setInterval(function(){

var sec = document.getElementById("second")

var min = document.getElementById("min")

var hour = document.getElementById("hour")

var date = new Date()

var second = date.getSeconds()

var minute = date.getMinutes()+second/60

var hou = date.getHours()+minute/60

sec.style.transform = "rotate(" + second*6 + "deg)"

min.style.transform = "rotate(" + minute*6 + "deg)"

hour.style.transform = "rotate("+hou*30+"deg)"

},1000)

}

</script>

</head>

<body>

<div id="outer">

<ul id="dail"></ul>

<div id="hour"></div>

<div id="min"></div>

<div id="second"></div>

</div>

</body>

</html>