簡體   English   中英

模態關閉內容外點擊

[英]Modal close clicking outside the content

有人可以幫幫我嗎? 當我在內容之外單擊時,我需要能夠關閉此模式; 這是我的代碼:

 $(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的新手,還是一般的編碼。 我真的很感激; 謝謝。

您說過您不想使用任何庫,所以這是一個普通的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';
  }
}

這是其余代碼的有效示例。


如果您想重新打開它,應該可以使用:

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

這是更新的代碼。

這是更新后的代碼,基本上,該概念是偵聽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 --> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM