์ค๋ ํด ๋ณผ ์ค์ต์ ๋๋ค.
โ ๋ชฉํ
- ๋ฒํผ ํ๋๋ฅผ ํด๋ฆญํ๋ฉด์ ๋คํฌ๋ชจ๋, ๋ผ์ดํธ๋ชจ๋๋ฅผ ๋ณํํฉ๋๋ค.
- ๋คํฌ๋ชจ๋, ๋ผ์ดํธ๋ชจ๋์ ์๋ฐ์คํฌ๋ฆฝํธ ์์ฑ ์ toggle์ ์ฌ์ฉํฉ๋๋ค.
๋จผ์ , html ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.
์ด ์ค์ต์ ๋คํฌ๋ชจ๋, ๋ผ์ดํธ๋ณํ ์๋ฐ์คํฌ๋ฆฝํธ ์ค์ต์ ์ํ ๊ฒ์ด๋ html์ ๊ฐ๋จํ ํ๊ฒ ์ต๋๋ค.
โ Html
- ๊ฐ์ด๋ฐ ์ค์ ์ ๋ ฌ๋ก div ๋ฐ์ค๋ฅผ ๋ง๋ญ๋๋ค.
- ์ค๋ฅธ์ชฝ ์๋จ์ ๋ฒํผ์ ๊ณ ์ ํฉ๋๋ค.
<body class="wrap light-mode">
<div>
<h1>Web Programming</h1>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>Javascipt</li>
</ul>
</div>
<button id="modeButton">
๋คํฌ ๋ชจ๋
</button>
</body>
- body ์ ์ฒด์ ํด๋์ค๋ฅผ ์ฃผ์ด ๋ฒํผ์ ๋๋ฅผ ๋๋ง๋ค body ์ ์ฒด์ ๋ฐฐ๊ฒฝ์, ๊ธ์ ์์ ํ๋ฒ์ ๋ณ๊ฒฝํฉ๋๋ค.
- body ํด๋์ค์ light-mode ํด๋์ค๋ฅผ ์ถ๊ฐํฉ๋๋ค.
- ๋ฒํผ์ด ๋๋ฆด ๋ dark-mode๋ก ํด๋์ค๋ฅผ ๋ณ๊ฒฝํ๋ฉด dark-mode CSS ๊ฐ ์ ์ฉ๋๊ณ
- light-mode๋ก ํด๋์ค๋ฅผ ๋ณ๊ฒฝํ๋ฉด light-mode CSS๊ฐ ์ ์ฉ๋ฉ๋๋ค.
- ์ด๋, dark-mode ์ธ ์ํฉ์์ ๋ฒํผ์ ๋๋ฅด๋ฉด light-mode๋ก ๋ณ๊ฒฝ๋๊ณ
- light-mode์ธ ์ํฉ์์ ๋ฒํผ์ ๋๋ฅด๋ฉด dark-mode๋ก ๋ณ๊ฒฝ๋๊ฒ ํ๊ธฐ ์ํด
- toggle์ ์ฌ์ฉํฉ๋๋ค.
โ CSS
<style>
*{
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
button{
position: fixed;
top: 20px;
right: 20px;
padding: 10px;
background-color: white;
color: black;
}
.dark-mode{
background-color: black;
color: white;
}
.dark-mode button{
background-color: black;
border: 1px solid white;
color: white;
}
.light-mode{
background-color: white;
color: black;
}
</style>
- ๋ชจ๋๊ฐ ๋ฐ๋๋ฉด ๋ฒํผ ๋ด๋ถ ์๊ณผ ๊ธ์ ์๋ ๋ณ๊ฒฝํด์ผ ํ๋ฏ๋ก
- .dark-mode button
- .dark-mode ํด๋์ค ์์ ์๋ button ์ผ๋ก CSS ๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
โ JavaScript
<script>
document.addEventListener('DOMContentLoaded', function(){
const modeBtn = document.querySelector('#modeButton');
const body= document.body;
modeBtn.addEventListener('click', (e)=>{
body.classList.toggle('dark-mode');
body.classList.toggle('light-mode');
if(body.classList.contains('dark-mode')){
modeBtn.innerText = "๋คํฌ ๋ชจ๋";
}else{
modeBtn.innerText = "๋ผ์ดํธ ๋ชจ๋";
}
});
});
</script>
DomContentLoaded :
๋ฌธ์ ๋ด์ฉ์ด ๋ชจ๋ ๋ก๋๋์์ ๋ ์คํํ์ฌ, ๋คํธ์ํฌ ๋ฌธ์ ๊ฐ ์์ ๋์ ๋ฐ์ํ ์ ์๋ CSS ์ค๋ฅ๋ฅผ ๋ฐฉ์งํฉ๋๋ค.
๐ก ํด๋์ค ์์ฑ toggle ํ๊ธฐ ๐ก
toggle์ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด ์๋์ ๊ฐ์ ์ฝ๋๋ก ์์ฑํ ์ ์์ต๋๋ค.
document.addEventListener('DOMContentLoaded', function(){
const modeBtn = document.querySelector('#modeButton');
const body= document.body;
modeBtn.addEventListener('click', (e)=>{
// body.classList.toggle('dark-mode');
// body.classList.toggle('light-mode');
if(body.classList.contains('dark-mode')){
modeBtn.innerText = "๋คํฌ ๋ชจ๋";
body.classList.remove('dark-mode');
body.classList.add('light-mode');
}else{
modeBtn.innerText = "๋ผ์ดํธ ๋ชจ๋";
body.classList.add('dark-mode');
body.classList.remove('light-mode');
}
});
});
ํ์ง๋ง remove์ add๋ฅผ ์ญ์ ํ๊ณ ๋ค์ ์ถ๊ฐํ๋ ๊ณผ์ ์ด ๊ฝค ๋ฒ๊ฑฐ๋กญ์ต๋๋ค.
์ด๋ด ๋ ์ฌ์ฉํ ์ ์๋ ์์ฑ์ด toggle ์ ๋๋ค.
toggle์ด๋, ์จ/์คํ, ๋๊ธฐ/์ผ๊ธฐ ์ฒ๋ผ 2๊ฐ์ง ์ํ๋ฅผ ๋ฒ๊ฐ์ ๋ฐ๊พธ๋ ๊ฒ์ ๋งํฉ๋๋ค.
toggle ํจ์๋ ์ด ์ค์ต์ฒ๋ผ
- ํด๋์ค ์ด๋ฆ๋ง ์ง์ ํ ์๋ ์๊ณ ,
- ์กฐ๊ฑด์ ํจ๊ป ์ฌ์ฉํ์ฌ ์กฐ๊ฑด์ด true์ธ ๊ฒฝ์ฐ์ ํด๋น ํด๋์ค๋ฅผ ํ ๊ธํ๋๋ก ์ง์ ํ๊ธฐ๋ ํฉ๋๋ค.
toggle(ํด๋์ค๋ช
)
toggle(ํด๋์ค๋ช
, ์กฐ๊ฑด)
๊ธฐ๋ณธํ์ ์ด๋ ๊ฒ 2๊ฐ์ง์ ๋๋ค.
1๋ฒ ๊ธฐ๋ณธํ์ ์๋์ ๊ฐ์ต๋๋ค
// ํด๋์ค ์ด๋ฆ์ dark-mode๋ก ๋ณ๊ฒฝํฉ๋๋ค.
toggle('dark-mode')
2๋ฒ ๊ธฐ๋ณธํ๊ณผ ๊ฐ์ด ์กฐ๊ฑด์ ํจ๊ป ์ฐ๋ ๊ฒฝ์ฐ์ ๋๋ค.
//this๊ฐ ์ฒดํฌ๊ฐ ๋ ๊ฒฝ์ฐ, ํด๋์ค๋ช
์ 'dark-mode'๋ก ๋ณ๊ฒฝํฉ๋๋ค.
//checked ๊ฐ ์ฌ์ฉ๋์์ผ๋ฏ๋ก this๋ checkbox ์
๋๋ค.
toggle('dark-mode', this.checked)
โ ์ ์ฒด ์ฝ๋
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>๋คํฌ ๋ชจ๋</title>
<!-- light-mode, dark-mode css ๊ฐ๊ฐ ๋ง๋ค๊ณ
.light-mode
.wrap .dark-mode
script :
๋ฒํผ ๋๋ฅด๋ฉด toggle๋ก
classList์ dark-mode ํฌํจ๋์ด ์์ผ๋ฉด ~ ๋ฒํผ text ๋ฐ๊พธ๊ธฐ
-->
<style>
*{
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
button{
position: fixed;
top: 20px;
right: 20px;
padding: 10px;
background-color: white;
color: black;
}
.dark-mode{
background-color: black;
color: white;
}
.dark-mode button{
background-color: black;
border: 1px solid white;
color: white;
}
.light-mode{
background-color: white;
color: black;
}
</style>
</head>
<body class="wrap light-mode">
<div>
<h1>Web Programming</h1>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>Javascipt</li>
</ul>
</div>
<button id="modeButton">
๋คํฌ ๋ชจ๋
</button>
<script>
document.addEventListener('DOMContentLoaded', function(){
const modeBtn = document.querySelector('#modeButton');
const body= document.body;
modeBtn.addEventListener('click', (e)=>{
body.classList.toggle('dark-mode');
body.classList.toggle('light-mode');
if(body.classList.contains('dark-mode')){
modeBtn.innerText = "๋คํฌ ๋ชจ๋";
}else{
modeBtn.innerText = "๋ผ์ดํธ ๋ชจ๋";
}
});
});
//toggle ์ ์ฌ์ฉํ์ง ์์ผ๋ฉด
// document.addEventListener('DOMContentLoaded', function(){
// const modeBtn = document.querySelector('#modeButton');
// const body= document.body;
// modeBtn.addEventListener('click', (e)=>{
// // body.classList.toggle('dark-mode');
// // body.classList.toggle('light-mode');
// if(body.classList.contains('dark-mode')){
// modeBtn.innerText = "๋คํฌ ๋ชจ๋";
// body.classList.remove('dark-mode');
// body.classList.add('light-mode');
// }else{
// modeBtn.innerText = "๋ผ์ดํธ ๋ชจ๋";
// body.classList.add('dark-mode');
// body.classList.remove('light-mode');
// }
// });
// });
</script>
</body>
</html>