写一个结合JS登录验证、网页开关灯、轮播图、倒计时功能的网页
时间: 2023-09-03 21:06:12 浏览: 37
好的,我可以为您提供一个简单的网页示例,结合了JS登录验证、网页开关灯、轮播图、倒计时功能。以下是示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>网页示例</title>
<style>
body {
background-color: white;
color: black;
}
.dark {
background-color: black;
color: white;
}
#slideshow {
position: relative;
height: 300px;
overflow: hidden;
}
#slideshow img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;
transition: opacity 1s;
}
#slideshow img.active {
opacity: 1;
}
</style>
</head>
<body>
<h1>网页示例</h1>
<!-- 登录表单 -->
<form id="login-form">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br>
<input type="submit" value="登录">
</form>
<!-- 开关灯按钮 -->
<button id="light-switch">开灯</button>
<!-- 轮播图 -->
<div id="slideshow">
<img src="https://via.placeholder.com/800x300.png?text=Slide+1" class="active">
<img src="https://via.placeholder.com/800x300.png?text=Slide+2">
<img src="https://via.placeholder.com/800x300.png?text=Slide+3">
</div>
<!-- 倒计时 -->
<div id="countdown"></div>
<script>
// 登录验证
const loginForm = document.querySelector('#login-form');
loginForm.addEventListener('submit', function(event) {
event.preventDefault();
const username = document.querySelector('#username').value;
const password = document.querySelector('#password').value;
// 发送AJAX请求进行登录验证
// 这里只是一个示例,具体实现需要后端支持
if (username === 'admin' && password === 'password') {
alert('登录成功');
} else {
alert('用户名或密码错误');
}
});
// 开关灯
const lightSwitch = document.querySelector('#light-switch');
const body = document.querySelector('body');
lightSwitch.addEventListener('click', function() {
body.classList.toggle('dark');
// 保存设置到本地
if (body.classList.contains('dark')) {
localStorage.setItem('light', 'off');
} else {
localStorage.setItem('light', 'on');
}
});
// 加载上次的灯光设置
const light = localStorage.getItem('light');
if (light === 'off') {
body.classList.add('dark');
}
// 轮播图
const slideshow = document.querySelector('#slideshow');
const slides = slideshow.querySelectorAll('img');
let slideIndex = 0;
function showSlide(index) {
slides.forEach(slide => slide.classList.remove('active'));
slides[index].classList.add('active');
}
function nextSlide() {
slideIndex++;
if (slideIndex >= slides.length) {
slideIndex = 0;
}
showSlide(slideIndex);
}
setInterval(nextSlide, 3000);
// 倒计时
const countdown = document.querySelector('#countdown');
const endDate = new Date('2022-01-01');
function updateCountdown() {
const now = new Date();
const diff = endDate - now;
if (diff <= 0) {
countdown.innerHTML = '倒计时结束!';
return;
}
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
countdown.innerHTML = `距离2022年1月1日还有 ${days} 天 ${hours} 小时 ${minutes} 分钟 ${seconds} 秒`;
}
setInterval(updateCountdown, 1000);
</script>
</body>
</html>
```
这个示例包含了一个登录表单,一个开关灯按钮,一个轮播图和一个倒计时。登录表单和开关灯按钮使用了JS实现,轮播图和倒计时使用了JS和CSS实现。您可以在本地打开这个HTML文件,查看示例效果。