$(document).ready(function () { const COOKIE_NAME = "exit_intent_seen"; const COOKIE_DAYS = 7; const DELAY_MS = 10000; const DESKTOP_MIN = 992; if (getCookie(COOKIE_NAME)) return; let exitShown = false; let delayPassed = false; let lastScroll = window.scrollY; const isDesktop = $(window).width() >= DESKTOP_MIN; const $popup = $(".exit-intent-popup-section"); const $popupContent = $(".exit-intent-popup-wrapper"); /* ========================= INITIAL STATE ========================= */ gsap.set($popup, { autoAlpha: 0, display: "flex" }); gsap.set($popupContent, { scale: 0.95 }); /* ========================= DELAY GATE ========================= */ setTimeout(() => { delayPassed = true; }, DELAY_MS); /* ========================= DESKTOP EXIT INTENT ========================= */ if (isDesktop) { $(document).on("mouseout", function (e) { if (e.clientY <= 0 && delayPassed && !exitShown) { exitShown = true; showPopup(); } }); } /* ========================= MOBILE EXIT INTENT ========================= */ if (!isDesktop) { /* --- Back Button --- */ history.pushState({ exitIntent: 1 }, "", ""); window.addEventListener("popstate", function () { if (delayPassed && !exitShown) { exitShown = true; showPopup(); history.pushState({ exitIntent: 1 }, "", ""); } }); /* --- Scroll Up Near Top --- */ window.addEventListener("scroll", function () { let current = window.scrollY; if ( delayPassed && !exitShown && current < lastScroll && // scrolling up current < 120 // near top ) { exitShown = true; showPopup(); } lastScroll = current; }); } /* ========================= CLOSE POPUP ========================= */ $(".close-popup").on("click", function () { hidePopup(); setCookie(COOKIE_NAME, true, COOKIE_DAYS); }); /* ========================= ANIMATIONS ========================= */ function showPopup() { gsap.to($popup, { duration: 0.4, autoAlpha: 1, ease: "power1.out" }); gsap.to( $popupContent, { duration: 0.4, scale: 1, ease: "power1.out" }, "<" ); $("html").addClass("w-lightbox-noscroll"); } function hidePopup() { gsap.to($popup, { duration: 0.25, autoAlpha: 0, ease: "power2.in" }); gsap.to($popupContent, { scale: 0.95, ease: "power1.out" }, "<"); $("html").removeClass("w-lightbox-noscroll"); } /* ========================= COOKIE HELPERS ========================= */ function setCookie(name, value, days) { const expires = new Date(); expires.setDate(expires.getDate() + days); document.cookie = name + "=" + value + "; expires=" + expires.toUTCString() + "; path=/"; } function getCookie(name) { return document.cookie .split("; ") .find((row) => row.startsWith(name + "=")); } });