仅CSS弹出 - 页面在关闭时移动

时间:2016-01-11 23:06:33

标签: html css popup

我正在尝试创建一个仅限CSS的欢迎弹出窗口。一切正常,除非你关闭它,页面转移到popup 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">&times;</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%;
}
}

1 个答案:

答案 0 :(得分:2)

  • 变化

    .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伪通常会将焦点放在与其链接的元素上。这意味着它将向上或向下滚动&#39;找到该元素的页面。有用的链接可以了解这个伪的工作原理here

    这是工作片段(限制:弹出框不会滑出窗口,如示例中所示....)

    &#13;
    &#13;
    *, *: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%;
      }
    }
    &#13;
    <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">&times;</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>
    &#13;
    &#13;
    &#13;

  • 相关问题