HTML+CSS+JS制作一个漂亮的橙子动态时钟
1. 效果图:
2. 背景产生:
利用四块与圆同高的矩形转一定的角度将圆切分成八块形成橙子内里,利用径向渐变形成橙皮。
background: radial-gradient(ellipse at center, #fff 60%, #ffd36a calc( 60% + 1px ),#fab91f calc( 68% + 1px ));
radial-gradient() 函数用径向渐变创建 "图像"。径向渐变由中心点定义。center(默认):设置中间为径向渐变圆心的纵坐标值。这个想要详细了解的话可以看看这篇深入理解CSS径向渐变radial-gradient文章。
transform:rotate(45deg);
transform就是变形的意思,rotate(angle):通过指定的角度参数对原元素指定一个2D rotation(2D 旋转),需先有transform-origin属性的定义。transform-origin定义的是旋转的基点,其中angle是指旋转角度,如果设置的值为正数表示顺时针旋转,如果设置的值为负数,则表示逆时针旋转。
3. 代码实现:
<!DOCTYPE html>
<html lang="ch" >
<head>
<title>橙子时钟——戈小戈</title>>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="author" content="戈小戈">
<!-- CSS代码开始 -->
<style type="text/css">
/* body {
background-color: #fff;
margin: 0;
} */
.wrapper {
position: relative;
}
/* author:戈小戈 */
.face {
display: flex;
justify-content: center;
align-items: center;
width: 20vmin;
height: 20vmin;
border-radius: 50%;
position: relative;
background: radial-gradient(ellipse at center, #fff 60%, #ffd36a calc( 60% + 1px ),#fab91f calc( 68% + 1px ));
}
.back{
display: flex;
justify-content: center;
align-items: center;
width: 80%;
height:80%;
border-radius: 50%;
position: relative;
background-color:#fce05c;
}
.backborderright1{
width:5%;
height:100%;
position: absolute;
background-color:#fff;
}
.backborderright2{
width:5%;
height:100%;
position: absolute;
background-color:#fff;
transform:rotate(45deg);
-ms-transform:rotate(45deg); /* IE 9 */
-moz-transform:rotate(45deg); /* Firefox */
-webkit-transform:rotate(45deg); /* Safari 和 Chrome */
-o-transform:rotate(45deg); /* Opera */
}
.backborderright3{
width:5%;
height:100%;
position: absolute;
background-color:#fff;
transform:rotate(90deg);
-ms-transform:rotate(90deg); /* IE 9 */
-moz-transform:rotate(90deg); /* Firefox */
-webkit-transform:rotate(90deg); /* Safari 和 Chrome */
-o-transform:rotate(90deg); /* Opera */
}
.backborderright4{
width:5%;
height:100%;
position: absolute;
background-color:#fff;
transform:rotate(135deg);
-ms-transform:rotate(135deg); /* IE 9 */
-moz-transform:rotate(135deg); /* Firefox */
-webkit-transform:rotate(135deg); /* Safari 和 Chrome */
-o-transform:rotate(135deg); /* Opera */
}
.face:after {
content: '';
display: block;
width: 3vmin;
height: 3vmin;
border-radius: 50%;
/* background-color: #f8ccce; */
position: absolute;
background-size:cover;
-webkit-transform: rotate(-360deg);
animation: rotation 3s linear infinite;
-moz-animation: rotation 3s linear infinite;
-webkit-animation: rotation 3s linear infinite;
-o-animation: rotation 3s linear infinite;
}
@-webkit-keyframes rotation{
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(-360deg);}
}
.quarter {
height: 80%;
width: 80%;
position: absolute;
}
.quarter div {
height: 10%;
width: 2.4%;
border-radius: 1vmin;
background-color: #fab91f;
position: absolute;
}
.quarter div:nth-child(even) {
top: 45%;
transform: rotate(90deg);
}
.quarter div:nth-child(odd) {
left: 48.8%;
}
.quarter div:nth-child(1) {
top: 0;
}
.quarter div:nth-child(2) {
right: 3%;
}
.quarter div:nth-child(3) {
bottom: 0;
}
.quarter div:nth-child(4) {
left: 3%;
}
.hands div {
position: absolute;
bottom: 50%;
border-radius: 1vmin;
transform-origin: 50% 100%;
transform: rotate(0);
}
.hands div.h {
height: 27%;
width: 2.4%;
left: 48.8%;
background-color: #db9a03;
}
.hands div.m {
height: 40%;
width: 2.4%;
left: 48.8%;
background-color: #fcb104;
}
.hands div.s {
height: 50%;
width: 1%;
left: 49.5%;
bottom: 40%;
transform-origin: 50% 80%;
background-color: #c7b684;
}
</style>
<!-- CSS代码结束 -->
</head>
<body>
<div class="wrapper" style="padding:10px;">
<!-- author:戈小戈 -->
<div class="face">
<div class="back">
<div class="backborderright1"></div>
<div class="backborderright2"></div>
<div class="backborderright3"></div>
<div class="backborderright4"></div>
</div>
<div class="quarter">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="hands">
<div class="m"></div>
<div class="h"></div>
<div class="s"></div>
</div>
</div>
</div>
<!--结束-->
<script>
// author:戈小戈
const sHand = document.querySelector('.s');
const mHand = document.querySelector('.m');
const hHand = document.querySelector('.h');
function setTime() {
const d = new Date();
const s = d.getSeconds();
const m = d.getMinutes();
const h = d.getHours();
const sDeg = (( s / 60 ) * 360 );
const mDeg = (( m / 60 ) * 360 );
const hDeg = (( h / 12 ) * 360 );
sHand.style.transform = `rotate( ${sDeg}deg )`;
mHand.style.transform = `rotate( ${mDeg}deg )`;
hHand.style.transform = `rotate( ${hDeg}deg )`;
}
setInterval( setTime, 1000 );
</script>
</body>
</html>