Javascript
Light AND Dark Mode Toggle
HiroDaegu
2022. 5. 16. 01:06
728x90
SMALL
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LIGHT N DARK MODE TOGGLE</title>
<link rel="stylesheet" href="style.css">
<script defer src="main.js"></script>
</head>
<body>
<div class="night-toggle" onclick="switchMode()">
<div id="moon" class="moon"></div>
</div>
<div class="opt">
<div>
<h1>Night/Day Mode Toggle</h1>
</div>
<div class="break"></div>
<div>
<p>Click on the moon in the upper-right corner to change to night mode</p>
<p>Click again on the sun to change back to day mode</p>
</div>
</div>
</body>
</html>
body {
transition: 1.5s;
}
.night-toggle {
width: 33px;
height: 33px;
position: absolute;
right: 20px;
top: 20px;
}
.night-toggle:hover {
cursor: pointer;
}
.moon {
background-color: transparent;
box-shadow: -6px 1px 0 3px #275E8E;
border-left: 3px solid #27476D;
border-radius: 50%;
width: 20px;
height: 20px;
margin-left: 8px;
margin-top: 0;
transition: 2s;
}
.sun {
background-color: #FDD462;
box-shadow: 2px 0px 0px 1px #D19C29;
border-radius: 50%;
width: 26px;
height: 26px;
transition: 2s;
}
.break {
flex-basis: 100%;
height: 0;
}
.opt {
display: flex;
flex-wrap: wrap;
justify-content: center;
font-family: sans-serif;
margin-top: 5%;
text-align: center;
}
function switchMode() {
let moon = document.getElementById("moon");
if(moon.className == "moon") {
moon.className = "sun"
document.body.style.backgroundColor = "#141D26";
document.body.style.color = "#fff";
} else {
moon.className = "moon";
document.body.style.backgroundColor = "#fff";
document.body.style.color = "#000";
}
}
728x90
LIST