JS实现钟表

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=640,user-scalable=no,target-densitydpi=device-dpi" />
        <style>
        *{
           margin:0;
           padding:0;
        }
        .parents{
            width:250px;
            height:250px;
            background:#000;
            -webkit-border-radius:20px;
        }
         .ab{
            position: absolute;
            width: 1px;
            height: 1px;
            border: 0px solid #999;
            background: #999;
            line-height: 3px;
            text-indent: -4px;
            font-size: 6px;
         }
         p.houre{
            height:200px;
            position:absolute;
            top:25px;
            left:125px;
            background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAABxCAYAAADlPJLFAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAPhJREFUeNrsmMEKgkAQhrWDbyAIXT0FnoRA8GrHXqs3CHqNEDp5EgqiQxTdwlvPEEiw/VuzIqGhBp3+gQ90Zz7WndmTtlLKaol6wv5MjqyBQZEiRYoUKVKkSJEiRYoUKVKkSJEiRYoU/yzaDb+CIzAHY3m/gTXYte3ogg3YggTchUTWdM6rqvWOwAUFOIFA1uoEkiukthJTcAFOg2RwpCY14hQ8QPhFMoRSG+nmLPDFIZh1bGgG9ro5E3DuMYmjdrRYDhhjqcUrCHpIwcvBGWM5sN+hOb7UxvVx5B3GkdfH8dMF0HggU+84gBVYyrOSnGfqB1/ypwADAJwYYKUbK5foAAAAAElFTkSuQmCC) 0 29px no-repeat rgba(0,0,0,0);
            width:10px;
            background-size:100%;
         }
         p.minutes{
            position: absolute;
            top:25px;
            left:125px;
            background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAACLCAIAAAArqbRdAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAACBJREFUeNpi/P//PwMYMDHAwChrlDXKGmWNsqjNAggwAC0BBBMOvt0tAAAAAElFTkSuQmCC) 0 4px no-repeat rgba(0,0,0,0);
            height:200px;
            width:2px;
            background-size:100%;
          }
         p.second{
            position: absolute;
            top:25px;
            left:125px;
            background:rgba(0,0,0,0);
            height:200px;
            width:1px;
          }
          p.second:before{
            content: "";
            position: absolute;
            top:0;
            left:0;
            background:#642D0E;
            height:100px;
            width:1px;
          }
        </style>
    </head>
    <body>
       <div class="parents">
           <p class="houre"></p>
           <p class="minutes"></p>
           <p class="second"></p>
       </div>
       <p class="nowtime"></p>
    </body>
    <script>
        (function(){
            var a=125,b=125,r=100,sdeg=mdeg=hdeg=0,divs=timer=null;
            var second=document.querySelector(".second"),
                minute=document.querySelector(".minutes"),
                houre=document.querySelector(".houre"),
                nowtime=document.querySelector(".nowtime"),
                parents=document.querySelector(".parents"),
                nowhours=(new Date().getHours()<=12)?new Date().getHours():new Date().getHours()-12,
                nowminutes=new Date().getMinutes(),
                nowseconds=new Date().getSeconds();
                console.log(nowhours+"sss"+nowminutes+"sssss"+nowseconds);
                hdeg=nowhours*30;
                mdeg=nowminutes*6;
                sdeg=nowseconds*6;
                clearInterval(timer);
                timer=setInterval(function(){
                    if(sdeg>=360){
                        sdeg=0;
                        if(mdeg>360){
                          mdeg=0;
                          hdeg+=6;
                          if(hdeg>=360){
                             hdeg=0;
                          }
                        }
                        mdeg+=6;
                    }
                    sdeg+=6;
                    second.style.webkitTransform='rotate('+sdeg+'deg)';
                    minute.style.webkitTransform='rotate('+mdeg+'deg)';
                    houre.style.webkitTransform='rotate('+hdeg+'deg)';
                    nowtime.innerText=new Date().toLocaleTimeString();
                },1000)
                for(var times=0; times<60; times++) {
                    var hudu = (2*Math.PI/ 360) * 6 * times;
                    var X = a + Math.sin(hudu) * r;
                    var Y = b - Math.cos(hudu) * r;
                    divs=document.createElement("div");
                    divs.className="ab";
                    divs.style.cssText='left:'+X+'px;top:'+Y+'px;';
                    divs.style.webkitTransform='rotate('+(6*times)+'deg)';
                    if(times%5==0){
                    divs.innerText=times/5;
                    divs.style.cssText='background:#2F5FA1;height:8px;border-color:#2F5FA1';
                    }
                    if(times==0)divs.innerText=12;
                    parents.appendChild(divs);
                }
        })()
    </script>
</html>

演示地址:http://sandbox.runjs.cn/show/xloecikn

时间: 2024-11-02 15:16:32

JS实现钟表的相关文章

JS 简单钟表

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> function startTime() { var d = new Date(); //声明一个Date对象,默认是当前时间 var h = d.getHours(); //以下分别获取d的小时,分钟,秒 var m = d.getMin

使用 canvas+JS绘制钟表

效果如下: 附上代码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>使用Canvas绘制钟表</title> 6 <style> 7 body { 8 background-color: pink; 9 } 10 #clock { 11 display: block;

JS+CSS 钟表

.<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> #wrap{ width: 580px; height: 580px; border-radius: 50%; background: linear-gradient(to bottom,whi

JS时钟钟表

<!DOCTYPE html> <html>    <head>        <meta charset="utf-8" />        <title></title>        <style> .clock{                width: 600px;                height: 600px;                background: url(im

js原生钟表(属性只支持的谷歌)

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>分页</title> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=

利用css+原生js制作简易钟表

利用css+原生js制作简单的钟表.效果如下所示 实现该效果,分三大块:html.javascript.css html部分html部分比较简单,定义一个clock的div,内部有原点.时分秒针.日期以及时间,至于钟表上的刻度.数字等元素,因为量比较多,采用jvascript生成 <!doctype html> <html> <head> <meta charset="UTF-8"> <link rel='stylesheet' h

简单的JS钟表计时

思路:先写出简单的数字计时,根据时分秒的数值转换成度数,使用CSS3的transform进行div倾斜. 知识点:transform可以对div进行倾斜或旋转等效果.但是根据浏览器不同代码也不同,本代码只能根据chorme浏览器或safari使用 div { transform:rotate(7deg); -ms-transform:rotate(7deg); /* IE 9 */ -moz-transform:rotate(7deg); /* Firefox */ -webkit-transf

css3++js钟表

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <style> 7 div{ 8 width:200px; 9 height:200px; 10 border-radius: 50%; 11 border:1px solid #000;

html,css,js实现的一个钟表

效果如图: 实现代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Clock</title> <style> body { display: flex; flex-direction: column; justify-content: center; align-items: cente