我有一個覆蓋DIV,一旦用戶點擊鏈接就會出現。用戶可以通過點擊DIV中的「X」鏈接關閉覆蓋層。我希望用戶能夠通過點擊頁面上的任何位置來關閉疊加DIV。請幫助我實現此功能。以下是我的代碼...如何通過點擊身體上的任何位置關閉彈出DIV
$(function(){
var mHeight = $(window).height();
var popHeight = $(window).height();
var mWidth = $(window).width();
var popWidth = $(window).width();
$(".pop_Show").click(function(){
if(mHeight < popHeight){
$(".pop_Content").css({position: "absolute", "margin-top": "0"});
$(".pop_Content").animate({top: '0'}, "slow");
}else{
$(".pop_Content").animate({top: '50px'}, "slow");
}
if(mWidth < popWidth){
$(".pop_Content").css({left: "0", "margin-left": "0"});
}
$("body").append("<div class='disable_bg'></div>");
});
//Script for hiding the overlay div by clicking on X
$(".pop_Close").click(function(){
var popHeight2 = popHeight+500;
$(".pop_Content").animate({top: "-"+popHeight2}, "100",function(){$(".disable_bg").remove();});
});
// I want the script for hiding the overlay by clicking anywhere in the page
});
.pop_Content {
overflow: hidden;
z-index:2500;
position:fixed;
top:-2000px;
left: 50%;
margin-left:-150px;
width:300px;
height:100px;
background:#ccc;
padding:15px;
}
.pop_Close{
position:absolute;
z-index:1000;
top:0;
right:0;
float:right;
cursor:pointer;
margin:0px 10px;
color:#595959;
font:1.5em verdana;
text-align:center;
}
.pop_Close:before {
content: "x";
}
.disable_bg {
background: black;
opacity: .5;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
position: fixed;
z-index: 2450;
}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<body>
<a href="#" class="pop_Show">Click Me</a>
<!--Overlay Div-->
<div class="pop_Content"><a class="pop_Close"></a>
I am the Overlay Div
</div>
</body>
</html>
嘗試$( 「體> *」)。不是( 「tc_bg」)。點擊(函數(){....}) –
請發表您的全部代碼。代碼片段主要用於顯示完整的代碼。 –
我已經添加了下面的完整代碼。請幫忙 – Manu