HTML5与CSS3实现动态网页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>动态网页示例</title>
<style>
body, html {
height: 100%;
margin: 0;
font-family: Arial, sans-serif;
}
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
background-color: #f7f7f7;
}
.clock {
text-align: center;
padding: 20px;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.clock-time {
font-size: 48px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<div class="clock">
<div class="clock-time"></div>
</div>
</div>
<script>
function updateClock() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
timeString = hours + ':' + minutes + ':' + seconds;
document.querySelector('.clock-time').textContent = timeString;
}
setInterval(updateClock, 1000);
updateClock();
</script>
</body>
</html>
这段代码使用了CSS3的Flexbox布局来居中容器,并通过JavaScript设置了一个简单的动态时钟。它展示了如何结合使用HTML5和CSS3来创建一个动态的网页,并且代码保持简洁。
评论已关闭