How to make modal close on click outside

后端 未结 7 712
误落风尘
误落风尘 2020-12-19 02:05

I have used this JavaScript below:

7条回答
  •  佛祖请我去吃肉
    2020-12-19 02:47

    This is working for me: I have an image inside of the modal window, so if someone click outside the image (centered):

    html code:

    
    

    ccs code:

    #modal_div {
            display: none;
              position: fixed; 
            z-index: 1; 
            left: 0;
            top: 0;
            width: 100%; 
            height: 100%; 
            overflow: auto; 
              background-color: transparent;
    }
    
    #image_in_modal_div {
           left: 50%;
           top: 30%;
           position: absolute;
    }
    

    mixed jquery and javascript code:

    $( document ).ready(function() {
    
        $("#element_that_opens_modal").click(function(){
            $("#modal_div").show();
        });
    
        window.onclick = function(event) {
           if (event.target.id != "image_in_modal_div") {
              $("#modal_div").hide();
           }
        }
    
    });
    

提交回复
热议问题