我正在尝试创建一个仅限 CSS 的欢迎弹出窗口。一切正常,除非您关闭它时,页面会向下移动到弹出 div 原来所在的位置。我该如何修复它,以便在关闭弹出窗口时页面不会移动?
我正在使用以下....
http://codepen.io/john84/pen/WrOZJj
HTML
<h1>Popup/Modal Windows without JavaScript</h1>
<div class="box">
<a class="button" href="#">Login /signup</a>
</div>
<div id="popup1" class="overlay">
<div class="popup">
<h2>Here i am</h2>
<a class="close" href="#popup1">×</a>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eleifend nunc non malesuada feugiat. Fusce lacinia urna sed nunc accumsan viverra.
</div>
</div>
</div>
CSS
*, *:after, *:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background: url(http://www.shukatsu-note.com/wp-content/uploads/2014/12/computer-564136_1280.jpg) no-repeat;
background-size: cover;
height: 100vh;
}
h1 {
text-align: center;
font-family: Tahoma, Arial, sans-serif;
color: #06D85F;
margin: 100px 0;
}
.box {
width: 40%;
margin: 0 auto;
background: rgba(255,255,255,0.2);
padding: 35px;
border: 2px solid #fff;
border-radius: 20px/50px;
background-clip: padding-box;
text-align: center;
}
.button {
font-size: 1em;
padding: 10px;
color: #fff;
border: 2px solid #06D85F;
border-radius: 20px/50px;
text-decoration: none;
cursor: pointer;
transition: all 0.3s ease-out;
}
.button:hover {
background: #06D85F;
}
.popup h2 {
margin-top: 0;
color: #333;
font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
position: absolute;
top: 20px;
right: 30px;
transition: all 0.2s;
font-size: 30px;
font-weight: bold;
text-decoration: none;
color: #333;
}
.popup .close:hover {
color: #06D85F;
}
.popup .content {
max-height: 30%;
overflow: auto;
}
/*Let's make it appear when the page loads*/
.overlay:target:before {
display: none;
}
.overlay:before {
content:"";
top: 0;
left: 0;
right: 0;
bottom: 0;
display: block;
background: rgba(0, 0, 0, 0.6);
position: fixed;
z-index: 9;
}
.overlay .popup {
background: #fff;
border-radius: 5px;
width: 30%;
position: fixed;
top: 0;
left: 35%;
padding: 25px;
margin: 70px auto;
z-index: 10;
-webkit-transition: all 0.6s ease-in-out;
-moz-transition: all 0.6s ease-in-out;
transition: all 0.6s ease-in-out;
}
.overlay:target .popup {
top: -100%;
left: -100%;
}
@media screen and (max-width: 768px){
.box{
width: 70%;
}
.overlay .popup{
width: 70%;
left: 15%;
}
}
最佳答案
.overlay:target:before {
display: none;
}
到
.overlay:target{
display: none;
}
<div class="box">
<a class="button" href="#">Login /signup</a>
到
<div class="box" id='box'>
<a class="button" href="#box">Login /signup</a>
这将强制 :target
在单击按钮时将焦点放在按钮上,并在按钮所在的位置显示您的框。 :target
伪通常将焦点添加到它链接到的元素上。这意味着它将“向上或向下滚动”页面以找到该元素。了解此伪工作原理的有用链接是 here
这是工作代码段(限制:如您的示例所示,弹出框不会滑出窗口....)
*, *:after, *:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background: url(http://www.shukatsu-note.com/wp-content/uploads/2014/12/computer-564136_1280.jpg) no-repeat;
background-size: cover;
height: 100vh;
}
h1 {
text-align: center;
font-family: Tahoma, Arial, sans-serif;
color: #06D85F;
margin: 100px 0;
}
.box {
width: 40%;
margin: 0 auto;
background: rgba(255,255,255,0.2);
padding: 35px;
border: 2px solid #fff;
border-radius: 20px/50px;
background-clip: padding-box;
text-align: center;
}
.button {
font-size: 1em;
padding: 10px;
color: #fff;
border: 2px solid #06D85F;
border-radius: 20px/50px;
text-decoration: none;
cursor: pointer;
transition: all 0.3s ease-out;
}
.button:hover {
background: #06D85F;
}
.popup h2 {
margin-top: 0;
color: #333;
font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
position: absolute;
top: 20px;
right: 30px;
transition: top,right 0.2s;
font-size: 30px;
font-weight: bold;
text-decoration: none;
color: #333;
}
.popup .close:hover {
color: #06D85F;
}
.popup .content {
max-height: 30%;
overflow: auto;
}
/*Let's make it appear when the page loads*/
#popup1:target{
top: -100%;
left: -100%;
display:none;
}
.overlay:before {
content:"";
top: 0;
left: 0;
right: 0;
bottom: 0;
display: block;
background: rgba(0, 0, 0, 0.6);
position: fixed;
z-index: 9;
}
.overlay .popup {
background: #fff;
border-radius: 5px;
width: 30%;
position: fixed;
top: 0;
left: 35%;
padding: 25px;
margin: 70px auto;
z-index: 10;
-webkit-transition: all 0.6s ease-in-out;
-moz-transition: all 0.6s ease-in-out;
transition: all 0.6s ease-in-out;
}
.overlay:target .popup {
top: -100%;
left: -100%;
}
@media screen and (max-width: 768px){
.box{
width: 70%;
}
.overlay .popup{
width: 70%;
left: 15%;
}
}
<h1>Popup/Modal Windows without JavaScript</h1>
<div class="box" id='box'>
<a class="button" href="#box">Login /signup</a>
</div>
<br><br><br><br><br><br><br><br>
<div id="popup1" class="overlay">
<div class="popup">
<h2>Here i am</h2>
<a class="close" href="#popup1">×</a>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eleifend nunc non malesuada feugiat. Fusce lacinia urna sed nunc accumsan viverra.
</div>
</div>
</div>
关于html - 仅 CSS 弹出窗口 - 页面在关闭时移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34732801/