Skip to content

Instantly share code, notes, and snippets.

@Sathyaish
Forked from Vardner/dropdown
Created November 4, 2019 09:45
Show Gist options
  • Save Sathyaish/caab661e912b51788851fe95c9c390d9 to your computer and use it in GitHub Desktop.
Save Sathyaish/caab661e912b51788851fe95c9c390d9 to your computer and use it in GitHub Desktop.
Simple dropdown menu
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