简体   繁体   English

如何也可以通过在主要内容区域之外单击来关闭此弹出窗口?

[英]How can I get this popup to close by clicking outside of the main content area as well?

I have reproduced some js (that I am using on my site) and put it in a codepen and everything works as expected but I would love for the popup to close not only when a user clicks on the 'X' (which it does now) but also clicks outside of the main content area (.lion .wrap). 我已经复制了一些js(我在我的网站上使用的js)并将其放在代码笔中,并且一切正常,但我希望弹出窗口不仅在用户单击“ X”时关闭,现在也要关闭),也可以在主要内容区域(.lion .wrap)之外单击。

Thank you so much in advance! 提前非常感谢您! Jorie 乔丽

I found this online : https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_modal_close - that functions exactly as I would like mine to however I have tried to incorporate that code into the js that I am using and simply do not have the skill set to get it to work. 我在网上找到了这个网址: https : //www.w3schools.com/w3css/tryit.asp?filename=tryw3css_modal_close-该功能与我想要的完全相同,但是我尝试将该代码合并到我正在使用的js中,根本不具备使它起作用的技能。 I have tried everything and thought that maybe someone here could help me out! 我已经尝试了一切,并认为这里也许有人可以帮助我!

https://codepen.io/rjhodges/pen/LopoJp https://codepen.io/rjhodges/pen/LopoJp

var body = document.body,
    lionoverlay = document.querySelector('.lion'),
    lionoverlayBtts = document.querySelectorAll('button[class$="lion"]');

[].forEach.call(lionoverlayBtts, function(btt) {

  btt.addEventListener('click', function() { 

     /* Detect the button class name */
     var lionoverlayOpen = this.className === 'open-lion';

     /* Toggle the aria-hidden state on the overlay and the 
        no-scroll class on the body */
     lionoverlay.setAttribute('aria-hidden', !lionoverlayOpen);
     body.classList.toggle('noscroll', lionoverlayOpen);

     /* On some mobile browser when the overlay was previously
        opened and scrolled, if you open it again it doesn't 
        reset its scrollTop property after the fadeout */
     setTimeout(function() {
         lionoverlay.scrollTop = 0;              }, 1000)

  }, false);

}); 

So as is demonstrated by the codepen, the box closes as expected when clicking on the 'X' but I would like it to also close when a user clicks outside of the main content area (.lion .wrap) 因此,如代码笔所显示的,单击“ X”时,该框将按预期关闭,但是我希望当用户单击主要内容区域(.lion .wrap)之外时,该框也将关闭。

Add this to the bottom of your script: 将此添加到脚本的底部:

// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
  if (event.target == lionoverlay) {
    lionoverlay.setAttribute('aria-hidden', true);
  }
}

 var body = document.body, lionoverlay = document.querySelector('.lion'), lionoverlayBtts = document.querySelectorAll('button[class$="lion"]'); [].forEach.call(lionoverlayBtts, function(btt) { btt.addEventListener('click', function() { /* Detect the button class name */ var lionoverlayOpen = this.className === 'open-lion'; /* Toggle the aria-hidden state on the overlay and the no-scroll class on the body */ lionoverlay.setAttribute('aria-hidden', !lionoverlayOpen); body.classList.toggle('noscroll', lionoverlayOpen); /* On some mobile browser when the overlay was previously opened and scrolled, if you open it again it doesn't reset its scrollTop property after the fadeout */ setTimeout(function() { lionoverlay.scrollTop = 0; }, 1000) }, false); }); // When the user clicks anywhere outside of the modal, close it window.onclick = function (event) { if (event.target == lionoverlay) { lionoverlay.setAttribute('aria-hidden', true); } } 
 li { list-style: none; } button.open-lion { background: none!important; border: none; color: #1a1a1a; cursor: pointer; font-family: 'Raleway', Arial, Helvetica, sans-serif; font-size: 11px; padding: 0; text-decoration: none; text-transform: none; &:hover { color: #898989; text-decoration: none; } } button.close-lion { background: none!important; border: none; color: #1a1a1a; font-size: 30px; height: 30px; line-height: 30px; padding: 0; position: absolute; right: 20px; text-transform: none; top: 20px; transition: 0; width: 30px; } button.close-lion:hover { background: none; color: #898989; } .noscroll { overflow: hidden; } .lion { background: rgba(247, 247, 247, 0.75); bottom: 0; left: 0; overflow-y: scroll; overscroll-behavior: contain!important; position: fixed; right: 0; text-align: left; top: 0; /*transition: opacity 0s!important;*/ } #lionID[aria-hidden="true"] { opacity: 0; width: 100vw; z-index: -1; } #lionID[aria-hidden="false"] { opacity: 1; width: 100%; z-index: 1510; } .lion .wrap { background: #ffffff; left: 50%; margin-bottom: 60px; max-width: 800px; padding: 45px; position: absolute; top: 60px; transform: translateX(-50%); width: 85%; } 
 <div class="detail-buttons-wrap"> <div class="detail-buttons"> <ul> <li><button type="button" class="open-lion">Sizing</button></li> </ul> </div> <section id="lionID" class="lion" aria-hidden="true"> <div class="wrap"> <button type="button" class="close-lion">X</button> CONTENT of LION will be here. </div> </section> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM