After creating a custom menu on Wordpress using jQuery slideToggle to toggle dropdown on hover, everything seemed to be working perfectly. However, I noticed that when I refreshed the page while moving my cursor between two menu items with dropdown menus, the slideToggle effect would reverse on one of them.
jQuery(document).ready(function() {
jQuery(".has-mega-menu").has('.sub-menu').children('.sub-menu').hide();
jQuery('.has-mega-menu').hover(function() {
jQuery(this).has('.sub-menu').children('.sub-menu').stop().slideToggle();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li id="menu-item-28" class="has-mega-menu menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-28">
<a href="shop-link">Shop</a>
<ul class="sub-menu" style="height: 222px; padding-top: 0px; margin-top: 0px; padding-bottom: 0px; margin-bottom: 0px; display: none;">
<li id="menu-item-42" class="mega-menu-column menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-42"><a href="#">TEST</a>
<ul class="sub-menu">
<li id="menu-item-41" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-41">
<a href="#">Test Item 1</a>
</li>
<li id="menu-item-36" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-36">
<a href="Shop-link">Test Item 2</a>
</li>
</ul>
</li>
<li id="menu-item-43" class="mega-menu-column menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-43">
<a href="#">TEST 2</a>
<ul class="sub-menu">
<li id="menu-item-44" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-44">
<a href="#">Test Item 1</a>
</li>
<li id="menu-item-45" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-45">
<a href="#">Test Item 2</a>
</li>
</ul>
</li>
<li id="menu-item-46" class="mega-menu-column menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-46">
<a href="#">TEST 3</a>
<ul class="sub-menu">
<li id="menu-item-47" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-47">
<a href="#">Test Item 1</a>
</li>
</ul>
</li>
</ul>
</li>