JS中有关数组的操作
思维导图如下
设计案例有如下
如何设计出电子时钟
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>电子时钟</title>
<style>
h1 {
text-align: center;
}
</style>
</head>
<body>
<h1></h1>
</body>
<script>
var h1 = document.getElementsByTagName("h1")[0];
function setH1(){
// 获取时间戳
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var riqi = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var str = year + "/" + month + "/" + riqi + " " + hour + ":" + minute + ":" + second;
// 通过innerHTML对标签的内容进行获取和修改
h1.innerHTML = str;
}
// 函数的执行
setH1();
// 定时器
// 每间隔1000毫秒 执行一次function
var timer = setInterval (function() {
setH1();
},1000)
</script>
</html>
代码实现图如下
为实时更新的数字电子时钟
关于实现以下数字时钟的操作
代码段如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>时钟</title>
<style>
* {
margin: 0;
padding: 0;
}
.clock {
width: 200px;
height: 200px;
border: 10px solid lightgray;
margin: 0 auto;
border-radius: 50%;
box-shadow: 5px 5px 5px lightgray;
background-color: #fff;
position: relative
}
.clock p {
width: 200px;
height: 20px;
/* background-color: pink; */
line-height: 20px;
position: absolute;
top: 90px;
}
.clock p span {
display: block;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
/* background-color: gold; */
}
#hour {
position: absolute;
transform-origin: center bottom;
width: 6px;
height: 50px;
background-color: tomato;
left: 97px;
top: 50px;
}
#minute {
position: absolute;
transform-origin: center bottom;
width: 4px;
height: 70px;
background-color: deepskyblue;
left: 98px;
top: 30px;
}
#second {
position: absolute;
transform-origin: center bottom;
width: 2px;
height: 90px;
background-color: hotpink;
left: 99px;
top: 10px;
}
</style>
</head>
<body>
<div class="clock">
<p><span>1</span></p>
<p><span>2</span></p>
<p><span>3</span></p>
<p><span>4</span></p>
<p><span>5</span></p>
<p><span>6</span></p>
<p><span>7</span></p>
<p><span>8</span></p>
<p><span>9</span></p>
<p><span>10</span></p>
<p><span>11</span></p>
<p><span>12</span></p>
<div id="hour"></div>
<div id="minute"></div>
<div id="second"></div>
</div>
</body>
<script>
// 获取数字
var ps = document.getElementsByTagName("p");
// 获取三个指针
var dHour = document.getElementById("hour");
var dMinute = document.getElementById("minute");
var dSecond = document.getElementById("second");
//定时器
var timer = null;
// 设置数字的位置
function setNums() {
for (var i=0; i<ps.length; i++) {
// ps[i].style.transform = `rotate(${120 + i*30}deg)`;
ps[i].style.transform = 'rotate(' + (120 + i*30) + 'deg)';
// 获取p下边的span标签
var span = ps[i].getElementsByTagName("span")[0];
span.style.transform = 'rotate(' + ((120 + i*30)*-1) + 'deg)';
}
}
setNums();
// 开启定时器
timer = setInterval(function() {
changeZhen();
}, 1000)
function changeZhen() {
// 获取时间戳
var date = new Date();
var s = date.getSeconds();
var m = date.getMinutes() + s/60;
var h = date.getHours() + m/60;
dSecond.style.transform = `rotate(${s * 6}deg)`;
dMinute.style.transform = `rotate(${m * 6}deg)`;
dHour.style.transform = `rotate(${h * 30}deg)`;
}
</script>
</html>
实现图如下
计时器以及date
思维导图如下