Hot Line:

+1 (917) 730-2010

AI-Powered Web Development, SEO, Shopify & WordPress — Get a Free Consultation Today. Get Started

Tutorials

How To Add Drop Down On Hover – Updated for 9.0.0 Themes

  • 13 Apr, 2023
  • 0 Comments
  • By WebSensePro
How To Add Drop Down On Hover – Updated for 9.0.0 Themes

This tutorial provides a step-by-step guide for adding hover functionality to drop-down menus in Shopify. The updated version of the tutorial ensures that the code works on the latest Shopify themes, specifically Dawn 9.0.0 Refresh 9.0.0 Sense 9.0.0, as well as any other themes that have been updated to version 9.0.0. The tutorial explains the process of adding the necessary code to a Shopify theme’s files and includes helpful screenshots and code snippets to guide users through the process. Whether you are a beginner or an experienced developer, this tutorial can help you create smoother and more user-friendly drop-down menus for your Shopify store.

Add Code in Header.liquid

Search for “Javascript” in header.liquid file and the following code above it

<script>
  let items = document.querySelector(".header__inline-menu").querySelectorAll("details");
let dropdownItems = document.querySelector(".header__submenu");

items.forEach(item => {
  item.addEventListener("mouseenter", () => {
    item.setAttribute("open", true);
    let ulElement = item.querySelector("ul");
    if (ulElement !== null) {
      ulElement.addEventListener("mouseleave", () => {
        item.removeAttribute("open");
      });
    }
  });

  item.addEventListener("focus", () => {
    item.setAttribute("open", true);
  });

  item.addEventListener("blur", () => {
    item.removeAttribute("open");
  });
});

if (dropdownItems !== null) {
  dropdownItems.addEventListener("mouseleave", () => {
    items.forEach(item => {
      item.removeAttribute("open");
    });
  });
}
</script>
4.7/5 · 18 votes
Tags:

    Comments

    Background Pattern

    Want to work with us?

    Let’s build your website, grow your traffic, and automate your business with AI.

    Background Pattern