<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>jq点击自身以外地方关闭弹出窗</title>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<style>
* {
margin: 0;
padding: 0
}
#branch {
width: 100%;
height: auto;
}
/* 按钮 */
#branch .branch-header button {
margin: 300px 600px;
}
/* 弹出框 */
#branch .branch-proup {
display: none;
position: fixed;
top: 0;
left: 0;
z-index: 10;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .2);
border-radius: 5px 5px 0 0;
}
#branch .branch-proup-content {
position: fixed;
top: 50%;
left: 50%;
width: 320px;
height: 320px;
border: 1px solid #ccc;
margin-top: -160px;
margin-left: -160px;
background-color: #fff;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="branch">
<div class="branch-header">
<button>点击出现弹出框</button>
</div>
<!-- 弹出框 -->
<div class="branch-proup">
<div class="branch-proup-content">
<!-- 弹出框内容 -->
</div>
</div>
</div>
<script type="text/javascript">
$(function() {
// 点击按钮
var btn = $('#branch .branch-header button');
// 弹出框
var proup = $('#branch .branch-proup');
// 点击按钮 弹窗出现
btn.click(function() {
proup.show();
})
// 点击弹窗内容以外的地方关闭弹窗
proup.on('click', function(e) {
if ($(e.target).closest('.branch-proup-content').length > 0) {
alert('弹出框内部被点击了');
} else {
alert('弹出框以外的部分被点击了');
// 关闭弹框
proup.hide();
}
});
})
</script>
</body>
</html>
Jquery 实现弹出框以外的地方关闭弹出框
最新推荐文章于 2024-05-28 12:36:33 发布