用H5和C3实现简单的时钟效果

php中世界最好的语言
发布: 2018-01-19 09:39:04
原创
2204 人浏览过

这次给大家带来用H5和C3实现简单的时钟效果,用H5和C3实现简单的时钟效果的注意事项有哪些,下面就是实战案例,一起来看一下。

目的:

利用html5,css实现钟摆效果

知识点:

1) 利用position/left/top和calc()实现元素的水平和垂直居中;

2) 利用CSS3的animation/transform/transform-origin属性定义动画;

3) 利用transform-origin实现旋转原点的圆心调整

废话不多说了,直接看代码吧,具体代码如下所示:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        li{
            list-style:none;
        }
        #box{
            width: 400px;
            height: 400px;
            position: absolute;
            top:calc(50% - 200px);
            left:calc(50% - 200px);
            border: 2px solid palegoldenrod;
        }
        #dial{
            width: 200px;
            height: 200px;
            position: absolute;
            top:calc(50% - 100px);
            left:calc(50% - 100px);
            border: 2px solid cyan;
            border-radius: 50%;
        }  
        .scaleIndex{
            width: 4px;
            height: 12px;
            position: absolute;
            top: 0;
            left: calc(50% - 2px);
            background-color: gray;
            transform-origin: 2px 100px;
        }
        .angle30{transform : rotate(30deg);}
        .angle60{transform : rotate(60deg);}
        .angle90{transform : rotate(90deg);}
        .angle120{transform : rotate(120deg);}
        .angle150{transform : rotate(150deg);}
        .angle180{transform : rotate(180deg);}
        .angle210{transform : rotate(210deg);}
        .angle240{transform : rotate(240deg);}
        .angle270{transform : rotate(270deg);}
        .angle300{transform : rotate(300deg);}
        .angle330{transform : rotate(330deg);}
        #fixPoint{
            width: 10px;
            height: 10px;
            position: absolute;
            top:calc(50% - 5px);
            left:calc(50% - 5px);
            background-color: cadetblue;
            border-radius: 50%;
        }
        #hourHand{
            width: 6px;
            height: 70px;
            position:absolute;
            top: 40px;
            left: calc(50% - 3px);
            background-color: darkblue;
            transform-origin: 50% 60px;
        }
        #minuteHand{
            width: 4px;
            height: 75px;
            position:absolute;
            top: 35px;
            left: calc(50% - 2px);
            background-color: yellow;
            transform-origin: 50% 65px;
        }
        #secondHand{
            width: 2px;
            height: 90px;
            position:absolute;
            top: 20px;
            left: calc(50% - 1px);
            background-color: red;
            transform-origin: 50% 80px;
        }
    </style>
</head>
<body>
    <div id = "box">
            <div id = &#39;dial&#39;>
                <ul id = "scale">
                    <li class = "scaleIndex"></li>
                    <li class = "scaleIndex angle30"></li>
                    <li class = "scaleIndex angle60"></li>
                    <li class = "scaleIndex angle90"></li>
                    <li class = "scaleIndex angle120"></li>
                    <li class = "scaleIndex angle150"></li>
                    <li class = "scaleIndex angle180"></li>
                    <li class = "scaleIndex angle210"></li>
                    <li class = "scaleIndex angle240"></li>
                    <li class = "scaleIndex angle270"></li>
                    <li class = "scaleIndex angle300"></li>
                    <li class = "scaleIndex angle330"></li>
                </ul>
                <div id = "fixPoint"></div>
                <div id = "hourHand"></div>
                <div id = "minuteHand"></div>
                <div id = "secondHand"></div>
            </div>
        </div>
<script type = &#39;text/javascript&#39; src = &#39;js/jquery-3.2.1.js&#39;></script>
<script type = "text/javascript">
window.onload = function(){
            var hourHand = document.getElementById(&#39;hourHand&#39;);
            var minuteHand = document.getElementById(&#39;minuteHand&#39;);
            var secondHand = document.getElementById(&#39;secondHand&#39;);
setInterval(function(){
                    var currentTime = new Date();
                    var hourValue = currentTime.getHours();
                    var hourAngle = hourValue / 24 * 360 + &#39;deg&#39;;
                    var minuteValue = currentTime.getMinutes();
                    var minuteAngle = minuteValue / 60 * 360 + &#39;deg&#39;;
                    var secondValue = currentTime.getSeconds();
                    var secondAngle = secondValue / 60 * 360 + &#39;deg&#39;;
                    console.log(hourAngle);
// 方法一:利用jquery的css()增加属性
var cmdHour = &#39;rotate(&#39;+ hourAngle +&#39;)&#39;;
$(&#39;#hourHand&#39;).css({transform:&#39;rotate(&#39;+ hourAngle +&#39;)&#39;});
var cmdMinute = &#39;rotate(&#39;+ minuteAngle +&#39;)&#39;;
$(&#39;#minuteHand&#39;).css({transform:cmdMinute});
var cmdSecond = &#39;rotate(&#39;+ secondAngle +&#39;)&#39;;
$(&#39;#secondHand&#39;).css({transform:cmdSecond});
 
// 方法二:利用DOM元素的style属性设置
//      hourHand.style.transform = &#39;rotate(&#39;+ hourAngle + &#39;)&#39;;
//      minuteHand.style.transform = &#39;rotate(&#39;+ minuteAngle + &#39;)&#39;;
//      secondHand.style.transform = &#39;rotate(&#39;+ secondAngle + &#39;)&#39;; 
 
},1000);
}
    </script>
</body>
</html>
登录后复制

相信看了这些案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

相关阅读:

HTML的table鼠标拖拽排序该如何实现

html属于什么文件html的文件该如何打开 

Html怎样实现动态显示颜色块的报表效果

以上是用H5和C3实现简单的时钟效果的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!