效果图
图片:
代码
xml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>clock</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="clock">
<div class="outer-clock-face">
<div class="marking marking-one"></div>
<div class="marking marking-two"></div>
<div class="marking marking-three"></div>
<div class="marking marking-four"></div>
<div class="inner-click-face">
<div class="hand hour-hand"></div>
<div class="hand min-hand"></div>
<div class="hand second-hand"></div>
</div>
</div>
</div>
<script src="./index.js"></script>
</body>
</html>
css
css
.clock{
width: 300px;
height: 300px;
border-radius: 50%;
border: 7px solid #ffebdb;
background-image: url('./clock.jpeg');
background-size: 111%;
padding: 20px;
}
.outer-clock-face{
width: 100%;
height: 100%;/*h100p+tab*/
border-radius: 100%;
position: relative;
}
/*伪元素 行内元素*/
.outer-clock-face::before,
.outer-clock-face::after
{
content: '';
display: block;
width: 10px;
height: 100%;
background-color: #596235;
border-radius: 8px;/*bdrs+tab*/
position: absolute;
left: 50%;
transform: translate(-50%);/*左移棍子的一半*/
}
.outer-clock-face::after{
transform: rotateZ(90deg);
transform-origin: center center;/*控制旋转基点在棍子的正中间*/
}
.marking{
width: 3px;
height: 100%;
background-color: #596235;
position: absolute;
left:50%;
transform: translate(-50%);
transform-origin: center center;
}
.marking-one{
transform: rotateZ(30deg);
}
.marking-two{
transform: rotateZ(60deg);
}
.marking-three{
transform: rotateZ(120deg);
}
.marking-four{
transform: rotateZ(150deg);
}
.inner-click-face{
width: 80%;
height: 80%;
border-radius: 50%;
background-color: #ffebdb;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
z-index: 1;/*默认0*/
}
.hand {
width: 50%;
height: 6px;
background-color: red;
border-radius: 6px;
position: absolute;
top: 50%;
right: 50%;
transform: translateY(-50%) rotateZ(90deg);
transform-origin: 100% center;
}
.hour-hand{
width: 30%;
}
.min-hand{
width: 40%;
height: 3px;
}
.second-hand{
width: 45%;
height: 2px;
background-color: #b3b3b3;
}
js
ini
//获取秒针
//设置秒针旋转的角度
//获取分针
//获取分针的旋转角度
//获取时针
//获取时针的旋转角度
var secondHand=document.querySelector('.second-hand');
var minHand=document.querySelector('.min-hand');
var hourHand=document.querySelector('.hour-hand');
function setTime(){
var now=new Date();//获取当前时间
//读取秒
var seconds=now.getSeconds();
var secondsDegrees=seconds*6+90;
secondHand.style.transform=`rotate(${secondsDegrees}deg)`;/* ``用于变量和字符串拼接 */
var mins=now.getMinutes();
var minsDegrees=mins*6+90;
minHand.style.transform=`rotate(${ minsDegrees}deg)`;
var hours=now.getHours();
var hoursDegrees=hours*30+90+(mins/60)*30;
hourHand.style.transform=`rotate(${hoursDegrees}deg)`;
}
setTime();
//定时器
setInterval(setTime,1000);
js解析
先通过document.querySelector('.xxx') 获取元素secondHand,minHand 和hourHand ,通过new Date() 获取当前时间,并命名now 为其值,再分别通过now.getSeconds(),now.getMinutes(),now.getHours() 来获取秒,分,时,并分别命名为seconds,mins,hours ,再分别通过seconds/60*360+90 获取秒针 的度数secondsDegrees ,mins/60*360+90 获取分针 的度数 minsDegrees 和hours/12360+90+(mins/60)/12360 获取时针 的度数hoursDegrees ,最后通过xxx.style.transform=rotate(${ xxx}deg)
将度数转化为带deg单位的数值。