Css3+Js 漂亮时钟

Css3+Js 漂亮时钟

浏览器支持 IE9/GoogleChrome/FireFox/Safari
效果图:


HTML代码

<!DOCTYPE html>
<html>
<head>
<meta >
<title>css3+js打造漂亮时钟</title>
<style>
body{background: -webkit-radial-gradient(center center,circle,#666,#000); margin: 0;}
div{margin: 0; padding: 0;}
.box{padding-top: 50px; width:410px; margin: 0 auto;}
.clock{position: relative; width:400px; height: 400px; border: 5px solid #fff; border-radius: 200px; background:-webkit-radial-gradient(center center,circle,#fff,#bbb); box-shadow: 1px 1px 30px rgba(0, 0, 0, 0.8); }
.clock .clock-xin{position: absolute; top: 50%; left: 50%; width:30px; height: 30px; border-radius: 15px; background: #eee;margin:-15px 0 0 -15px;}
.clock .clock-xin2{position: absolute; top: 50%; left: 50%; width:12px; height: 12px; border-radius: 6px; background: #f00; z-index: 100; margin:-6px 0 0 -6px;}
.clock .date{position: absolute; z-index: 3; top: 245px; left: 130px; font-size: 20px; color: #000; text-shadow: 1px 1px white; }
.clock .hour{position: absolute; z-index: 3; top: 50%; left: 50%; width:160px; height: 6px; border-radius:5px; background: #000; -webkit-transform-origin: 10px 50%; margin:-3px 0 0 -10px;}
.clock .min{position: absolute; z-index: 4;top: 50%; left: 50%; width:180px; height: 4px; border-radius:5px; background: #333; -webkit-transform-origin: 10px 50%; margin:-2px 0 0 -10px;}
.clock .sec{position: absolute; z-index: 5; top: 50%; left: 50%; width:210px; height: 2px; background: #f00; -webkit-transform-origin: 30px 50%; margin:-1px 0 0 -30px;}
.clock em{display: block; width: 2px; height: 5px; background: #000; position: absolute; top: 0; left: 0; -webkit-transform-origin: 50% 0; margin-left: -1px;}
.clock em.ishour{width: 6px; height: 10px; margin-left: -3px;}
.clock em.ishour i{font-size: 25px; color: #000; position: absolute; top: 12px; left: -7px;text-shadow: 1px 1px white; }
</style>
</head>
<body>
<div style="color:white;">浏览器支持:GoogleChrome</div>
<div class="box">
    <div class="clock" id="clock">
        <div class="clock-xin"></div>
        <div class="clock-xin2"></div>
        <div id="date" class="date"></div>
        <div id="hour" class="hour"></div>
        <div id="min" class="min"></div>
        <div id="sec" class="sec"></div>
    </div>
</div>


<script>
window.onload = function() {
    var winHeight = document.documentElement.clientHeight;
    document.getElementsByTagName('body')[0].style.height = winHeight+'px';


    var $clock = document.getElementById('clock'),
        $date = document.getElementById('date'),
        $hour = document.getElementById('hour'),
        $min = document.getElementById('min'),
        $sec = document.getElementById('sec'),
        oSecs = document.createElement('em');
    for (var i = 1; i < 61; i++) {
        var tempSecs = oSecs.cloneNode(),
        pos = getSecPos(i);
        if(i%5==0){
            tempSecs.className = 'ishour';
            tempSecs.innerHTML = '<i style="-webkit-transform:rotate('+(-i*6)+'deg);">'+(i/5)+'</i>';
        }
        tempSecs.style.cssText='left:'+pos.x+'px; top:'+pos.y+'px; -webkit-transform:rotate('+i*6+'deg);';
        $clock.appendChild(tempSecs);
    }


    // 圆弧上的坐标换算
    function getSecPos(dep) {
        var hudu = (2*Math.PI/360)*6*dep,
        r = 200; //半径大小
        return {
            x: Math.floor(r + Math.sin(hudu)*r),
            y: Math.floor(r - Math.cos(hudu)*r),
        }
    }




    ;(function() {
        // 当前时间
        var _now = new Date(),
        _h = _now.getHours(),
        _m = _now.getMinutes(),
        _s = _now.getSeconds();


        var _day = _now.getDay();
        _day = (_day==0)?7:_day;
if(_day==1){
_day = "一";
}else if(_day==2){
_day = "二";
}else if(_day==3){
_day = "三";
}else if(_day==4){
_day = "四";
}else if(_day==5){
_day = "五";
}else if(_day==6){
_day = "六";
}else if(_day==7){
_day = "日";
}
        $date.innerHTML = _now.getFullYear()+'-'+(_now.getMonth()+1)+'-'+_now.getDate()+' 星期'+_day;
        //绘制时钟
        var gotime = function(){
            var _h_dep = 0;
            _s++;
            if(_s>59){
                _s=0;
                _m++;
            }
            if(_m>59){
                _m=0;
                _h++;
            }
            if(_h>12){
               _h = _h-12;
            }


            //时针偏移距离
            _h_dep = Math.floor(_m/12)*6; 


            $hour.style.cssText = '-webkit-transform:rotate('+(_h*30-90+_h_dep)+'deg);';
            $min.style.cssText = '-webkit-transform:rotate('+(_m*6-90)+'deg);';
            $sec.style.cssText = '-webkit-transform:rotate('+(_s*6-90)+'deg);';


        };


        gotime();
        setInterval(gotime, 1000);
    })();


};




</script>
</body>
</html>


  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 可以使用 JavaScript 来实现一个时钟,HTML 和 CSS 用来布局和样式化。以下是一个简单的实现: HTML: ``` <div id="clock"></div> ``` CSS: ``` #clock { font-size: 2em; font-weight: bold; text-align: center; } ``` JavaScript: ``` function updateClock() { var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds(); var timeString = hours + ':' + minutes + ':' + seconds; document.getElementById('clock').innerHTML = timeString; } setInterval(updateClock, 1000); ``` 这个时钟会每秒钟更新一次,显示当前的时间。 ### 回答2: CSS、JS和HTML时钟是一种通过使用层叠样式表、JavaScript和HTML来实现的时钟。这种时钟能够显示当前的时间,并且会实时更新。下面我将分别介绍CSS、JS和HTML在实现时钟功能中的作用。 首先是CSS,它负责时钟的外观和样式。我们可以使用CSS选择器来选择时钟的不同部分,例如表盘、时针、分针和秒针。通过设置不同的样式属性,我们可以控制时钟的颜色、大小、字体等。同时,我们还可以使用CSS过渡和动画效果来实现指针的流畅运动,使时钟更加美观。 其次是JS,它负责时钟的逻辑和功能。我们可以使用JavaScript的Date对象来获取当前的时间信息,包括小时、分钟和秒钟。通过计算时间的角度,我们可以将时钟的指针旋转到正确的位置。另外,我们还可以使用定时器函数(setInterval)来实现时钟的实时更新,让时钟能够每秒钟更新一次。 最后是HTML,它负责时钟的结构和布局。我们可以使用HTML标签来创建时钟的不同部分,例如表盘、指针和数字。通过设置不同的class和id,我们可以在CSS和JS中选择和操作时钟的各个元素。同时,我们还可以使用CSS Grid或Flexbox来对时钟进行布局和对齐,使其在不同设备上都能够正常显示。 综上所述,CSS、JS和HTML时钟是通过使用层叠样式表、JavaScript和HTML来实现的一种能够显示当前时间的时钟。CSS负责样式和外观,JS负责逻辑和功能,HTML负责结构和布局。通过它们的结合,我们可以创造出一个精确而美观的时钟效果。 ### 回答3: CSS、JS、HTML时钟是一种通过CSS样式、JavaScript脚本和HTML标记语言来实现的显示当前时间的时钟。 在HTML中,我们可以使用<div>标签来创建一个容器,然后使用<span>标签来分别显示时、分、秒,并用":"作为分隔符。 在CSS中,我们可以为时钟容器设置宽度、高度以及背景颜色,使其显示为一个矩形框。为时、分、秒分别设置字体、字号、颜色等样式。通过调整这些样式,我们可以实现不同的外观效果。 在JavaScript中,我们可以使用Date对象来获取当前的时间,并通过JavaScript函数来动态更新时钟的显示。我们可以编写一个函数,在函数中获取当前的时、分、秒,并将其分别赋值给对应的<span>标签,然后通过setTimeout()函数每秒钟调用一次该函数,实现时钟的动态更新。 最后,在HTML中调用该JavaScript函数,即可在网页中显示一个实时动态更新的时钟。 总结起来,CSS、JS、HTML时钟通过HTML标签创建结构,使用CSS样式设置外观,通过JavaScript脚本获取、更新时间,从而实现了一个简单的时钟效果。这种时钟不仅能够显示当前的时间,还能够随着时间的变化进行实时更新。这种时钟在网页中广泛应用于显示当前时间或计时器等功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值