HTML5web前端开发:掌握新技术,提升用户体验
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.clock {
display: flex;
justify-content: space-around;
align-items: center;
background-color: #f7f7f7;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="clock">
<div id="hour"></div>
<div id="minute"></div>
<div id="second"></div>
</div>
<script>
function updateClock(selector, time) {
var date = new Date();
var value = date[time];
document.getElementById(selector).innerHTML = value < 10 ? '0' + value : value;
}
setInterval(function() {
updateClock('hour', 'getHours');
updateClock('minute', 'getMinutes');
updateClock('second', 'getSeconds');
}, 1000);
</script>
</body>
</html>
这段代码使用了CSS Flexbox布局来创建一个简单的时钟界面,并通过JavaScript的setInterval函数每秒更新时间显示。它展示了如何将HTML、CSS和JavaScript结合起来创建一个动态的数字时钟。
评论已关闭