用H5写一个自动弹出的弹窗代码
时间: 2024-06-12 18:08:03 浏览: 84
<!DOCTYPE html>
<html>
<head>
<title>自动弹出弹窗</title>
<script type="text/javascript">
window.onload = function() {
setTimeout(function() {
var popup = document.getElementById('popup');
popup.classList.add('active');
}, 3000); // 3000毫秒后自动弹出
}
</script>
<style type="text/css">
#popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
background-color: #fff;
box-shadow: 0px 0px 10px rgba(0,0,0,0.5);
display: none;
}
#popup.active {
display: block;
}
#popup h2 {
margin-top: 0;
}
#popup p {
margin-bottom: 0;
}
#popup button {
margin-top: 10px;
padding: 5px 10px;
background-color: #ccc;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="popup">
<h2>自动弹出的弹窗</h2>
<p>3秒后自动弹出</p>
<button onclick="closePopup()">关闭</button>
</div>
<script type="text/javascript">
function closePopup() {
var popup = document.getElementById('popup');
popup.classList.remove('active');
}
</script>
</body>
</html>
阅读全文