Home > Web Front-end > H5 Tutorial > body text

H5+C3 achieves clock effect

php中世界最好的语言
Release: 2018-03-27 10:52:36
Original
2512 people have browsed it

This time I will bring you H5+C3 to achieve the clock effect. What are the precautions for H5+C3 to achieve the clock effect? ​​The following is a practical case, let’s take a look.

Purpose:

Use html5 and css to achieve the pendulum effect

Knowledge points:

1) Use position/left/top and calc() to achieve horizontal and vertical centering of elements;

2) Use CSS3 animation/transform/transform-origin attribute definition Animation;

3) Use transform-origin to adjust the center of the rotation origin

No more nonsense, let’s look at the code directly. The specific code is as follows:

<!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>
    <p id = "box">
            <p 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>
                <p id = "fixPoint"></p>
                <p id = "hourHand"></p>
                <p id = "minuteHand"></p>
                <p id = "secondHand"></p>
            </p>
        </p>
<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('hourHand');
            var minuteHand = document.getElementById('minuteHand');
            var secondHand = document.getElementById('secondHand');
setInterval(function(){
                    var currentTime = new Date();
                    var hourValue = currentTime.getHours();
                    var hourAngle = hourValue / 24 * 360 + 'deg';
                    var minuteValue = currentTime.getMinutes();
                    var minuteAngle = minuteValue / 60 * 360 + 'deg';
                    var secondValue = currentTime.getSeconds();
                    var secondAngle = secondValue / 60 * 360 + 'deg';
                    console.log(hourAngle);
// 方法一:利用jquery的css()增加属性
var cmdHour = 'rotate('+ hourAngle +')';
$('#hourHand').css({transform:'rotate('+ hourAngle +')'});
var cmdMinute = 'rotate('+ minuteAngle +')';
$('#minuteHand').css({transform:cmdMinute});
var cmdSecond = 'rotate('+ secondAngle +')';
$('#secondHand').css({transform:cmdSecond});
// 方法二:利用DOM元素的style属性设置
//      hourHand.style.transform = 'rotate('+ hourAngle + ')';
//      minuteHand.style.transform = 'rotate('+ minuteAngle + ')';
//      secondHand.style.transform = 'rotate('+ secondAngle + ')';  
},1000);
}
    </script>
</body>
</html>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

H5 implements scalable clock animation

Detailed explanation of H5’s drag and drop function

The above is the detailed content of H5+C3 achieves clock effect. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template