简体   繁体   English

打开和关闭模式窗口不起作用

[英]Open and close modal windows not working

What am trying to do is when a user clicks the ".inner" element it finds the modal window that is inside of it and removes the "slideOutLeft" class if there is one and then adds the "slideInLeft" class and fades in the overlay element. 要做的是,当用户单击“ .inner”元素时,它会找到其中的模式窗口并删除“ slideOutLeft”类(如果有的话),然后添加“ slideInLeft”类并在叠加层中淡入淡出元件。 This works fine . 这很好用

But what doesn't work is when you click the .closeBtn element it doesn't remove the "slideInLeft" and then add the "slideOutLeft" class. 但是,当你点击.closeBtn元素,它不会删除“slideInLeft”,然后添加“slideOutLeft”类什么行不通的。 But when you click the black overlay behind the modal window it does function properly. 但是,当您单击模态窗口后面的黑色覆盖时 ,它可以正常运行。

I'm obviously missing something but I can't figure it out. 我显然缺少了一些东西,但我无法弄清楚。

Here is a fiddle with all my code 这是我所有代码的小提琴

 $(document).ready(function() { $('.inner').click(function() { $(this).find('.modalWindow').removeClass('slideOutLeft').addClass('slideInLeft').css('display', 'block'); $('.overlay').fadeToggle(500); }); $('.closeBtn, .overlay').click(function() { $('.modalWindow').removeClass('slideInLeft').addClass('slideOutLeft'); $('.overlay').fadeToggle(500); }); }); 
 *, *:before, *:after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .wrapper { width: 50%; float: left; text-align: center; padding: 20px; } .modalBtn { padding: 15px 30px; border: 1px solid #333; border-radius: 5px; text-transform: uppercase; } .overlay { width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.3); position: fixed; top: 0; left: 0; z-index: 999997; display: none; } .modalWindow { width: 80%; right: 0; left: 0; top: 50%; margin: 0 auto; height: auto; position: fixed; display: none; -moz-transform: translateY(-50%); -ms-transform: translateY(-50%); -webkit-transform: translateY(-50%); transform: translateY(-50%); z-index: 999998; background-color: white; padding: 20px; } .closeBtn { font-size: 32px; color: #333; position: absolute; right: 20px; top: 5px; } .inner:hover { cursor: pointer; } /* Animations */ .animated { -webkit-animation-duration: 1s; animation-duration: 1s; -webkit-animation-fill-mode: both; animation-fill-mode: both; } /*the animation definition*/ @-webkit-keyframes slideInLeft { 0% { -webkit-transform: translate3d(-100%, 0, 0); transform: translate3d(-100%, 0, 0); visibility: visible } 100% { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0) } } @keyframes slideInLeft { 0% { -webkit-transform: translate3d(-125%, 0, 0); -ms-transform: translate3d(-125%, 0, 0); transform: translate3d(-125%, 0, 0); visibility: visible } 100% { -webkit-transform: translate3d(0, 0, 0); -ms-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0) } } .slideInLeft { -webkit-animation-name: slideInLeft; animation-name: slideInLeft } @-webkit-keyframes slideOutLeft { 0% { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0) } 100% { visibility: hidden; -webkit-transform: translate3d(-100%, 0, 0); transform: translate3d(-100%, 0, 0) } } @keyframes slideOutLeft { 0% { -webkit-transform: translate3d(0, 0, 0); -ms-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0) } 100% { visibility: hidden; -webkit-transform: translate3d(-125%, 0, 0); -ms-transform: translate3d(-125%, 0, 0); transform: translate3d(-125%, 0, 0) } } .slideOutLeft { -webkit-animation-name: slideOutLeft; animation-name: slideOutLeft } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="overlay" class="overlay"></div> <div class="wrapper"> <div class="inner"> <div class="modalBtn">click for modal 1</div> <div class="modalWindow animated"> <div class="closeBtn">X</div> <span>This is modal window 1</span> </div> </div> </div> <div class="wrapper"> <div class="inner"> <div class="modalBtn">click for modal 2</div> <div class="modalWindow animated"> <div class="closeBtn">X</div> <span>This is modal window 2</span> </div> </div> </div> 

Problem is in your html structure, so when you click on close button you are clicking on .inner div, too: Fix: 问题出在您的html结构中,因此,当您单击关闭按钮时,您也同时单击了.inner div:修复:

 $('.closeBtn, .overlay').click(function(e){
  e.stopPropagation();
    $('.modalWindow').removeClass('slideInLeft').addClass('slideOutLeft');
    $('.overlay').fadeToggle(500);
  });

Demo: https://jsfiddle.net/ssjfu611/24/ 演示: https : //jsfiddle.net/ssjfu611/24/

The issue is your <div class="inner"> is still clickable please change your HTML mark up: 问题是您的<div class="inner">仍可点击,请更改您的HTML标记:

<div class="wrapper">
  <div class="inner">
    <div class="modalBtn">click for modal 1</div>
  </div>

  //KEEP THIS OUT OF THE INNER DIV
  <div class="modalWindow animated">
    <div class="closeBtn">X</div>
    <span>This is modal window 1</span>
  </div>
</div>

Then your JavaScript too: 然后你的JavaScript:

$('.inner').click(function() {
 $(this).parent().find('.modalWindow').removeClass('slideOutLeft').addClass('slideInLeft').css('display', 'block');
 $('.overlay').fadeToggle(500);
});

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

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