How To Add Custom Scroll Up Or Down Button To A Specific Section In Shopify

How To Add Custom Scroll Up Or Down Button To A Specific Section In Shopify

Learn how to enhance user experience on your Shopify store by adding custom scroll up or down buttons to a specific section. This tutorial will guide you through the process of creating and implementing these buttons using liquid code and CSS. No coding experience required! Discover how to improve navigation, increase engagement, and boost conversions with this simple yet effective customization.

Benefits of Adding a Custom Scroll Up/Down Button:

  • Improved User Experience: Provides users with an easy way to navigate through long sections, reducing scrolling fatigue.
  • Enhanced Navigation: Helps users quickly move between different parts of a page, improving overall site usability.
  • Increased Engagement: Encourages users to explore more of your content by making it easier to navigate.
  • Boosted Conversions: A smoother user experience can lead to increased conversions and sales.
  • Professional Look: Adds a touch of customization to your Shopify store, giving it a more polished appearance.
  • Mobile-Friendly: Can be designed to be responsive and work well on different screen sizes.

By adding custom scroll up/down buttons to specific sections of your Shopify store, you can significantly enhance the overall user experience and potentially drive more sales.

<a class="button btn scroll-button" href="#shopify-section-template--22945066287408__114fc349-9832-4eb7-9035-f91731bb9499" style="
    position: fixed;
    background-color: #000000;
    bottom: 40px;
    z-index: 3;
    right: 50%;
    transform: translateX(50%);
">Scroll To Products</a>
<script>
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
  anchor.addEventListener('click', function (e) {
    e.preventDefault();

    const targetId = this.getAttribute('href').substring(1);   

    const targetElement = document.getElementById(targetId);

    if (targetElement)   
 {
      try {
        targetElement.scrollIntoView({ behavior: 'smooth' });
      } catch (error) {
        console.error('Smooth scroll error:', error);
        // Handle the error, e.g., fallback to instant scroll:
        window.scrollTo({ top: targetElement.offsetTop, behavior: 'instant' });
      }
    } else {
      console.error('Target element not found:', targetId);
    }
  });
});




const button = document.querySelector('.scroll-button');
let lastScrollTop = 0;
let delta = 5; // Adjust this value to control sensitivity

window.addEventListener('scroll', () => {
  const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  const direction = scrollTop > lastScrollTop ? 'down' : 'up';
  lastScrollTop = scrollTop;   


  if (direction === 'down') {
    if (scrollTop > delta) {
      button.classList.add('show');
    } else {
      button.classList.remove('show');
    }
  } else {
    if (scrollTop + window.innerHeight < document.body.scrollHeight - delta) {
      button.classList.add('show');
    } else {
      button.classList.remove('show');
    }
  }
});



  
</script>