/* * 파일명: main.js * 위치: /assets/js/main.js * 기능: 메인 스크립트 * 작성일: 2025-05-08 */ /** * DOM이 로드된 후 실행되는 함수 */ document.addEventListener('DOMContentLoaded', function() { // 이미지 지연 로딩 초기화 initLazyLoading(); // 부드러운 스크롤 초기화 initSmoothScroll(); // 모바일 메뉴 토글 초기화 initMobileMenuToggle(); // 상품 이미지 확대 효과 초기화 initProductImageZoom(); }); /** * 이미지 지연 로딩 초기화 함수 */ function initLazyLoading() { // data-src 속성을 가진 이미지 요소들 선택 const lazyImages = document.querySelectorAll('img[data-src]'); // Intersection Observer 설정 const imageObserver = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { // 이미지가 화면에 들어오면 로딩 if (entry.isIntersecting) { const img = entry.target; img.src = img.dataset.src; img.classList.add('fade-in'); observer.unobserve(img); } }); }, { rootMargin: '50px 0px', threshold: 0.01 }); // 각 이미지에 Observer 적용 lazyImages.forEach(img => { imageObserver.observe(img); }); } /** * 부드러운 스크롤 초기화 함수 */ function initSmoothScroll() { // 내부 링크(#으로 시작하는)에 부드러운 스크롤 적용 document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function(e) { e.preventDefault(); const targetId = this.getAttribute('href'); if (targetId === '#') return; const targetElement = document.querySelector(targetId); if (targetElement) { window.scrollTo({ top: targetElement.offsetTop - 70, // 헤더 높이 고려 behavior: 'smooth' }); } }); }); } /** * 모바일 메뉴 토글 초기화 함수 */ function initMobileMenuToggle() { // 모바일 메뉴 토글 버튼이 있는 경우 const menuToggle = document.querySelector('.menu-toggle'); const navMenu = document.querySelector('.nav-menu'); if (menuToggle && navMenu) { menuToggle.addEventListener('click', function() { navMenu.classList.toggle('active'); this.classList.toggle('active'); }); } } /** * 상품 이미지 확대 효과 초기화 함수 */ function initProductImageZoom() { // 상품 상세 페이지의 이미지에 확대 효과 적용 const productImage = document.querySelector('.product-image'); if (productImage) { productImage.addEventListener('mouseenter', function() { this.style.transform = 'scale(1.05)'; this.style.transition = 'transform 0.3s ease'; }); productImage.addEventListener('mouseleave', function() { this.style.transform = 'scale(1)'; }); } } /** * 쿠키 설정 함수 * @param {string} name 쿠키 이름 * @param {string} value 쿠키 값 * @param {number} days 유효 기간(일) */ function setCookie(name, value, days) { let expires = ''; if (days) { const date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = '; expires=' + date.toUTCString(); } document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=/'; } /** * 쿠키 가져오기 함수 * @param {string} name 쿠키 이름 * @return {string|null} 쿠키 값 또는 null */ function getCookie(name) { const nameEQ = name + '='; const ca = document.cookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) === ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) === 0) { return decodeURIComponent(c.substring(nameEQ.length, c.length)); } } return null; } /** * 쿠키 삭제 함수 * @param {string} name 쿠키 이름 */ function eraseCookie(name) { setCookie(name, '', -1); } /** * 페이지 새로고침 함수 */ function refreshPage() { window.location.reload(); } /** * URL 파라미터 가져오기 함수 * @param {string} name 파라미터 이름 * @return {string|null} 파라미터 값 또는 null */ function getUrlParameter(name) { name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); const regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); const results = regex.exec(location.search); return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); }