明霞山资源网 Design By www.htccd.com
本文实例为大家分享了js+css3实现简单时钟的具体代码,供大家参考,具体内容如下
1.实现了时钟的特效,可以转动,时间准确,画面美观大气;
2.用到了css3的transform: rotate,transform-origin:,伪元素,border-radius,定位,z-index等等
效果如图:
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS3时钟特效</title>
<link rel="shortcut icon" type="image/x-icon" href="img/an.ico" />
<style>
/*表盘边框*/
.clock {
/* 设置大小 */
width: 400px;
height: 400px;
position: relative;
margin: 40px auto;
/*上边距*/
border-radius: 50%;
/*圆形*/
box-shadow: 5px 5px 20px rgba(0, 0, 0, 0.5);
/*表盘阴影*/
background: #F5DEB3;
border: 10px solid #FFFF00;
}
/*画刻度的面板*/
.box {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
/*用来装刻度和数字的div*/
.box div {
width: 0px;
height: 200px;
position: absolute;
left: 200px;
/*旋转*/
transform: rotate(0deg);
/*设置基点为右下角*/
transform-origin: bottom right;
background: rgba(255, 0, 0, 0.5);
}
/*数字*/
.box div i {
float: left;
margin-top: 20px;
margin-left: -10px;
font-style: normal;
width: 20px;
text-align: center;
font-style: 18px;
}
/*小刻度*/
.box div::after {
content: "";
position: absolute;
background: #484848;
width: 2px;
height: 10px;
left: -1px;
}
/*大刻度*/
.box div.five::after {
position: absolute;
content: "";
width: 4px;
height: 20px;
left: -2px;
top: 0;
background: #484848;
border-radius: 0 0 2px 2px;
}
/*秒针样式*/
.second {
width: 1px;
height: 200px;
background: red;
position: absolute;
left: 200px;
/*距离表盘宽度一半*/
margin-top: 30px;
z-index: 10;
transform: rotate(0deg);
transform-origin: center 170px;
/*定位旋转位置*/
}
/*圆心样式*/
.second::after {
content: "";
position: absolute;
width: 20px;
height: 20px;
background: red;
border-radius: 50%;
bottom: 20px;
left: -10px;
}
/*分针样式*/
.minute {
width: 2px;
height: 140px;
background: #8b8b8d;
position: absolute;
left: 199px;
margin-top: 60px;
z-index: 9;
transform-origin: center bottom;
transform: rotate(12deg);
animation: minute 60s linear infinite;
}
/*时针样式*/
.hour {
width: 6px;
height: 100px;
background: #333;
position: absolute;
left: 197px;
margin-top: 100px;
z-index: 8;
border-radius: 3px;
transform: rotate(2deg);
transform-origin: center bottom;
animation: minute 60s linear infinite;
}
</style>
</head>
<body>
<div class="clock">
<div class="box"></div>
<div class="second"></div>
<div class="minute"></div>
<div class="hour"></div>
</div>
<script>
var box = document.getElementsByClassName("box")[0];
var ssObj = document.getElementsByClassName("second")[0];
var mmObj = document.getElementsByClassName("minute")[0];
var hhObj = document.getElementsByClassName("hour")[0];
/*获取当前时间*/
var date = new Date();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
/*计算页面指针加载时的角度*/
hhDeg = 360 * (hh % 12) / 12;
mmDeg = 360 * mm / 60;
ssDeg = 360 * ss / 60;
hhObj.style.transform = "rotate(" + hhDeg + "deg)";
mmObj.style.transform = "rotate(" + mmDeg + "deg)";
ssObj.style.transform = "rotate(" + ssDeg + "deg)";
// 定义初始刻度的度数
var Deg = 0;
/*画刻度*/
for (var i = 0; i < 60; i++) {
var div1 = document.createElement("div"); //创建一个div
var hourNum = i / 5;
//当为5时
if (hourNum == 0) hourNum = 12;
if (i % 5 == 0) { //大刻度
div1.className = "five";
div1.innerHTML = "<i>" + hourNum + "</i>"
}
div1.style.transform = "rotate(" + Deg + "deg)";
box.appendChild(div1);
Deg += 6;// 每两个刻度之间是6度
}
/*指针转动的函数*/
function drawSS() {
// 秒针的度数
ssDeg = 360 * ss / 60;
// 分针的度数
mmDeg1 = 360 * mm / 60;
// 时针的度数
hhDeg1 = 360 * (hh % 12) / 12;
// 分针每秒走的位置
mmDeg = mmDeg1 + (6 * ss / 60);
// 时针每分钟走的位置
hhDeg = hhDeg1 + (30 * mm / 60);
hhObj.style.transform = "rotate(" + hhDeg + "deg)";
mmObj.style.transform = "rotate(" + mmDeg + "deg)";
ssObj.style.transform = "rotate(" + ssDeg + "deg)";
ss += 1;
if (ss > 60) {
ss = 1;
mm += 1;
}
if (mm == 60) {
mm = 0;
hh += 1;
}
setTimeout(function() {
drawSS();
}, 1000);
}
drawSS();
</script>
</body>
</html>
精简版:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS3时钟特效</title>
<link rel="shortcut icon" type="image/x-icon" href="img/an.ico" />
<style>
/*表盘边框*/
.clock {
/* 设置大小 */
width: 400px;
height: 400px;
position: relative;
margin: 40px auto;
/*上边距*/
border-radius: 50%;
/*圆形*/
box-shadow: 5px 5px 20px rgba(0, 0, 0, 0.5);
/*表盘阴影*/
background: #F5DEB3;
border: 10px solid #FFFF00;
}
/*画刻度的面板*/
.box {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
/*用来装刻度的div*/
.box div {
width: 0px;
height: 200px;
position: absolute;
left: 200px;
/*旋转*/
transform: rotate(0deg);
/*设置基点为右下角*/
transform-origin: bottom right;
background: rgba(255, 0, 0, 0.5);
}
/*小刻度*/
.box div:after {
content: "";
position: absolute;
background: #484848;
width: 2px;
height: 10px;
left: -1px;
}
/*大刻度*/
.box div.five:after {
position: absolute;
content: "";
width: 4px;
height: 20px;
left: -2px;
top: 0;
background: #484848;
border-radius: 0 0 2px 2px;
}
/*秒针样式*/
.second {
width: 1px;
height: 200px;
background: red;
position: absolute;
left: 200px;
/*距离表盘宽度一半*/
margin-top: 30px;
z-index: 10;
transform: rotate(0deg);
transform-origin: center 170px;
/*定位旋转位置*/
}
/*圆心样式*/
.second:after {
content: "";
position: absolute;
width: 20px;
height: 20px;
background: red;
border-radius: 50%;
bottom: 20px;
left: -10px;
}
/*分针样式*/
.minute {
width: 2px;
height: 140px;
background: #8b8b8d;
position: absolute;
left: 199px;
margin-top: 60px;
z-index: 9;
transform-origin: center bottom;
transform: rotate(12deg);
}
/*时针样式*/
.hour {
width: 6px;
height: 100px;
background: #333;
position: absolute;
left: 197px;
margin-top: 100px;
z-index: 8;
border-radius: 3px;
transform: rotate(2deg);
transform-origin: center bottom;
}
</style>
</head>
<body>
<div class="clock">
<div class="box"></div>
<div class="second"></div>
<div class="minute"></div>
<div class="hour"></div>
</div>
<script>
var box = document.getElementsByClassName("box")[0];
var ssObj = document.getElementsByClassName("second")[0];
var mmObj = document.getElementsByClassName("minute")[0];
var hhObj = document.getElementsByClassName("hour")[0];
/*获取当前时间*/
var date = new Date();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
/*计算页面指针加载时的角度*/
drawSS();
// 定义初始刻度的度数
var Deg = 0;
/*画刻度*/
for (var i = 0; i < 60; i++) {
var div1 = document.createElement("div"); //创建一个div
//当为5时
if (i % 5 == 0) { //大刻度
div1.className = "five";
}
div1.style.transform = "rotate(" + Deg + "deg)";
box.appendChild(div1);
Deg += 6;// 每两个刻度之间是6度
}
/*指针转动的函数*/
function drawSS() {
// 秒针的度数
ssDeg = 360 * ss / 60;
// 分针的度数
mmDeg = 360 * mm / 60 + (6 * ss / 60);
// 时针的度数
hhDeg = 360 * (hh % 12) / 12 + (30 * mm / 60);
// 旋转
hhObj.style.transform = "rotate(" + hhDeg + "deg)";
mmObj.style.transform = "rotate(" + mmDeg + "deg)";
ssObj.style.transform = "rotate(" + ssDeg + "deg)";
ss += 1;
// 每秒钟调用一次
setTimeout(function() {
drawSS();
}, 1000);
}
</script>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
js,css3,时钟
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
