web实例——时钟

实现如图时钟效果:

 1.首先制作表盘

     html:

<div id="container">
        <div id="clock_dial"></div>
    </div>

    css:

*{
    margin: 0;
    padding: 0;
}
body{
    width: 100vm;
    height: 100vh;
    background: #000;
    display: flex;
    justify-content: center;
    align-items: center;
}
#container{
    width: 400px;
    height: 400px;
    background: #fff;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 0 10px #fff;
}
#container #clock_dial{
    width: 370px;
    height: 370px;
    background: #000;
    border-radius: 50%;
    box-shadow: inset 0 0 10px #fff;
}

当前效果:

 2.在表盘中填入数字等部分

   html:

<div id="container">
        <div id="clock_dial">
            <div class="num">3</div>
            <div class="num">6</div>
            <div class="num">9</div>
            <div class="num">12</div>
        </div>
</div>

  css:(未经改动的代码这里不重复,下同)

#container #clock_dial{
    width: 370px;
    height: 370px;
    background: #000;
    border-radius: 50%;
    box-shadow: inset 0 0 10px #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}
#container #clock_dial .num{
    color: #fff;
    font-size: 50px;
    font-weight: 800;
    position: absolute;
}
.num:nth-child(1){
    right: 10px;
}
.num:nth-child(2){
    bottom: 0px;
}
.num:nth-child(3){
    left: 10px;
}
.num:nth-child(4){
    top: 0px;
}
#container #clock_dial .lattice{
    width: 6px;
    height: 30px;
    background: #fff;
    position: absolute;
    top: 0px;
    transform-origin: center 185px;
}

  JavaScript:

let clock = document.getElementById('clock_dial');

for (let i=1; i<9;i++){
    let la = document.createElement('div');
    la.className = 'lattice'
    if(i<3){
        la.style.transform = `rotate(${i*30}deg)`;
    }else if(i<5){
        la.style.transform = `rotate(${(i+1)*30}deg)`;
    }else if(i<7){
        la.style.transform = `rotate(${(i+2)*30}deg)`;
    }else{
        la.style.transform = `rotate(${(i+3)*30}deg)`;
    }
    clock.appendChild(la)
}

当前效果:

 3.最后,实现指针部分

    html:

<div id="container">
        <div id="clock_dial">
            <div class="num">3</div>
            <div class="num">6</div>
            <div class="num">9</div>
            <div class="num">12</div>
            <div id="circle"></div>
            <div id="hour"></div>
            <div id="min"></div>
            <div id="sec"></div>
        </div>
</div>

  css:

#container #clock_dial #circle{
    width: 30px;
    height: 30px;
    background: #fff;
    border-radius: 50%;
}
#container #clock_dial #hour{
    position: absolute;
    width: 15px;
    height: 70px;
    background: #fff;
    transform: translateY(-35px);
    transform-origin: center bottom;
}
#container #clock_dial #min{
    position: absolute;
    width: 10px;
    height: 100px;
    background: #fff;
    transform: translateY(-50px);
    transform-origin: center bottom;
}
#container #clock_dial #sec{
    position: absolute;
    width: 5px;
    height: 120px;
    background: #fff;
    transform: translateY(-60px);
    transform-origin: center bottom;
}

   JavaScript:

let hour = document.getElementById('hour');
let min = document.getElementById('min');
let sec = document.getElementById('sec');

let run = () => {
    let h = new Date().getHours();
    let m = new Date().getMinutes();
    let s = new Date().getSeconds();

    if (h > 12) {
        h = h - 12;
        hour.style.transform = `translateY(-35px) rotate(${h*30}deg)`;
    }else{
        hour.style.transform = `translateY(-35px) rotate(${h*30}deg)`;
    }
    min.style.transform = `translateY(-50px) rotate(${m*6}deg)`;
    sec.style.transform = `translateY(-60px) rotate(${s*6}deg)`;
}
run();
setInterval(run,1000);

当前效果:

 4.完整代码

  html主体部分:

<body>
    <div id="container">
        <div id="clock_dial">
            <div class="num">3</div>
            <div class="num">6</div>
            <div class="num">9</div>
            <div class="num">12</div>
            <div id="circle"></div>
            <div id="hour"></div>
            <div id="min"></div>
            <div id="sec"></div>
        </div>
    </div>
</body>

  css:

*{
    margin: 0;
    padding: 0;
}
body{
    width: 100vm;
    height: 100vh;
    background: #000;
    display: flex;
    justify-content: center;
    align-items: center;
}
#container{
    width: 400px;
    height: 400px;
    background: #fff;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 0 10px #fff;
}
#container #clock_dial{
    width: 370px;
    height: 370px;
    background: #000;
    border-radius: 50%;
    box-shadow: inset 0 0 10px #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}
#container #clock_dial .num{
    color: #fff;
    font-size: 50px;
    font-weight: 800;
    position: absolute;
}
.num:nth-child(1){
    right: 10px;
}
.num:nth-child(2){
    bottom: 0px;
}
.num:nth-child(3){
    left: 10px;
}
.num:nth-child(4){
    top: 0px;
}
#container #clock_dial .lattice{
    width: 6px;
    height: 30px;
    background: #fff;
    position: absolute;
    top: 0px;
    transform-origin: center 185px;
}
#container #clock_dial #circle{
    width: 30px;
    height: 30px;
    background: #fff;
    border-radius: 50%;
}
#container #clock_dial #hour{
    position: absolute;
    width: 15px;
    height: 70px;
    background: #fff;
    transform: translateY(-35px);
    transform-origin: center bottom;
}
#container #clock_dial #min{
    position: absolute;
    width: 10px;
    height: 100px;
    background: #fff;
    transform: translateY(-50px);
    transform-origin: center bottom;
}
#container #clock_dial #sec{
    position: absolute;
    width: 5px;
    height: 120px;
    background: #fff;
    transform: translateY(-60px);
    transform-origin: center bottom;
}

  JavaScript:

let clock = document.getElementById('clock_dial');
let hour = document.getElementById('hour');
let min = document.getElementById('min');
let sec = document.getElementById('sec');

for (let i = 1; i < 9; i++) {
    let la = document.createElement('div');
    la.className = 'lattice'
    if (i < 3) {
        la.style.transform = `rotate(${i * 30}deg)`;
    } else if (i < 5) {
        la.style.transform = `rotate(${(i + 1) * 30}deg)`;
    } else if (i < 7) {
        la.style.transform = `rotate(${(i + 2) * 30}deg)`;
    } else {
        la.style.transform = `rotate(${(i + 3) * 30}deg)`;
    }
    clock.appendChild(la)
}

let run = () => {
    let h = new Date().getHours();
    let m = new Date().getMinutes();
    let s = new Date().getSeconds();

    if (h > 12) {
        h = h - 12;
        hour.style.transform = `translateY(-35px) rotate(${h*30}deg)`;
    }else{
        hour.style.transform = `translateY(-35px) rotate(${h*30}deg)`;
    }
    min.style.transform = `translateY(-50px) rotate(${m*6}deg)`;
    sec.style.transform = `translateY(-50px) rotate(${s*6}deg)`;
}
run();
setInterval(run,1000);

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值