您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了HTML5+CSS3怎么实现时钟效果的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇HTML5+CSS3怎么实现时钟效果文章都会有所收获,下面我们一起来看看吧。
目的:
利用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 = 'dial'> <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 = 'text/javascript' src = 'js/jquery-3.2.1.js'></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>
关于“HTML5+CSS3怎么实现时钟效果”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“HTML5+CSS3怎么实现时钟效果”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。