Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

Close Modal by the Escape key #60

Open
Tetrikus opened this issue Jan 2, 2018 · 8 comments
Open

Close Modal by the Escape key #60

Tetrikus opened this issue Jan 2, 2018 · 8 comments

Comments

@Tetrikus
Copy link

Tetrikus commented Jan 2, 2018

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
Copy link

nickfmc commented Mar 16, 2018

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
Copy link

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

@nickfmc
Copy link

nickfmc commented Aug 8, 2018

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();
    }
  });

mgzrobles pushed a commit to mgzrobles/animatedModal.js that referenced this issue Sep 6, 2018
@rezaaria
Copy link

rezaaria commented Feb 13, 2019

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
Copy link

nickfmc commented Feb 13, 2019

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
Copy link

rezaaria commented Feb 14, 2019

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
Copy link

Anyone have solution to fix close by esc ?

@bjornweb
Copy link

bjornweb commented Jul 21, 2019

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');
   }
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants