How to close a modal by clicking outside the modal window?

前端 未结 4 803
既然无缘
既然无缘 2021-01-11 21:16

In a very simple jQuery modal, I close the modal by clicking on CLOSE as

$(\'#close\').click(function(e) {
  e.preventDefault();
  $(\'#overlay, #alertModalO         


        
相关标签:
4条回答
  • 2021-01-11 21:29

    I found it helpful to include:

    $('.item-modal').click(function(e) {
      e.stopPropagation();
    });
    
    0 讨论(0)
  • 2021-01-11 21:39

    I know this question pertains to jQuery, but here's how I did this in Vue on my modal component in case anyone finds it helpful. My modal HTML is basically lifted directly from this: https://vuejs.org/v2/examples/modal.html

    I set two @click attributes, one on the outermost modal element (modal-mask) that calls my close() method (which emits the close event to the parent component) and one on the actual modal window element (modal-container) that with the .stop event modifier (@click.stop) to stop the click from bubbling up to the parent element (modal-mask). Works like a charm.

    0 讨论(0)
  • 2021-01-11 21:44

    Changing your function like so should work:

        $('#close, #overlay').click(function(e) {
          e.preventDefault();
          $('#overlay, #alertModalOuter').fadeOut(400, function() {
          $('#close').remove();
        });
    });
    
    0 讨论(0)
  • 2021-01-11 21:51

    Add the same click listener to your overlay.

    0 讨论(0)
提交回复
热议问题