html弹窗美化代码
时间: 2023-06-30 18:14:32 浏览: 134
以下是一个简单的 HTML 弹窗美化代码,你可以根据自己的需求进行修改:
<!DOCTYPE html>
<html>
<head>
<title>弹窗美化</title>
<style type="text/css">
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
z-index: 999;
display: none;
}
.popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
z-index: 1000;
display: none;
}
.close-btn {
position: absolute;
top: -20px;
right: -20px;
width: 40px;
height: 40px;
line-height: 40px;
text-align: center;
font-size: 20px;
color: #fff;
background-color: #333;
border-radius: 50%;
cursor: pointer;
}
</style>
</head>
<body>
<div class="overlay"></div>
<div class="popup">
<div class="close-btn">X</div>
<h2>这是一个弹窗</h2>
<p>这里可以放一些内容</p>
</div>
<button onclick="showPopup()">点击打开弹窗</button>
<script type="text/javascript">
function showPopup() {
document.querySelector('.overlay').style.display = 'block';
document.querySelector('.popup').style.display = 'block';
}
document.querySelector('.close-btn').addEventListener('click', function() {
document.querySelector('.overlay').style.display = 'none';
document.querySelector('.popup').style.display = 'none';
});
</script>
</body>
</html>
这段代码中,使用了两个 CSS 类 overlay
和 popup
来定义遮罩层和弹窗的样式。其中,overlay
类定义了遮罩层的样式,popup
类定义了弹窗的样式。同时还包括了一个关闭按钮的样式,以及使用 JavaScript 实现弹窗的显示和隐藏。
相关推荐















