joaopereirawd / animatedModal.js

animatedModal.js is a jQuery plugin to create a fullscreen modal with CSS3 transitions. You can use the transitions from animate.css or create your own transitions.
MIT License
962 stars 207 forks source link

Close Modal by the Escape key #60

Open Tetrikus opened 6 years ago

Tetrikus commented 6 years ago

I'd love to be able to close the modals with the ESC key. I believe, this would be an intuitive behaviour for the user.

nickfmc commented 6 years ago

Props to @onetrev for this code

// close modal menu if esc key is used

  jQuery(document).keyup(function(ev){
    if(ev.keyCode == 27) {
      modal_menu.close();
    }
  });
richardwiggins commented 5 years ago

This doesn't work for me. Should I need to be adding anything else?

nickfmc commented 5 years ago

you need to make sure your var matches. here this should help.

 // modal menu init
  var modal_menu = jQuery("#ModalMenuButton").animatedModal({
    modalTarget: 'ModalNav',
    animatedIn: 'slideInDown',
    animatedOut: 'slideOutUp',
    animationDuration: '0.50s',
    color: '#008f8f'
  });
  // close modal menu if esc key is used
  jQuery(document).keyup(function(ev){
    if(ev.keyCode == 27) {
      modal_menu.close();
    }
  });
rezaaria commented 5 years ago

you need to make sure your var matches. here this should help.

 // modal menu init
  var modal_menu = jQuery("#ModalMenuButton").animatedModal({
    modalTarget: 'ModalNav',
    animatedIn: 'slideInDown',
    animatedOut: 'slideOutUp',
    animationDuration: '0.50s',
    color: '#008f8f'
  });
  // close modal menu if esc key is used
  jQuery(document).keyup(function(ev){
    if(ev.keyCode == 27) {
      modal_menu.close();
    }
  });

I use this code but its not working ? any solution ?

var modal_menu = jQuery("#xang-modal-search").animatedModal({
    modalTarget: 'xang-modal-searchid',
    animatedIn: 'slideInDown',
    animatedOut: 'slideOutUp',
    animationDuration: '0.50s',
    color: '#008f8f'
});
jQuery(document).keyup(function(ev){
    if(ev.keyCode == 27) {
    modal_menu.close();
    }
});
nickfmc commented 5 years ago

if your modal is working other than the esc close? then check the key you are pressing is in fact registering as keycode 27 https://keycode.info/

rezaaria commented 5 years ago

Yes modal work well but close by esc doesn't work.I checked keycode and its 27 but it doesn't work.

Is there any other solution ?

rezaaria commented 5 years ago

Anyone have solution to fix close by esc ?

bjornweb commented 4 years ago

animatedModal does not have a close function (for now ;-) ). But why not simply trigger a click on the modal close button when the escape key is pressed?

// Close (every) modal if Escape key is pressed.
jQuery(document).keyup(function(ev){ 
   if(ev.keyCode == 27) {
     jQuery('.modal-container [class^="close-fancyModal_"]').trigger('click');
   }
});