$(document).ready(function () { let lastPlayedVideo = null; function safePlayVideo(video) { if (!video || !video.paused || video.readyState < 2) return; video.play().then(() => { lastPlayedVideo = video; }).catch(error => { console.warn("Safe play interrupted:", error); }); } // ============================== // 1. Header GNB 모션 // ============================== $(".h_gnb li a, .siteMapItem").hover( function () { $(this).find(".basic").css("transform", "translateY(-100%)"); $(this).find(".rolling").css("transform", "translateY(0)"); }, function () { $(this).find(".basic").css("transform", "translateY(0)"); $(this).find(".rolling").css("transform", "translateY(100%)"); } ); // ============================== // 2. PC에서 첫 번째 비디오만 로드 // ============================== function loadFirstPCVideo() { if (!isMobile()) { var firstVideo = $(".com_slide_videon_box .scroll_videon_montion_box:first-child video.lazy-video")[0]; if (firstVideo) { loadVideoSrc(firstVideo); } } } // ============================== // 3. Lazy Load // ============================== function loadVideoSrc(video) { var source = video.querySelector("source"); if (source && source.dataset.src && !video.getAttribute("data-loaded")) { source.src = source.dataset.src; video.load(); video.setAttribute("data-loaded", "true"); } } function lazyLoadVideos() { const videos = document.querySelectorAll(".scroll_videon_montion_box video.lazy-video"); videos.forEach(video => loadVideoSrc(video)); } // ============================== // 4. 모바일에서 자동 재생 // ============================== function isMobile() { return /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent); } function checkMobileVideoVisibility() { if (!isMobile()) return; let centerBox = null; let centerDistance = Infinity; const viewportCenter = window.innerHeight / 2; $(".scroll_videon_montion_box").each(function () { var videoBox = $(this); var videoElement = videoBox.find("video.lazy-video")[0]; if (!videoElement) return; var rect = videoBox[0].getBoundingClientRect(); var videoCenter = rect.top + rect.height / 2; var distance = Math.abs(viewportCenter - videoCenter); if (distance < centerDistance) { centerDistance = distance; centerBox = videoBox; } }); if (centerBox) { $(".scroll_videon_montion_box").removeClass("active"); centerBox.addClass("active"); var centerVideo = centerBox.find("video.lazy-video")[0]; if (centerVideo) { if (lastPlayedVideo && lastPlayedVideo !== centerVideo) { lastPlayedVideo.pause(); } loadVideoSrc(centerVideo); safePlayVideo(centerVideo); } } } // ============================== // 5. PC Hover 재생 // ============================== $(".scroll_videon_montion_box").on("mouseenter", function () { if (!isMobile()) { $(".scroll_videon_montion_box video.lazy-video").each(function () { this.pause(); $(this).siblings(".video-thumbnail").show(); }); $(".scroll_videon_montion_box").removeClass("active"); $(this).addClass("active"); var videoElement = $(this).find("video.lazy-video")[0]; if (videoElement) { loadVideoSrc(videoElement); safePlayVideo(videoElement); } } }); $(".scroll_videon_montion_box").on("mouseleave", function () { if (!isMobile()) { setTimeout(() => { $(".scroll_videon_montion_box.active video.lazy-video").each(function () { safePlayVideo(this); }); }, 100); } }); // ============================== // 6. PC 자동 재생 Intersection Observer // ============================== function setupVideoObservers() { const videos = document.querySelectorAll(".scroll_videon_montion_box video.lazy-video"); const observer = new IntersectionObserver(entries => { entries.forEach(entry => { const video = entry.target; if (entry.isIntersecting) { video.muted = true; loadVideoSrc(video); safePlayVideo(video); } else { video.pause(); } }); }, { threshold: 0.5 }); videos.forEach(video => observer.observe(video)); } // ============================== // 7. 타이틀 인터랙션 // ============================== function checkTitleVisibility() { $(".com_tit01").each(function () { var title = $(this); var isMvTitle = title.closest(".mv").length > 0; var boundingBox = title[0].getBoundingClientRect(); var viewportHeight = window.innerHeight; var titleMidPoint = boundingBox.top + (boundingBox.height / 2); var viewportThreshold = viewportHeight * 0.45; if (titleMidPoint <= viewportThreshold && boundingBox.bottom >= 0) { if (!title.hasClass("active")) { if (isMvTitle && !title.data("delayed")) return; title.addClass("active"); } } var removalThreshold = viewportHeight * 0.2; if (boundingBox.bottom < removalThreshold || boundingBox.top > viewportHeight) { title.removeClass("active"); } }); } setTimeout(() => { $(".mv .com_tit01").data("delayed", true); checkTitleVisibility(); }, 2000); $(window).on("scroll", function () { requestAnimationFrame(checkTitleVisibility); }); checkTitleVisibility(); // ============================== // 8. 사이트맵 기능 // ============================== $(".headerMenuBtn").click(function (e) { e.stopPropagation(); $("#header").toggleClass("active"); $("#siteMap").toggleClass("active"); $("html, body").toggleClass("active"); }); $(document).click(function (e) { if (!$(e.target).closest("#header, #siteMap").length) { $("#header").removeClass("active"); $("#siteMap").removeClass("active"); $("html, body").removeClass("active"); } }); // ============================== // 9. 모바일 스크롤 재생 // ============================== $(window).on("scroll touchmove", function () { requestAnimationFrame(checkMobileVideoVisibility); }); setTimeout(checkMobileVideoVisibility, 300); // ============================== // 10. 초기 실행 // ============================== loadFirstPCVideo(); setupVideoObservers(); // ============================== // software_con01 영역 전용 // ============================== function checkSoftwareConVideoScroll() { let centerBox = null; let centerDistance = Infinity; const viewportCenter = window.innerHeight / 2; $(".software_con01 .scroll_videon_montion_box").each(function () { var videoBox = $(this); var videoElement = videoBox.find("video.lazy-video")[0]; if (!videoElement) return; var rect = videoBox[0].getBoundingClientRect(); var videoCenter = rect.top + rect.height / 2; var distance = Math.abs(viewportCenter - videoCenter); if (distance < centerDistance) { centerDistance = distance; centerBox = videoBox; } }); if (centerBox) { $(".software_con01 .scroll_videon_montion_box").removeClass("active"); centerBox.addClass("active"); var centerVideo = centerBox.find("video.lazy-video")[0]; if (centerVideo) { if (lastPlayedVideo && lastPlayedVideo !== centerVideo) { lastPlayedVideo.pause(); } loadVideoSrc(centerVideo); safePlayVideo(centerVideo); } } } // software_con01 영역만 이벤트 등록 if ($(".software_con01").length > 0) { $(window).on("scroll touchmove", function () { requestAnimationFrame(checkSoftwareConVideoScroll); }); setTimeout(checkSoftwareConVideoScroll, 300); } });