目的:
利用html5,css实现钟摆效果
知识点:
1) 利用position/left/top和calc()实现元素的水平和垂直居中;
2) 利用css3的animation/transform/transform-origin属性定义动画;
3) 利用transform-origin实现旋转原点的圆心调整
废话不多说了,直接看代码吧,具体代码如下所示:
<!doctype html><html><head> <meta chart="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-ra天津师范大学津沽学院dius: 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; } #condhand{ 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> &2021年高考时间lt;li class = "scaleindex angle90"></li> <li class = "scaleindex angle120"></li> <li class = "scaleindex angle150"></li> <li class = "scaleindex angle180"></l三部曲系列i> <li class = "scaleindex angle210"></li> <li class = "scalei武林外传 电影版ndex 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 = "condhand"></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 condhand = document.getelementbyid('condhand');tinterval(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 condvalue = currenttime.getconds(); var condangle = condvalue / 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 cmdcond = 'rotate('+ condangle +')';$('#condhand').css({transform:cmdcond});// 方法二:利用dom元素的style属性设置// hourhand.style.transform = 'rotate('+ hourangle + ')';// minutehand.style.transform = 'rotate('+ minuteangle + ')';// condhand.style.transform = 'rotate('+ condangle + ')'; },1000);} </script></body></html>
总结
以上所述是www.887551.com给大家介绍的基于html5+css3实现简单的时钟效果,希望对大家有所帮助
本文发布于:2023-04-06 11:37:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/45cf5522c0416d1f508a080d8a806b60.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:基于HTML5+CSS3实现简单的时钟效果.doc
本文 PDF 下载地址:基于HTML5+CSS3实现简单的时钟效果.pdf
留言与评论(共有 0 条评论) |