Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. $(document).on("click",".js_popup-trigger", function(e){
  2. e.preventDefault();
  3.  
  4. //Set variables
  5. var element = $(e.currentTarget).closest('.js_popup-trigger'),
  6. container = element.parent('.js_popup-container'),
  7. menu = element.next('.js_popup-menu');
  8.  
  9. //Toggle the menu
  10. menu.fadeToggle(100);
  11.  
  12. //Add the open classes to the container and trigger
  13. container.toggleClass('js_popup-container--open');
  14. element.toggleClass('js_popup-trigger--open');
  15.  
  16. /* Create a click listener for the document
  17. * This is to close the menu if clicked outside of it */
  18. $(document).click(function closePopup(e){
  19.  
  20. //If the clicked item is not in the popup, close it
  21. if(!$(e.target).closest(container[0]).length && element.is(":visible")){
  22.  
  23. //Close the popup
  24. menu.fadeOut(100);
  25.  
  26. //Remove the added classes
  27. container.removeClass('js_popup-container--open');
  28. element.removeClass('js_popup-trigger--open');
  29.  
  30. //Unbind the closePopup function from the document
  31. $(document).unbind('click',closePopup);
  32. }
  33. });
  34. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement