<!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 {
font-family: Arial, sans-serif;
transition: background-color 0.5s ease;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
body.day {
background-color: #f0f9ff;
color: #333;
}
body.night {
background-color: #1a202c;
color: #fff;
}
#toggle-btn {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
background-color: #4299e1;
color: white;
transition: background-color 0.3s ease;
}
#toggle-btn:hover {
background-color: #3182ce;
}
#link {
position: absolute;
bottom: 20px;
right: 20px;
}
#link a {
color: inherit;
text-decoration: none;
transition: color 0.3s ease;
}
#link a:hover {
text-decoration: underline;
}
</style>
</head>
<body class="day">
<h1>欢迎体验白天黑夜切换</h1>
<button id="toggle-btn">切换模式</button>
<div id="link">
</div>
<script>
const toggleBtn = document.getElementById('toggle-btn');
const body = document.body;
toggleBtn.addEventListener('click', function () {
if (body.classList.contains('day')) {
body.classList.remove('day');
body.classList.add('night');
toggleBtn.textContent = '切换到白天';
} else {
body.classList.remove('night');
body.classList.add('day');
toggleBtn.textContent = '切换到黑夜';
}
});
</script>
</body>
</html>