我想制作一个不使用JavaScript的简单模态,但是能够单击背景以关闭模态。
我发现了这个我喜欢的演示,除了有一种方法可以使地址栏不包含模式的ID?还是可以使用另一种技巧来制作具有可点击背景而不使用javascript的模式。
我希望这是您所需要的(下面有一个在线示例)。
但是我的解决方案有一个局限性:只能有一个模态窗口,不能再有其他!
原意:https://www.cssscript.com/minimal-overlay-modal-pure-css/
.js-btn:focus ~ #modal, .js-btn:active ~ #modal,
#modal:focus {
visibility: visible;
opacity: 1;
}
body {
font-family: serif;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.5);
transition: opacity 200ms;
visibility: hidden;
opacity: 0;
transition: 500ms;
}
.overlay .cancel {
position: absolute;
width: 100%;
height: 100%;
cursor: default;
}
.modal {
margin: 100px auto;
padding: 20px;
background: #fff;
border: 1px solid #666;
width: 300px;
border-radius: 6px;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
position: relative;
}
button {
background-color: #fff;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
cursor: pointer;
display: block;
font-weight: 300;
height: 50px;
padding: 15px;
text-align: center;
border-radius: 6px;
margin: 40px auto;
max-width: 200px;
opacity: 1;
color: #333;
text-decoration: none;
transition: 0.3s box-shadow ease;
transform: translateY(0px);
text-shadow: 0 0 0;
}
button:hover { box-shadow: 0 12px 23px rgba(0, 0, 0, 0.23), 0 10px 10px rgba(0, 0, 0, 0.19); }
<h1 align="center">Puse CSS Modal with overlay</h1>
<button class="js-btn">Open Modal</button>
<div id="modal" class="overlay" tabindex="-1">
<a class="cancel" href="#"></a>
<div class="modal">
<h2>This is Modal Overlay 1</h2>
<div class="content">
<p>Click outside the modal to close.</p>
</div>
</div>
</div>
您可以用checkbox hack做一些很酷的事情。
基本上,您(滥用)使用标签及其远程检查/取消选中复选框输入的能力。复选框处于活动状态(:checked
)时,您可以使用相邻的同级组合器(+
)设置下一个元素及其所有子元素的样式,在此情况下为模式和灯箱效果。您也可以使用常规的同级组合器(~
)来获得相同的效果,但是我发现与相邻的组合器进行作用域更容易。
为了使模式可以打开和关闭,我使用了两个标签。第一个打开模式(选中该复选框),另一个关闭模式(取消选中该复选框)。您可以使用更多标签来关闭按钮,或使用其他方式打开模式,等等。只需确保for
属性与复选框的id
相匹配。
真正需要的所有设置是复选框输入,两个标签,触发器(由一个标签包裹),模态容器(用于定位-我也将其用于深色背景)以及模态本身。只需确保模式具有比结束标签更高的z-index(或在其自身的堆叠上下文中),否则单击模式将自行关闭。
我保留了该复选框的可见性,因此您可以更好地了解其工作原理。
.lightbox {
display: none;
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
justify-content: center;
align-items: center;
background-color: #000A;
}
#modal-toggle:checked + .lightbox {
display: flex;
}
.lightbox > label {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.modal {
z-index: 1;
}
/* For looks only */
.modal-trigger {
margin-top: 50px;
}
.modal,
.modal-trigger {
display: inline-block;
background-color: white;
border-radius: 5px;
padding: 1.5rem;
text-align: center;
}
.modal *:first-child {
margin-top: 0;
}
.modal *:last-child {
margin-bottom: 0;
}
body {
background-color: #DDD;
display: flex;
flex-direction: column;
align-items: center;
font-family: sans-serif;
}
#modal-toggle {
position: fixed;
top: 0;
left: 0;
z-index: 1;
}
<h1>Pure CSS Modal - Checkbox Hack</h1>
<label class="modal-trigger" for="modal-toggle">
Open Modal
</label>
<input type="checkbox" id="modal-toggle">
<div class="lightbox">
<label for="modal-toggle"></label>
<div class="modal">
<h3>This is A Modal</h3>
<p>Click outside the modal to close</p>
</div>
</div>
我也在同一页上也做了一个codepen with multiple modals。