简体   繁体   English

为什么单击按钮时没有出现我的黑色叠加层?

[英]Why isn't my black overlay appearing when button is clicked?

I am trying to create a pop-up window myself.我正在尝试自己创建一个弹出窗口 window。 I want my pop-up box to appear when the button is pressed and everything below to get darkened.我希望我的弹出框在按下按钮时出现,并且下面的所有内容都变暗。 However, when I press my button whole page hangs and no popup appears also.但是,当我按下按钮时,整个页面都会挂起,并且也不会出现任何弹出窗口。

If I remove the div which turns everything background black, my popup is working fine.如果我删除将所有背景变为黑色的 div,则我的弹出窗口工作正常。

Here is my html code which has script tags inside这是我的 html 代码,里面有脚本标签

 let visible = false; $('#showBox').on('click', function(params) { if (visible) { $('.box').removeClass('boxVisible'); $('.blackenBackground').removeClass('boxVisible'); visible = false; } else { $('.box').addClass('boxVisible'); $('.blackenBackground').addClass('boxVisible'); visible = true; } })
 .box { background: pink; height: 500px; width: 500px; position: absolute; top: 50%; left: 50%; z-index: 3; transform: translate(-50%, -50%); border-radius: 1%; opacity: 0; transition: 0.4s; transition-timing-function: ease-out; }.boxVisible { opacity: 1; }.blackenBackground { position: fixed; top: 0; left: 0; height: 100vh; width: 100vw; background: black; opacity: 0; z-index: 2; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box"></div> <p>Some lorem text..... </p> <button id="showBox">Show Box</button> <!-- When I remove this popup box works perfectly but background isn't darkening and my page hangs --> <div class="blackenBackground"></div>

Your fixed position div is blocking mouse events.您固定的 position div 正在阻止鼠标事件。 The opacity is 0 but the box is still technically visible, which catches the click and prevents the button from getting clicked.不透明度为 0,但该框在技术上仍然可见,它会捕获单击并防止按钮被单击。

Just make sure the box is completely hidden and it should work.只要确保盒子完全隐藏并且它应该可以工作。

 let visible = false; $('#showBox').on('click', function (params) { if(visible){ $('.box').removeClass('boxVisible'); $('.blackenBackground').removeClass('boxVisible'); visible = false; }else{ $('.box').addClass('boxVisible'); $('.blackenBackground').addClass('boxVisible'); visible = true; } })
 .box{ background: pink; height: 500px; width: 500px; position: absolute; top: 50%; left: 50%; z-index: 3; transform: translate(-50%, -50%); border-radius: 1%; opacity: 0; transition: 0.4s; transition-timing-function: ease-out; }.boxVisible{ opacity: 1; display: block; }.blackenBackground{ position: fixed; top: 0; left: 0; height: 100vh; width: 100vw; background: black; opacity: 0; display: none; z-index: 2; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box"></div> <p>Some lorem text.....</p> <button id="showBox">Show Box</button> <div class="blackenBackground"></div>

Did you want something like this?你想要这样的东西吗? I added an id element to the div and changed you addclass call in your Jquery to document.getElementById('dimmer').style.display= 'none / block' in your if-else statements and changed the .css class to我在div中添加了一个id元素,并将您的 Jquery 中的 addclass 调用更改为if-else语句中document.getElementById('dimmer').style.display= 'none / block'并将.css 8CBA22E28EB17B5F5C6AE2A2662CAB42F2ADB4F8EB40 更改为

.blackenBackground{
    pointer-events: none;
    background:#000;
    opacity:0.5;
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    display:none;
}

jsFiddle: https://jsfiddle.net/j0d8emsc/ jsFiddle: https://jsfiddle.net/j0d8emsc/

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

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