์๋ฐ์คํฌ๋ฆฝํธ๋ก ์ฌ๋ผ์ด๋ ํ ๊ธ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ (SlideUp / SlideDown)
์น ํ์ด์ง์์ ์ฝํ ์ธ ๋ฅผ ๋ถ๋๋ฝ๊ฒ ์จ๊ธฐ๊ฑฐ๋ ํ์ํ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉ์ ๊ฒฝํ์ ํฅ์์ํค๋ ํต์ฌ ์์์ ๋๋ค. ํนํ SlideUp ๊ณผ SlideDown ํจ๊ณผ๋ ์ง๊ด์ ์ธ UI๋ฅผ ๊ตฌํํ ๋ ํฐ ๋์์ด ๋ฉ๋๋ค. ์ด ๊ธ์์๋ jQuery์ ์์ JavaScript(๋ฐ๋๋ผ JS)๋ฅผ ์ฌ์ฉํ ์ฌ๋ผ์ด๋ ํ ๊ธ ๊ธฐ๋ฅ ๊ตฌํ ๋ฐฉ๋ฒ์ ์๊ฐํฉ๋๋ค.
๐ก jQuery๋ก ์ฌ๋ผ์ด๋ ํ ๊ธ ๊ตฌํ
jQuery์์๋ .slideUp(), slideDown(), .slideToggle() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๊ฐํธํ๊ฒ ๊ตฌํํ ์ ์์ต๋๋ค.
$(document).ready(function() {
$("#toggleButton").click(function() {
$("#content").slideToggle(500); // 500ms ๋์ ์ฌ๋ผ์ด๋
});
});
์ ์ฝ๋์์๋ ๋ฒํผ ํด๋ฆญ ์ #content ์์๊ฐ ์ฌ๋ผ์ด๋๋๋ฉฐ ํ์๋๊ฑฐ๋ ์จ๊ฒจ์ง๋๋ค.
๐ก ์์ JavaScript (Pure JS)๋ก ์ฌ๋ผ์ด๋ ํจ๊ณผ ๊ตฌํ
jQuery ์์ด ์์ JavaScript๋ก ์ฌ๋ผ์ด๋ ํจ๊ณผ๋ฅผ ๊ตฌํํ๋ ค๋ฉด CSS์ JavaScript๋ฅผ ํจ๊ป ํ์ฉํด์ผ ํฉ๋๋ค.
function slideToggle(element, duration = 500) {
let el = document.querySelector(element);
if (!el) return;
if (el.style.maxHeight) {
el.style.maxHeight = null;
} else {
el.style.maxHeight = el.scrollHeight + "px";
}
}
// ์ฌ์ฉ ์์
document.getElementById("toggleButton").addEventListener("click", function() {
slideToggle("#content");
});
์ ์ฝ๋์์๋ maxHeight ์์ฑ์ ํ์ฉํด #content ์์์ ๋์ด๋ฅผ ๋ถ๋๋ฝ๊ฒ ์กฐ์ ํ๋ฉฐ ์ฌ๋ผ์ด๋ ํจ๊ณผ๋ฅผ ๊ตฌํํฉ๋๋ค.
๐ก ๋ฐ๋๋ผ JavaScript๋ก ๋ถ๋๋ฌ์ด SlideUp/SlideDown ๊ตฌํ
๋ฐ๋๋ผ JavaScript๋ฅผ ์ฌ์ฉํด ์ข ๋ ์ธ๋ฐํ ์ ๋๋ฉ์ด์ ์ ๊ตฌํํ ์๋ ์์ต๋๋ค. requestAnimationFrame() ์ ํ์ฉํ๋ฉด ๋ณด๋ค ์์ฐ์ค๋ฌ์ด ์์ง์์ ์ฐ์ถํ ์ ์์ต๋๋ค.
function slideUp(element, duration = 300) {
let el = document.querySelector(element);
el.style.display = "block";
content.style.overflow = "hidden";
let height = "0";
el.style.height = el.scrollHeight + "px";
setTimeout(() => {
el.style.transition = `height ${duration}ms ease-in-out`;
el.style.height = height + "px";
}, 10);
console.log(el.scrollHeight);
}
function slideDown(element, duration = 300) {
let el = document.querySelector(element);
el.style.display = "block";
content.style.overflow = "hidden";
let height = el.scrollHeight;
el.style.height = "0px";
setTimeout(() => {
el.style.transition = `height ${duration}ms ease-in-out`;
el.style.height = height + "px";
}, 10);
console.log(height);
}
// ์ฌ์ฉ ์์
document.getElementById("toggleButton").addEventListener("click", function () {
let content = document.querySelector("#content");
if (content.style.display === "none" || content.style.height === "0px") {
slideDown("#content");
} else {
slideUp("#content");
}
});
์ ์ฝ๋์์๋ height ์์ฑ์ ํ์ฉํด ์ฝํ ์ธ ๊ฐ ๋ถ๋๋ฝ๊ฒ ํผ์ณ์ง๊ณ ์ ํ๋๋ก ์ค์ ํ์ต๋๋ค. jQuery ์์ด๋ ์ ๋๋ฉ์ด์ ํจ๊ณผ๋ฅผ ๋์ฑ ์ ๊ตํ๊ฒ ๋ค๋ฃฐ ์ ์์ต๋๋ค.
๐ก ์ฌ๋ผ์ด๋ ์๋ ์กฐ์
ํ์ฌ๋ ๊ณ ์ ๋ ์๋๋ก ์ฌ๋ผ์ด๋๋์ง๋ง, ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ฐ๋ผ ์๋๋ฅผ ๋ณ๊ฒฝํ ์ ์๋๋ก ์ค์ ํ ์๋ ์์ต๋๋ค. ์ด๋ฅผ ์ํด duration ๊ฐ์ ๋์ ์ผ๋ก ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
let speed = 500; // ๊ธฐ๋ณธ ์๋
document.getElementById("speedControl").addEventListener("change", function(event) {
speed = event.target.value;
});
document.getElementById("toggleButton").addEventListener("click", function() {
slideToggle("#content", speed);
});
๐ก ์๋ ์ฌ๋ผ์ด๋ (์ผ์ ์๊ฐ ํ ์จ๊น)
ํ์ด์ง ๋ก๋ ํ ์ผ์ ์๊ฐ์ด ์ง๋๋ฉด ์๋์ผ๋ก ์ฝํ ์ธ ๊ฐ ์จ๊ฒจ์ง๋๋ก ์ค์ ํ ์๋ ์์ต๋๋ค.
setTimeout(() => {
slideUp("#content", 1000);
}, 5000); // 5์ด ํ ์ฝํ
์ธ ์จ๊น
๐ก ๋ค์ค ์์ ์ฌ๋ผ์ด๋
ํ ๋ฒ์ ๋ฒํผ ํด๋ฆญ์ผ๋ก ์ฌ๋ฌ ๊ฐ์ ์์๋ฅผ ๋์์ ์ฌ๋ผ์ด๋ ํ ์ ์์ต๋๋ค.
document.getElementById("toggleButton").addEventListener("click", function() {
document.querySelectorAll(".slide-item").forEach(item => {
slideToggle(item, 500);
});
});
๐ก ๋ถ๋๋ฌ์ด ํ์ด๋ ํจ๊ณผ ์ถ๊ฐ
์ฌ๋ผ์ด๋์ ํจ๊ป opacity ๋ฅผ ์กฐ์ ํ์ฌ ๋ถ๋๋ฌ์ด ํ์ด๋ ํจ๊ณผ๋ฅผ ์ค ์๋ ์์ต๋๋ค.
function slideFadeToggle(element, duration = 500) {
let el = document.querySelector(element);
if (!el) return;
el.style.transition = `height ${duration}ms ease-in-out, opacity ${duration}ms ease-in-out`;
if (el.style.opacity == 0 || el.style.display === "none") {
el.style.display = "block";
el.style.opacity = 1;
el.style.height = el.scrollHeight + "px";
} else {
el.style.opacity = 0;
el.style.height = "0px";
setTimeout(() => {
el.style.display = "none";
}, duration);
}
}
// ์ฌ์ฉ ์์
document.getElementById("toggleButton").addEventListener("click", function() {
slideFadeToggle("#content");
});
๐ก ์คํฌ๋กค ์์น์ ๋ฐ๋ฅธ ์๋ ์ฌ๋ผ์ด๋
์ฌ์ฉ์๊ฐ ํน์ ์์น๊น์ง ์คํฌ๋กคํ๋ฉด ์๋์ผ๋ก ์ฝํ ์ธ ๊ฐ ๋ํ๋๊ฑฐ๋ ์ฌ๋ผ์ง๊ฒ ๋ง๋ค ์๋ ์์ต๋๋ค.
window.addEventListener("scroll", function() {
let content = document.querySelector("#content");
if (window.scrollY > 200) {
slideDown("#content");
} else {
slideUp("#content");
}
});
์ด๋ฌํ ์ถ๊ฐ ๊ธฐ๋ฅ์ ์กฐํฉํ๋ฉด ๋์ฑ ์ง๊ด์ ์ด๊ณ ์ธํฐ๋ํฐ๋ธํ ์ฌ์ฉ์ ๊ฒฝํ์ ๊ตฌํํ ์ ์์ต๋๋ค! ๐
๐ก ๋ง๋ฌด๋ฆฌ
์ฌ๋ผ์ด๋ ์ ๋๋ฉ์ด์ ์ ์ธํฐ๋ํฐ๋ธํ UI๋ฅผ ์ ์ํ๋ ๋ฐ ๋งค์ฐ ์ ์ฉํฉ๋๋ค.
jQuery๋ฅผ ํ์ฉํ๋ฉด ๊ฐ๋จํ๊ฒ ๊ตฌํํ ์ ์์ง๋ง,
์์ JavaScript์ ๋ฐ๋๋ผ JavaScript๋ฅผ ํ์ฉํ๋ฉด ์ข ๋ ์ธ๋ฐํ ์กฐ์์ด ๊ฐ๋ฅํฉ๋๋ค.
ํ๋ก์ ํธ์ ์๊ตฌ ์ฌํญ์ ๋ง์ถฐ ์ ์ ํ ๋ฐฉ๋ฒ์ ์ ํํ์ธ์!