html - 使用 html、CSS 自动弹出关闭按钮

标签 html css

您好,我有代码可以在单击按钮时生成弹出窗口。 但是任何人都可以修改我的代码,以便我可以将该代码插入我的页面并在我的页面加载时自动弹出。

我希望在完成表单后出现的元素成功页面中插入此代码。

.box {
  width: 20%;
  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 orange;
  border-radius: 20px/50px;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease-out;
}
.button:hover {
  background: orange;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}

.popup h2 {
  margin-top: 0;
  color: #333;
  font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
.popup .close:hover {
  color: orange;
}
.popup .content {
  max-height: 30%;
  overflow: auto;
}
<div class="box">
	<a class="button" href="#popup1">Let me Pop up</a>
</div>

<div id="popup1" class="overlay">
	<div class="popup">
		<h2>Here i am</h2>
		<a class="close" href="#">×</a>
		<div class="content">
			Thank to pop me out of that button, but now i'm done so you can close this window.
		</div>
	</div>
</div>

最佳答案

您正在寻找 target CSS psuedo-class .

如果没有 JQuery,您可以使用片段标识符。 ID 与片段标识符匹配的任何元素都将自动应用 :target CSS 伪类。

然后您可以使用 CSS 规则默认隐藏弹出窗口并在 :targeted 时显示它(或相反),并添加按钮以打开和关闭弹出窗口导航到相关片段。

.box {
    width: 20%;
    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 orange;
    border-radius: 20px/50px;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.3s ease-out;
}

.button:hover {
    background: orange;
}

.popup {
    margin: 70px auto;
    padding: 20px;
    background: #fff;
    border-radius: 5px;
    width: 30%;
    position: relative;
    transition: all 5s ease-in-out;
}

.popup h2 {
    margin-top: 0;
    color: #333;
    font-family: Tahoma, Arial, sans-serif;
}

.popup .close {
    position: absolute;
    top: 20px;
    right: 30px;
    transition: all 200ms;
    font-size: 30px;
    font-weight: bold;
    text-decoration: none;
    color: #333;
}

.popup .close:hover {
    color: orange;
}

.popup .content {
    max-height: 30%;
    overflow: auto;
}

.overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.7);
}

/* These are the rules you are looking for. */
.overlay {
    transition: opacity 500ms;
    opacity: 1;
    pointer-events: all;
}

.overlay:target {
    opacity: 0;
    pointer-events: none;
}
<div class="box">
    <!-- This shows the popup. -->
    <a class="button" href="#popup">Let me Pop up</a>
</div> 

<div id="popup1" class="overlay">
    <div class="popup" >
        <h2>Here i am</h2>
        <!-- This hides it. -->
        <a class="close" href="#popup1">×</a>
        <div class="content">
            Thank to pop me out of that button, but now I'm done so you can close this window.
        </div>
    </div>
</div>

关于html - 使用 html、CSS 自动弹出关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29345016/

相关文章:

html - 使用 ReactJS 映射物化 css <select> 的 <option>

javascript - HTML Textarea,将光标放在底线(CSS)?

javascript - 使用 angularjs 过滤器显示包含特定 css 类的元素

html - 尽管宽度和高度为 100%,但父 div 仍大于子 div

css - 在 vue js 组件的类中插入字符串

HTML 插入 "tab"

html - 以响应居中div位置

html - 将鼠标悬停在文本上时如何防止文本更改?

html - CSS,我可以在一个表格单元格中使用两种不同的颜色吗? (例如深紫色和浅紫色)

css - 如何更改将级联出来的 JavaFX 8 Modena 主题中的 BASE 字体大小?