一个简单的HTML+CSS+JS应用案例是创建一个倒计时器。以下是实现这个功能的代码:
1. HTML代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>倒计时器</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="countdown">
<span id="days"></span>天
<span id="hours"></span>小时
<span id="minutes"></span>分钟
<span id="seconds"></span>秒
</div>
<script src="script.js"></script>
</body>
</html>
```
2. CSS代码(styles.css):
```css
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.countdown {
display: flex;
justify-content: center;
align-items: center;
}
span {
font-size: 2rem;
margin: 0 1rem;
}
```
3. JavaScript代码(script.js):
```javascript
const countdown = ElementById('countdown');
css简单网页代码const daysElement = ElementById('days');
const hoursElement = ElementById('hours');
const minutesElement = ElementById('minutes');
const secondsElement = ElementById('seconds');
const targetDate = new Date('2022-12-31T00:00:00'); // 设置目标日期和时间
function updateCountdown() {
const currentTime = new Date();
const timeDifference = targetDate - currentTime;
const days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);
Content = days;
Content = hours;
Content = minutes;
Content = seconds;
}
updateCountdown();
setInterval(updateCountdown, 1000); // 每秒更新一次倒计时
```
将以上代码分别保存为index.html、styles.css和script.js文件,然后用浏览器打开index.html文件,即可看到一个简单的倒计时器应用。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论