How to make modal close on click outside

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

I have used this JavaScript below:

7条回答
  •  一生所求
    2020-12-19 02:55

    $('body').on('click', '.modal-open', function(e) {
        
        $('.modal-background, .modal').show();
        e.preventDefault();
    })
    .on('click', '.modal-close', function(e) {
        
        $('.modal-background, .modal').hide();
        e.preventDefault();
    });
    
    if ( !$('.modal-background.modal-close').length ) {
        $('
    ').addClass('modal-background modal-close').appendTo('body'); }
    body {
        background: #ccc;
        overflow-y: scroll;
        padding: 15px;
    }
    
    button {
        cursor: pointer;
    }
    
    .modal {
        width: 400px;
        margin: 5% auto 0 -200px;
        padding: 10px;
        background: #eee;
        display: none;
        position: absolute;
        left: 50%;
        top: 0;
        z-index: 10;
    }
    
    .modal-background {
        background: rgba(0, 0, 0, 0.5);
        /* background: transparent; */
        /* position: absolute; */
        position: fixed;
        z-index: 9; /* .modal[zIndex] - 1 */
        bottom: 0;
        right: 0;
        left: 0;
        top: 0;
        display: none;
    }

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

提交回复
热议问题