CSS时钟案例

效果如下图
在这里插入图片描述
下面是html代码

<div class="box">
    <div class="line line1"></div>
    <div class="line line2"></div>
    <div class="line line3"></div>
    <div class="line line4"></div>
    <div class="line line5"></div>
    <div class="line line6"></div>
    <div class="roundness circle"></div>
    <div class="needle hour"></div>
    <div class="needle minute"></div>
    <div class="needle second"></div>
    <div class="roundness cir"></div>
</div>

首先是外圆,给类box添加样式

.box {
    margin: 100px auto;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    border: 10px solid #cccccc;
    position: relative;
}

在这里插入图片描述
然后是画上刻度,给类line,line1 — line6添加样式

.box > .line {
    height: 300px;
    width: 10px;
    position: absolute;
    background-color: #cccccc;
    left: 50%;
    transform: translate(-50%, 0);
}
.box > .line2 {
    width: 7px;
    transform: translate(-50%, 0) rotateZ(30deg);
}
.box > .line3 {
    width: 7px;
    transform: translate(-50%, 0) rotateZ(60deg);
}
.box > .line4 {
    transform: translate(-50%, 0) rotateZ(90deg);
}
.box > .line5 {
    width: 7px;
    transform: translate(-50%, 0) rotateZ(120deg);
}
.box > .line6 {
    width: 7px;
    transform: translate(-50%, 0) rotateZ(150deg);
}

在这里插入图片描述
然后加一个圆,把中间的线遮住,给类roundness(公共样式—最后有个小圆会用到) circle添加样式

.roundness {
    border-radius: 50%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
.circle {
    width: 270px;
    height: 270px;
    background-color: #fff;
}

在这里插入图片描述
下面是给时、分、秒针,给类needle(公共样式) hour minute second添加样式,并添加动画效果

.needle {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -100%);
}
.hour {
    width: 10px;
    height: 70px;
    background-color: limegreen;
    border-radius: 5px 5px;
    /*设置旋转轴心*/
    transform-origin: center bottom;
    /*添加动画*/
    animation: clockanimation 43200s linear infinite;
}
.minute {
    width: 7px;
    height: 90px;
    background-color: yellow;
    border-radius: 3.5px 3.5px;
    /*设置旋转轴心*/
    transform-origin: center bottom;
    /*添加动画*/
    animation: clockanimation 3600s linear infinite;
}
.second {
    width: 4px;
    height: 110px;
    background-color: red;
    border-radius: 2px 2px;
    /*设置旋转轴心*/
    transform-origin: center bottom;
    /*添加动画*/
    animation: clockanimation 60s linear infinite ;
}
@keyframes clockanimation {
    0% {
        transform: translate(-50%, -100%);
    }
    100% {
        transform: translate(-50%, -100%) rotate(360deg);
    }
}

在这里插入图片描述
最后在中心加一个小圆

.cir {
    width: 20px;
    height: 20px;
    background-color: #ccc;
}

在这里插入图片描述

下面是完整代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>时钟案例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            margin: 100px auto;
            width: 300px;
            height: 300px;
            border-radius: 50%;
            border: 10px solid #cccccc;
            position: relative;
        }
        .box>.line {
            height: 300px;
            width: 10px;
            position: absolute;
            background-color: #cccccc;
            left: 50%;
            transform: translate(-50%, 0);
        }
        .box>.line2 {
            width: 7px;
            transform: translate(-50%, 0) rotateZ(30deg);
        }
        .box>.line3 {
            width: 7px;
            transform: translate(-50%, 0) rotateZ(60deg);
        }
        .box>.line4 {
            transform: translate(-50%, 0) rotateZ(90deg);
        }
        .box>.line5 {
            width: 7px;
            transform: translate(-50%, 0) rotateZ(120deg);
        }
        .box>.line6 {
            width: 7px;
            transform: translate(-50%, 0) rotateZ(150deg);
        }
        .roundness {
            border-radius: 50%;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        .circle {
            width: 270px;
            height: 270px;
            background-color: #fff;
        }
        .needle {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -100%);
        }
        .hour {
            width: 10px;
            height: 70px;
            background-color: limegreen;
            border-radius: 5px 5px;
            /*设置旋转轴心*/
            transform-origin: center bottom;
            /*添加动画*/
            animation: clockanimation 43200s linear infinite;
        }
        .minute {
            width: 7px;
            height: 90px;
            background-color: yellow;
            border-radius: 3.5px 3.5px;
            /*设置旋转轴心*/
            transform-origin: center bottom;
            /*添加动画*/
            animation: clockanimation 3600s linear infinite;
        }
        .second {
            width: 4px;
            height: 110px;
            background-color: red;
            border-radius: 2px 2px;
            /*设置旋转轴心*/
            transform-origin: center bottom;
            /*添加动画*/
            animation: clockanimation 60s linear infinite;
        }
        @keyframes clockanimation {
            0% {
                transform: translate(-50%, -100%);
            }
            100% {
                transform: translate(-50%, -100%) rotate(360deg);
            }
        }
        .cir {
            width: 20px;
            height: 20px;
            background-color: #ccc;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="line line1"></div>
        <div class="line line2"></div>
        <div class="line line3"></div>
        <div class="line line4"></div>
        <div class="line line5"></div>
        <div class="line line6"></div>
        <div class="roundness circle"></div>
        <div class="needle hour"></div>
        <div class="needle minute"></div>
        <div class="needle second"></div>
        <div class="roundness cir"></div>
    </div>
</body>
</html>
  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以看到这是一个CSS时钟的样式代码,其中包括了时针、分针和秒针的样式。并没有提到立体数字标识。如果您需要实现CSS时钟立体数字标识,可以参考以下步骤: 1. 首先,您需要在HTML中创建一个容器,用于放置时钟和数字标识。 2. 然后,您可以使用CSS样式来创建数字标识。可以使用伪元素:before和:after来创建数字的立体效果。 3. 接下来,您需要使用JavaScript来获取当前时间,并将时、分、秒的值分别赋值给时针、分针和秒针的样式中的transform属性,以实现时钟的动态效果。 以下是一个简单的CSS时钟立体数字标识的示例代码: ```html <div class="clock"> <div class="digits"> <span class="digit">1</span> <span class="digit">2</span> <span class="digit">3</span> <span class="digit">4</span> <span class="digit">5</span> <span class="digit">6</span> <span class="digit">7</span> <span class="digit">8</span> <span class="digit">9</span> <span class="digit">10</span> <span class="digit">11</span> <span class="digit">12</span> </div> <div class="hands"> <div class="hour-hand"></div> <div class="minute-hand"></div> <div class="second-hand"></div> </div> </div> <style> .clock { position: relative; width: 200px; height: 200px; border-radius: 50%; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .digits { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 20px; font-weight: bold; color: #000; } .digit { position: absolute; width: 20px; height: 20px; text-align: center; line-height: 20px; } .digit:nth-child(1) { top: 10px; left: 50%; transform: translateX(-50%); } .digit:nth-child(2) { top: 20px; left: calc(50% + 40px); } .digit:nth-child(3) { top: 40px; left: calc(50% + 70px); } .digit:nth-child(4) { top: calc(50% - 10px); left: calc(50% + 80px); } .digit:nth-child(5) { top: calc(50% + 60px); left: calc(50% + 70px); } .digit:nth-child(6) { bottom: 10px; left: 50%; transform: translateX(-50%); } .digit:nth-child(7) { top: calc(50% + 60px); left: calc(50% - 70px); } .digit:nth-child(8) { top: calc(50% - 10px); left: calc(50% - 80px); } .digit:nth-child(9) { top: 40px; left: calc(50% - 70px); } .digit:nth-child(10) { top: 20px; left: calc(50% - 40px); } .digit:nth-child(11) { top: 10px; left: 50%; transform: translateX(-50%); } .digit:nth-child(12) { top: 10px; left: 50%; transform: translateX(-50%); } .hands { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .hour-hand { position: absolute; top: 50%; left: 50%; width: 6px; height: 50px; margin-top: -25px; margin-left: -3px; background-color: #000; border-radius: 6px 6px 0 0; transform-origin: bottom center; } .minute-hand { position: absolute; top: 50%; left: 50%; width: 4px; height: 70px; margin-top: -35px; margin-left: -2px; background-color: #000; border-radius: 4px 4px 0 0; transform-origin: bottom center; } .second-hand { position: absolute; top: 50%; left: 50%; width: 2px; height: 80px; margin-top: -40px; margin-left: -1px; background-color: #f00; border-radius: 2px 2px 0 0; transform-origin: bottom center; } .second-hand:before { content: ""; position: absolute; top: -10px; left: -10px; width: 20px; height: 20px; border-radius: 50%; background-color: #f00; } .second-hand:after { content: ""; position: absolute; top: 50%; left: 50%; width: 4px; height: 40px; margin-top: -20px; margin-left: -2px; background-color: #f00; border-radius: 4px 4px 0 0; transform-origin: bottom center; } </style> <script> function updateClock() { var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds(); var hourHand = document.querySelector(".hour-hand"); var minuteHand = document.querySelector(".minute-hand"); var secondHand = document.querySelector(".second-hand"); hourHand.style.transform = "rotate(" + (hours * 30 + minutes / 2) + "deg)"; minuteHand.style.transform = "rotate(" + (minutes * 6) + "deg)"; secondHand.style.transform = "rotate(" + (seconds * 6) + "deg)"; setTimeout(updateClock, 1000); } updateClock(); </script> --相关问题--:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值