๐จ Frontend/JavaScript
๐ฆVanilla JS 8-(1) ์ด๋ฒคํธ ๋ค๋ฃจ๊ธฐ + ์ค์ต ์์ 3๊ฐ
kongmi
2023. 4. 1. 08:29
- ์ด๋ฒคํธ๋ ์น ๋ธ๋ผ์ฐ์ ์ ์ฌ์ฉ์ ์ฌ์ด์ ์ํธ์์ฉ์ด ๋ฐ์ํ๋ ํน์ ์์ ์ ์๋ฏธ ํฉ๋๋ค.
- ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ์ด๋ฒคํธ ์ข ๋ฅ์ ๋ฐ๋ผ ์ด๋ค ์์ ์ ํ๊ฑฐ๋ ๋ฏธ๋ฆฌ ๋ฑ๋กํ ํจ์๋ฅผ ํธ์ถํ๋ ๋ฑ์ ์กฐ์์ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์ง์ ํ ์ ์์ต๋๋ค.
์ด๋ฒคํธ ์ข ๋ฅ
1. ์ธ๋ผ์ธ ๋ฐฉ์
<body>
<button onclick="clickEvent()">์ธ๋ผ์ธ ๋ฐฉ์</button>
<script src="./230331_2_0_์ด๋ฒคํธ๋ฑ๋ก.js"></script>
</body>
// 1. ์ธ๋ผ์ธ ๋ฐฉ์
function clickEvent() {
alert("์ธ๋ผ์ธ ๋ฐฉ์์ผ๋ก ๊ตฌํ๋ ํจ์๊ฐ ํธ์ถ ๋์์ต๋๋ค!");
}
2. ์ด๋ฒคํธ ๋ฑ๋ก ๋ฉ์๋(addEventListener())
- DOM์์ ์ ๊ณตํ๋ addEventListener() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ด๋ฒคํธ๋ฅผ ๋ฑ๋กํ ์ ์์ต๋๋ค.
- ๊ฐ์ฅ ๊ถ์ฅํ๋ ๋ฐฉ์์ ๋๋ค.
<๋
ธ๋>.addEventListener("<์ด๋ฒคํธ ํ์
>", <์ด๋ฒคํธ ํจ์>);
// โญ๏ธ 2. ์ด๋ฒคํธ ๋ฑ๋ก ๋ฉ์๋ ๋ฐฉ์
function eventFunc() {
alert("์ด๋ฒคํธ ๋ฑ๋ก ๋ฉ์๋๋ก ๊ตฌํ๋ ํจ์๋ฅผ ํธ์ถ");
}
let regBtnEl = document.getElementById("reg_btn");
regBtnEl.addEventListener("click", eventFunc); // ์ฝ๋ฐฑํจ์
์ค์ต๋ฌธ์ (1) - ๋ฒํผ ํด๋ฆญํ๋ฉด ๊ฒฝ๊ณ ์ฐฝ ์ถ๋ ฅํ๊ธฐ
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
<style>
.container {
display: flex;
gap: 10px;
}
.item {
width: 150px;
height: 60px;
text-align: center;
font-size: 1.5em;
border-radius: 10px;
box-shadow: 2px 2px 2px #000;
color: #171717;
font-weight: bold;
border: none;
}
.item1 {
background: rgb(53,168,0);
background: radial-gradient(circle, rgba(53,168,0,1) 0%, rgba(238,238,238,1) 100%);
}
.item2 {
background: rgb(255,100,0);
background: radial-gradient(circle, rgba(255,100,0,1) 0%, rgba(238,238,238,1) 100%);
}
.item3 {
background: rgb(147,0,255);
background: radial-gradient(circle, rgba(147,0,255,1) 0%, rgba(238,238,238,1) 100%);
}
</style>
</head>
<body>
<div class="container">
<button class="item item1">Green</button>
<button class="item item2">Orange</button>
<button class="item item3">Purple</button>
</div>
<script src="./230331_2_1_์ค์ต๋ฌธ์ .js"></script>
</body>
</html>
// ์ด๋ฒคํธ ๋ฑ๋ก ๋ฉ์๋ ๋ฐฉ์
const green_box = document.querySelector(".item1");
green_box.addEventListener("click", function() {
alert("green!!");
});
const orange_box = document.querySelector(".item2");
orange_box.addEventListener("click", function() {
alert("orange!!");
});
const purple_box = document.querySelector(".item3");
purple_box.addEventListener("click", function() {
alert("purple!!");
});
์ค์ต๋ฌธ์ (2) - ๋ฒํผ ํด๋ฆญํ๋ฉด ๋ฐฐ๊ฒฝ์ ๋ฐ๊พธ๊ธฐ
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
<style>
.container {
display: flex;
flex-direction: column;
gap: 10px;
justify-content: center;
align-items: center;
}
.sub-container {
display: flex;
gap: 10px;
}
.box {
width: 30%;
height: 150px;
border-radius: 20px;
border: 1px solid #bdbcbc;
}
.item {
width: 150px;
height: 40px;
text-align: center;
border-radius: 10px;
color: #fff;
font-weight: bold;
border: none;
}
.item1 {
background-color: green;
}
.item2 {
background-color: orange;
}
.item3 {
background-color: purple;
}
</style>
</head>
<body>
<div class="container">
<div class="sub-container">
<button class="item item1">Green</button>
<button class="item item2">Orange</button>
<button class="item item3">Purple</button>
</div>
<div class="box"></div>
</div>
<script src="./230331_2_2_์ค์ต๋ฌธ์ .js"></script>
</body>
</html>
const green_box = document.querySelector(".item1");
green_box.addEventListener("click", function() {
const box = document.querySelector(".box");
box.style.background = "green";
});
const orange_box = document.querySelector(".item2");
orange_box.addEventListener("click", function() {
const box = document.querySelector(".box");
box.style.background = "orange";
});
const purple_box = document.querySelector(".item3");
purple_box.addEventListener("click", function() {
const box = document.querySelector(".box");
box.style.background = "purple";
});
์ค์ต๋ฌธ์ (3) - ๋ฒํผ ํด๋ฆญ์ ์์ธ ์ค๋ช ํ์ ๋ฐ ๋ซ๊ธฐ
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
<style>
* {
margin: 0;
}
.container {
width: 400px;
margin: 30px auto;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.image {
background-image: url("./แแ
กแฏแ
แ
ตแแ
ฎแแ
ณ.png");
background-size: contain;
background-repeat: no-repeat;
width: 400px;
height: 400px;
position: relative;
}
.desc {
display: none
}
#detail {
border: 1px solid #c7c6c6;
width: 30px;
height: 30px;
text-align: center;
border-radius: 10px;
background: rgb(238,174,202);
background: radial-gradient(circle, rgba(238,174,202,1) 0%, rgba(238,238,238,1) 100%);
color: #000;
font-weight: bold;
position: relative;
left: -170px;
top: -40px;
opacity: 0.7;
}
</style>
</head>
<body>
<div class="container">
<div class="image"></div>
<button id="detail">โผ</button>
<div class="desc">
<h1>์ฐฐ๋ฆฌ ํธ์ค(Charlie Puth)</h1>
<p>2015๋
See You Again์ด๋ผ๋ ๋ฉ๊ฐ ํํธ๊ณก์ผ๋ก ๋น๋ณด๋์ ๋ฑ์ฅํ, ์๊ณก๊ณผ ํ๋ก๋์ฑ ๋ฅ๋ ฅ๊น์ง ๊ฒธ๋นํ ์ฑ์ด์ก๋ผ์ดํฐ์ด๋ค. ๋ฏธ๊ตญ ๋ณด์คํด์ ๋ช
๋ฌธ ๋ฒํด๋ฆฌ ์์
๋ํ ์ ์ก ์ฅํ์์ด๋ค. ๋ณธ๋ 2009๋
9์๋ถํฐ ์ ํ๋ฒ ์
ฐ์ธ ๋์จ์ ํ์ดํ๊ณก์ ์๊ณกํ๊ฑฐ๋, ๋ณธ์ธ์ ์ ํ๋ธ ํ์ด์ง์ ์ปค๋ฒ ๊ณก ๋๋ ์ค๋ฆฌ์ง๋ ๊ณก์ ์ฌ๋ฆฌ๋ ๋ฑ ๋ง์ด๋ํ ์ ํ๋ฒ์๋ค. ํ์ฌ ์ ํ๋ธ ๊ณต์ ๊ณ์ ์ด ๋ฐ๋ทํ๊ธฐ ์ ๋ถํฐ ์ฌ์ฉํด ์ค๋ ๊ณ์ [5]์ธ๋ฐ, ๊ฐ์๋ก ๋ฐ๋ท๋ฅผ ํ๋ฉด์ ์์ ์ ์ฌ๋ฆฐ ์ปค๋ฒ ๋ฐ ์ค๋ฆฌ์ง๋ ๊ณก ์์๋ค ์ค ๋ ๊ฐ๋ฅผ ์ ์ธํ๊ณ ๋ณธ์ธ ๊ณ์ ์์ ๋ณผ ์ ์๊ฒ ๋์๋ค. ์๋ง ๋น๊ณต๊ฐ๋ก ์ ํ์ํจ ๋ฏํ๋ค. ๋จ์์๋ ๋ ์์ ์ค ํ๋๊ฐ Sia์ Chandelier ์ปค๋ฒ ๊ณก ์์์ผ๋ก, ํ์ ๋ฏผ์๋งค๋ฅผ ์
๊ณ ํ์จํ๊ฒ ์ค์ฐํ
ํ๋ ์ฐฐ๋ฆฌ ํธ์ค์ ๋ชจ์ต์ ๋ณผ ์ ์๋ค.</p>
</div>
</div>
<script src="./230331_2_3_์ค์ต๋ฌธ์ .js"></script>
</body>
</html>
const detailBtn = document.querySelector("#detail");
detailBtn.addEventListener("click", function() {
const desc = document.querySelector(".desc");
console.log(desc);
if(desc.style.display == 'none') {
desc.style.display = 'block';
} else desc.style.display = 'none';
});