删除确认模态
创建于 2024-12-03 /
52
字体:
[默认]
[大]
[更大]
了解如何使用 CSS 创建删除确认模式。
点击按钮打开模态:
×Are you sure you want to delete your account?
如何创建删除模式
步骤 1) 添加 HTML:
实例
<button onclick="document.getElementById('id01').style.display='block'">Open Modal</button><div id="id01" class="modal">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
<form class="modal-content" action="/action_page.php">
<div class="container">
<h1>Delete Account</h1>
<p>Are you sure you want to delete your account?</p>
<div class="clearfix">
<button type="button" class="cancelbtn">Cancel</button>
<button type="button" class="deletebtn">Delete</button>
</div>
</div>
</form>
</div>
步骤 2) 添加 CSS:
实例
* {box-sizing: border-box}/* 为所有按钮设置样式 */
button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
opacity: 0.9;
}
button:hover {
opacity:1;
}
/* 浮动取消和删除按钮并添加等宽 */
.cancelbtn, .deletebtn {
float: left;
width: 50%;
}
/* 为取消按钮添加颜色 */
.cancelbtn {
background-color: #ccc;
color: black;
}
/* 为删除按钮添加颜色 */
.deletebtn {
background-color: #f44336;
}
/* 为容器添加内边距和居中对齐文本 */
.container {
padding: 16px;
text-align: center;
}
/* 模态(背景) */
.modal {
display: none; /* 默认隐藏 */
position: fixed; /* 原地不动 */
z-index: 1; /* 坐在上面 */
left: 0;
top: 0;
width: 100%; /* 全宽 */
height: 100%; /* 全高 */
overflow: auto; /* 如果需要,启用滚动 */
background-color: #474e5d;
padding-top: 50px;
}
/* 模态内容/框 */
.modal-content {
background-color: #fefefe;
margin: 5% auto 15% auto; /* 从顶部开始 5%,从底部开始 15% 并居中 */
border: 1px solid #888;
width: 80%; /* 可能或多或少,取决于屏幕大小 */
}
/* 设置水平尺样式 */
hr {
border: 1px solid #f1f1f1;
margin-bottom: 25px;
}
/* 模态关闭按钮 (x) */
.close {
position: absolute;
right: 35px;
top: 15px;
font-size: 40px;
font-weight: bold;
color: #f1f1f1;
}
.close:hover,
.close:focus {
color: #f44336;
cursor: pointer;
}
/* 清除浮动 */
.clearfix::after {
content: "";
clear: both;
display: table;
}
/* 更改超小屏幕上取消按钮和删除按钮的样式 */
@media screen and (max-width: 300px) {
.cancelbtn, .deletebtn {
width: 100%;
}
} 亲自试一试 »
提示:您还可以使用以下 javascript 通过单击模式内容外部来关闭模式(而不仅仅是使用"x"或"取消"按钮来关闭它):
实例
<script>// 获取模态
var modal = document.getElementById('id01');
// 当用户点击模态框外的任意位置时,关闭它
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script> 亲自试一试 »
0 人点赞过