简体   繁体   English

如何模仿Bootstrap中模式弹出窗口的阴影效果?

[英]How to mimic the shading effect of modal popups in Bootstrap?

In Bootstrap, there's the effect of modal popup window where the rest of the page gets inactivated (when clicking on a link, for instance) and grayed out. 在Bootstrap中,具有模式弹出窗口的作用,该页面的其余部分均被停用(例如,单击链接时)并变灰。 I'd like to mimic that effect in my own CSS. 我想在自己的CSS中模仿这种效果。

模态图像

After looking for a few hours into the CSS files of the package, I concluded that I won't be able to back-engineer the behavior myself. 在查看软件包的CSS文件几个小时后,我得出结论,我将无法对行为进行自我设计。 Simply too much junk in the trunk for me. 对于我来说,行李箱中太多的垃圾。

How can I mimic the described behavior in a custom way? 如何以自定义方式模仿所描述的行为? Or did I happen to hit a very complicated issue that requires border-line black magic to get working? 还是我碰巧遇到了一个非常复杂的问题,需要边界黑魔法开始工作?

You can add a div that covers the entire page with a higher z-index than your content. 您可以添加一个z索引比内容高的div来覆盖整个页面。 Make the div partially see through to get the desired look. 使div部分透视以得到所需的外观。

If you are adding a modal/popup, make sure this has a higher z-index than the backdrop. 如果要添加模式/弹出窗口,请确保其Z-index高于背景。

Here's an example: 这是一个例子:

 $('#test').click(function() { var backdropHeight = $(document).height(); $('#backdrop').css('height', backdropHeight); $('#backdrop').fadeIn(100, function() { $('#modal').fadeIn(200); }); }); $('#closeModal').click(function() { $('#modal').fadeOut(200, function() { $('#backdrop').fadeOut(100); }); }); 
 #backdrop { display: none; position: absolute; top: 0; left: 0; height: 100%; width: 100%; background: rgba(0, 0, 0, 0.3); z-index: 10; } #modal { display: none; position: absolute; left: calc(50% - 100px); top: calc(50% - 100px); height: 200px; width: 200px; background: #fff; text-align: center; z-index: 11; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <h1>Hellow World</h1> <button id="test">Show Modal</button> <div id="modal"> <h2>My modal</h2> <button id="closeModal">Close Modal</button> </div> <div id="backdrop"></div> 

There's nothing complicated about it. 没有什么复杂的。 It's just a "backdrop" using an absolute positioned element with partial opacity so that elements behind it show through. 它只是一个使用部分不透明的绝对定位元素的“背景”,以便其后面的元素得以显示。

.backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1040;
  background-color: #000;
  opacity: 0.5;
}

Demo http://www.codeply.com/go/KhCqGTSCBe 演示http://www.codeply.com/go/KhCqGTSCBe

I guess the simplest solution is to override .modal-backdrop.show class. 我猜最简单的解决方案是重写.modal-backdrop.show类。

By default it is set to: 默认情况下,它设置为:

.modal-backdrop.show{
    opacity: .5;
}

and you can just override it to 您可以将其覆盖为

.modal-backdrop.show{
    opacity: 0!important; /* to disable "gray effect" */
    /* OR */
    opacity: 1;
    background: red;
    /* to change background to red */
}

You can change anything u want in this class 你可以在这堂课上改变你想要的任何东西

use css position & z-index and opacity can implements modal overlay. 使用css position和z-index和opacity可以实现模式叠加。

 $('input').click(function(){ var methodName=$(this).attr('id'); $('.modal-dialog')[methodName]();; }); 
 html,body{border:0;padding:0;margin:0;} .modal-dialog,.overlay{ position:absolute; width:100%; height:100%; left:0; top:0; } .overlay{ background:#000; opacity:0.1; z-index:100; } .dialog{ position:absolute; z-index:1000; left:50%; top:50%; } .modal-dialog{ display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" id='fadeIn' value="Show"/> <div class='modal-dialog'> <div class='dialog'> <input id='fadeOut' type='button' value='Cancel'> </div> <div class="overlay"></div> </div> 

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

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