模态关闭内容外点击

时间:2018-11-22 08:23:50

标签: javascript css

有人可以帮我吗?当我在内容之外单击时,我需要能够关闭此模式;这是我的代码:

$(document).ready(function(){
    $(".add-roles-btn").click(function(){
       $("#modal1").addClass("show-modal");
    });
  
});
.overlay {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgba(50, 65, 97, 0.5);
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  transition: all .3s; }
  .overlay .cancel {
    position: absolute;
    width: 100%;
    height: 100%;
    cursor: default; }
  .overlay__content {
    position: absolute;
    top: 44%;
    left: 55.5%;
    padding: 4.8rem 6.4rem;
    width: 540px;
    background-color: #ffffff;
    box-shadow: 0 2rem 4rem rgba(81, 136, 255, 0.2);
    border-radius: 3px;
    display: table;
    overflow: hidden;
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.25);
    transition: all .4s .2s; }
    @media only screen and (max-width: 61.875em) {
      .overlay__content {
        top: 50%;
        left: 50%; } }
    @media only screen and (max-width: 47em) {
      .overlay__content {
        padding: 4rem 6.5rem;
        width: 500px; } }
    @media only screen and (max-width: 37.5em) {
      .overlay__content {
        padding: 4rem 6.5rem;
        width: 100%; } }

 .show-modal {
  opacity: 1;
  visibility: visible; }

 .show-modal .overlay__content {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1); }
 <button type="button" class="add-roles-btn"><i class="material-icons icon--middle icon--pr-8">add</i>Add roles</button>

<!-- Pop up modal -->
                <div class="overlay" id="modal1">
               
                  <div class="overlay__content">
                    <h3 class="heading-primary">Add role</h3>

                    <form action="#">
                      <div class="form__group">
                        <input type="text" class="form__input" id="role_title">
                        <label for="role_title" class="form__label">Role Title</label>
                      </div>

                     <div class="form__group">
                        <textarea name="" class="form__input resize-none u-margin-bottom-0" id="role_description" rows="5"></textarea>
                        <label for="role_description" class="form__label">Role Description</label>
                      </div>

                      <div class="align-right">
                            <button class="btn btn--primary capitalize add-role-btn">Add Role <i class="material-icons icon--sub add-modal-role">add</i></button>
                      </div>
                    </form>

                  </div>
                </div>
                <!-- Pop up modal -->
如您所见,单击它具有JS代码,它显示了模态,但是当您单击外部以关闭时,我无法更改它;如何才能做到这一点?我是从头开始创建的。我不想使用某些库或其他东西;你能帮助我吗?我是JavaScript的新手,还是一般的编码。我真的很感激;谢谢。

2 个答案:

答案 0 :(得分:0)

您说过您不想使用任何库,所以这是一个普通的JS解决方案。

为此,我还将您的jQuery代码“翻译”为香草JS:

document.getElementsByClassName('add-roles-btn')[0].onclick = function() {
    document.getElementById('modal1').classList.add('show-modal');
}

// This block hides the modal when the user clicks outside of it
window.onclick = function(event) {
    if (event.target == document.getElementById('modal1')) {
    document.getElementById('modal1').style.display = 'none';
  }
}

Here's a working example with the rest of your code.


如果您想重新打开它,应该可以使用:

var modal = document.getElementById('modal1');

document.getElementsByClassName('add-roles-btn')[0].onclick = function() {
    modal.classList.add('show-modal');
}

window.onclick = function(event) {
    if (event.target == modal) {
    modal.classList.remove('show-modal');
  }
}

Here's the updated code.

答案 1 :(得分:0)

这是更新后的代码,基本上,该概念是侦听body上的单击事件,并且如果元素上的DOM父链没有到达模式modal1-单击不在它,因此可以关闭模​​式。

另一个重要说明是调用e.stopPropagation,因为body事件将在button事件之后被调用,从而导致窗口关闭。调用e.stopPropgation意味着单击按钮时,“较大”的主体事件将不会触发。

$(document).ready(function(){
    $(".add-roles-btn").click(function(e){
       $("#modal1").addClass("show-modal");
       // This is required so that when clicking the button the click event wont propogate to the body event
       e.stopPropagation()
    });
    // This function listens to all clicks on the document and gets the event data e
    $('body').click(function(e) {
       target = e.target;
       // If the clicked target isnt under modal1 - that means it won't be found in its parents
       if (($(target)).parents('#modal1').length == 0) {
          $("#modal1").removeClass("show-modal");
       }
    })
});

$(document).ready(function(){
    $(".add-roles-btn").click(function(e){
       $("#modal1").addClass("show-modal");
       // This is required so that when clicking the button the click event wont propogate to the body event
       e.stopPropagation()
    });
    // This function listens to all clicks on the document and gets the event data e
    $('body').click(function(e) {
       target = e.target;
       // If the clicked target isnt under modal1 - that means it won't be found in its parents
       if (($(target)).parents('#modal1').length == 0) {
          $("#modal1").removeClass("show-modal");
       }
    })
});
.overlay {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgba(50, 65, 97, 0.5);
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  transition: all .3s; }
  .overlay .cancel {
    position: absolute;
    width: 100%;
    height: 100%;
    cursor: default; }
  .overlay__content {
    position: absolute;
    top: 44%;
    left: 55.5%;
    padding: 4.8rem 6.4rem;
    width: 540px;
    background-color: #ffffff;
    box-shadow: 0 2rem 4rem rgba(81, 136, 255, 0.2);
    border-radius: 3px;
    display: table;
    overflow: hidden;
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.25);
    transition: all .4s .2s; }
    @media only screen and (max-width: 61.875em) {
      .overlay__content {
        top: 50%;
        left: 50%; } }
    @media only screen and (max-width: 47em) {
      .overlay__content {
        padding: 4rem 6.5rem;
        width: 500px; } }
    @media only screen and (max-width: 37.5em) {
      .overlay__content {
        padding: 4rem 6.5rem;
        width: 100%; } }

 .show-modal {
  opacity: 1;
  visibility: visible; }

 .show-modal .overlay__content {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="add-roles-btn"><i class="material-icons icon--middle icon--pr-8">add</i>Add roles</button>

<!-- Pop up modal -->
                <div class="overlay" id="modal1">
               
                  <div class="overlay__content">
                    <h3 class="heading-primary">Add role</h3>

                    <form action="#">
                      <div class="form__group">
                        <input type="text" class="form__input" id="role_title">
                        <label for="role_title" class="form__label">Role Title</label>
                      </div>

                     <div class="form__group">
                        <textarea name="" class="form__input resize-none u-margin-bottom-0" id="role_description" rows="5"></textarea>
                        <label for="role_description" class="form__label">Role Description</label>
                      </div>

                      <div class="align-right">
                            <button class="btn btn--primary capitalize add-role-btn">Add Role <i class="material-icons icon--sub add-modal-role">add</i></button>
                      </div>
                    </form>

                  </div>
                </div>
                <!-- Pop up modal -->

相关问题