jQuery 弹层(也称为模态框、对话框)是一种网页交互元素,用于在当前页面上显示额外的内容,通常用于提示信息、表单验证、用户输入等场景。弹层会覆盖在当前页面内容之上,阻止用户与底层内容的交互,直到用户完成操作或关闭弹层。
以下是一个简单的 jQuery 模态弹层的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 弹层示例</title>
<style>
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<button id="openModalBtn">打开弹层</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>这是一个简单的 jQuery 弹层示例。</p>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
var modal = $('#myModal');
var span = $('.close');
$('#openModalBtn').click(function(){
modal.show();
});
span.click(function(){
modal.hide();
});
$(window).click(function(event){
if (event.target == modal[0]) {
modal.hide();
}
});
});
</script>
</body>
</html>
overflow: hidden
在 body 上,防止页面滚动。overflow: hidden
在 body 上,防止页面滚动。通过以上内容,你应该对 jQuery 弹层有了全面的了解,并能够解决一些常见问题。
没有搜到相关的文章