这篇文章主要介绍了jquery实现点击空白处隐藏元素p等方法和实现代码的讲解,一起学习下。
我们在网站开发的时候经常会有点击空白处去隐藏已有的元素,p或者其他,本次教给大家如何用jquery实现这个特效的方法。
一款jQuery点击空白处隐藏弹出层网页特效,点击按钮弹出层、点击页面空白处弹出层消失JS代码。主要功能是点击按钮弹出层显示,然后通过点击页面任意位置都能关闭弹出层显示效果,主要是$(document).click的操作应用。演示 Demo,效果源码:
CSS代码:
* {
padding: 0;
margin: 0;
}
#btnShow{
margin: 100px auto 0;
width: 90px;
display: block;
}
#pTop{
border: 2px solid #666666;
position: absolute; display: none;
width: 400px;
height: 200px;
color: #333;
background: #efefef;
padding-top: 10px;
text-align: center;
font: 16px/30px "微软雅黑";
margin-top: -105px;
margin-left: -200px;
left: 50%;
top: 50%;
}
JS代码:
$(function () {
$('#btnShow').click(function (event) {
//取消事件冒泡
event.stopPropagation();
//按钮的toggle,如果p是可见的,点击按钮切换为隐藏的;如果是隐藏的,切换为可见的。
$('#pTop').toggle('slow');
return false;
});
//点击空白处隐藏弹出层,下面为滑动消失效果和淡出消失效果。
$(document).click(function(event){
var _con = $('#pTop'); // 设置目标区域
if(!_con.is(event.target) && _con.has(event.target).length === 0){ // Mark 1
//$('#pTop').slideUp('slow'); //滑动消失
$('#pTop').hide(1000); //淡出消失
}
});
})
HTML代码:
<input><p> 点击空白区域弹出层关闭! </p>
经过测试,在移动端Iphone手机上点击页面空白处弹出层关闭失效,不支持document写法,解决方案:可以添加一个背景层作为页面空白对象处理。
点击空白处隐藏弹出层案例二:
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<title>点击空白处关闭弹出层</title><meta><style>
#box{width:300px;height:200px;border:1px solid #000;display:none;}
.btn{color:red;}
</style><script></script><script>
$(function(){
$(".btn").click(function(event){
var e=window.event || event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
$("#box").show();
});
$("#box").click(function(event){
var e=window.event || event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
});
document.onclick = function(){
$("#box").hide();
};
})
</script><p></p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/gongju/2331" title="jQuery 1.12.4"><img
src="https://img.php.cn/upload/manual/001/431/639/6a21619eac924328.png" alt="jQuery 1.12.4" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/gongju/2331" title="jQuery 1.12.4" class="overflowclass">jQuery 1.12.4</a>
<p class="overflowclass">jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。</p>
</div>
<a rel="nofollow" href="/xiazai/gongju/2331" title="jQuery 1.12.4" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<span>点击此处打开弹出层</span><br>点击空白处关闭弹出层
在给大家分享一下js实现的点击p区域外隐藏p区域的方法代码:
html>
<meta><script></script><title></title><style>
body
{
background-color:#999999;
}
#myp
{
background-color:#FFFFFF;
width:250px;
height:250px;
display:none;
}
</style><input>
<p>
This is a p;
</p>
<script>
var myp = $("#myp");
$(function ()
{
$("#btn").click(function (event)
{
showp();//调用显示p方法
$(document).one("click", function ()
{//对document绑定一个影藏p方法
$(myp).hide();
});
event.stopPropagation();//阻止事件向上冒泡
});
$(myp).click(function (event)
{
event.stopPropagation();//阻止事件向上冒泡
});
});
function showp()
{
$(myp).fadeIn();
}
</script>
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:









