Advertisement
Guest User

Code sticky

a guest
Sep 28th, 2021
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {% javascript %}
  2.   class StickyHeader extends HTMLElement {
  3.     constructor() {
  4.       super();
  5.     }
  6.  
  7.     connectedCallback() {
  8.       this.header = document.getElementById('shopify-section-header');
  9.       this.headerBounds = {};
  10.       this.currentScrollTop = 0;
  11.       this.preventReveal = false;
  12.  
  13.       this.onScrollHandler = this.onScroll.bind(this);
  14.       this.hideHeaderOnScrollUp = () => this.preventReveal = true;
  15.  
  16.       this.addEventListener('preventHeaderReveal', this.hideHeaderOnScrollUp);
  17.       window.addEventListener('scroll', this.onScrollHandler, false);
  18.  
  19.       this.createObserver();
  20.     }
  21.  
  22.     disconnectedCallback() {
  23.       this.removeEventListener('preventHeaderReveal', this.hideHeaderOnScrollUp);
  24.       window.removeEventListener('scroll', this.onScrollHandler);
  25.     }
  26.  
  27.     createObserver() {
  28.       let observer = new IntersectionObserver((entries, observer) => {
  29.         this.headerBounds = entries[0].intersectionRect;
  30.         observer.disconnect();
  31.       });
  32.  
  33.       observer.observe(this.header);
  34.     }
  35.  
  36.     onScroll() {
  37.       const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  38.  
  39.       if (scrollTop > this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
  40.         requestAnimationFrame(this.hide.bind(this));
  41.       } else if (scrollTop < this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
  42.         if (!this.preventReveal) {
  43.           requestAnimationFrame(this.reveal.bind(this));
  44.         } else {
  45.           window.clearTimeout(this.isScrolling);
  46.  
  47.           this.isScrolling = setTimeout(() => {
  48.             this.preventReveal = false;
  49.           }, 66);
  50.  
  51.           requestAnimationFrame(this.hide.bind(this));
  52.         }
  53.       } else if (scrollTop <= this.headerBounds.top) {
  54.         requestAnimationFrame(this.reset.bind(this));
  55.       }
  56.  
  57.  
  58.       this.currentScrollTop = scrollTop;
  59.     }
  60.  
  61.     hide() {
  62.       this.header.classList.add('shopify-section-header-hidden', 'shopify-section-header-sticky');
  63.     }
  64.  
  65.     reveal() {
  66.       this.header.classList.add('shopify-section-header-sticky', 'animate');
  67.       this.header.classList.remove('shopify-section-header-hidden');
  68.     }
  69.  
  70.     reset() {
  71.       this.header.classList.remove('shopify-section-header-hidden', 'shopify-section-header-sticky', 'animate');
  72.     }
  73.   }
  74.  
  75.   customElements.define('sticky-header', StickyHeader);
  76. {% endjavascript %}
  77. <style>
  78. #shopify-section-header {
  79.   z-index: 3;
  80. }
  81.  
  82. .shopify-section-header-sticky {
  83.   position: sticky;
  84.   top: 0;
  85.   background: #fff;
  86. }
  87.  
  88. .shopify-section-header-hidden {
  89.   transform: translateY(-100%);
  90. }
  91.  
  92. #shopify-section-header.animate {
  93.   transition: transform 0.15s ease-out;
  94. }
  95. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement