-
-
Save Sathyaish/caab661e912b51788851fe95c9c390d9 to your computer and use it in GitHub Desktop.
Simple dropdown menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTML: | |
<div data-dropdown="toggle"> | |
<div>Block</div> | |
<ul data-dropdown="target"> | |
<li> | |
<a f href="javascript: void(0)" class="category-menu__item-link"> | |
<span> | |
TEXT | |
</span> | |
</a> | |
</li> | |
</ul> | |
</div> | |
<div id="shadow></div> | |
JS: | |
'use strict'; | |
function enableDropdowns () { | |
const dropdownElements = $('[data-dropdown=toggle]'); | |
if (dropdownElements.length) { | |
dropdownElements.click(showDropdown) | |
} | |
function showDropdown (e) { | |
const shadow = $('#shadow'); | |
const dropdown = $(this); | |
const dropdownContent = dropdown.find('[data-dropdown=target]'); | |
if (e.target.closest('[data-dropdown=target]')) { | |
return; | |
} | |
shadow.toggleClass('active'); | |
dropdownContent.slideToggle(500); | |
shadow.click( | |
() => { | |
shadow.removeClass('active'); | |
dropdownContent.removeClass('active'); | |
dropdownContent.slideUp(500); | |
} | |
) | |
} | |
} | |
enableDropdowns(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment