Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
2.0 kB
3
Indexable
Never
(function () {
  try {
    /* main variables */
    var debug = 0;
    var variation_name = "";
    var $;

    /* Function to wait for an element to appear */
    function waitForElement(selector, trigger, delayInterval, delayTimeout) {
      var interval = setInterval(function () {
        if (
          document &&
          document.querySelector(selector) &&
          document.querySelectorAll(selector).length > 0
        ) {
          clearInterval(interval);
          trigger();
        }
      }, delayInterval);
      setTimeout(function () {
        clearInterval(interval);
      }, delayTimeout);
    }

    /* Function to hide the header on scroll */
    function hideHeaderOnScroll() {
      const header = document.querySelector('#primary_header');
      let lastScrollTop = 0;
      
      window.addEventListener('scroll', function () {
        const currentScrollTop = window.scrollY || document.documentElement.scrollTop;

        if (currentScrollTop > lastScrollTop) {
          // Scroll down
          header.style.transform = 'translateY(-100%)'; // Hide the header
        } else {
          // Scroll up
          header.style.transform = 'translateY(0)'; // Show the header
        }

        lastScrollTop = currentScrollTop <= 0 ? 0 : currentScrollTop; // For Mobile or negative scrolling
      });
    }

    /* Function to initialize */
    function init() {
      /* Find the target element */
      let selector = document.querySelector('#shopify-section-header #NavDrawer .drawer__scrollable');

      /* Ensure the element exists before inserting */
      if (selector) {
        selector.insertAdjacentHTML('beforebegin', eg_header);
      }

      // Call the hide header function
      hideHeaderOnScroll();
    }

    /* Initialize variation */
    waitForElement('#primary_header', init, 50, 15000);
  } catch (error) {
    console.error("Error:", error);
  }
})();
Leave a Comment