原文链接:http://hi.baidu.com/awz_tiger/item/863cfc10c4bb0f6171d5e8d9

http://blog.163.com/qiuxinke2006@126/blog/static/24885580201131763139536/

http://hi.baidu.com/kilwin/blog/item/f4cfaf2695375920c9955947.html

  用div层代替传统的弹出窗口已经变得很普遍了,因为div层是网页的一部分,不会像传统的弹出窗口那样容易被浏览器拦截。我们常见的弹出div层就是在页面加载后或者点击页面的某个链接时弹出一个div层,同时页面的其他地方会变灰。那么今天我就试着用jquery来实现这个效果。

  通过今天的jquery实例学习,我们要达到这样的效果:点击页面的链接,弹出一个div层,同时页面的其他部分变灰并且不能点击;无论是改变浏览器窗口大小还是下拉滚动条,这个弹出层都能始终保持居中;点击页面的关闭按钮,弹出层消失,页面恢复原样。

  点击查看效果>>>

  这里借鉴之前的一篇文章《基于jQuery的固定飘浮层》,使弹出窗口可以始终固定在浏览器的正中间。在这里有一个要点,就是如何使页面的其他地方在弹出窗口的同时变灰。我使用的方法就是在点击链接弹出div层的时候,给页面增加一个div层,这个层就“负责”使页面变灰。点击关闭后,删除这个层就能使页面恢复原样。不知道有没有更好的方法,有的话请告诉我哦。

  其他应该没什么问题了,还是很简单的,在这里顺便贴上jquery代码:
  

$(function(){var screenwidth,screenheight,mytop,getPosLeft,getPosTopscreenwidth = $(window).width();screenheight = $(window).height();//获取滚动条距顶部的偏移mytop = $(document).scrollTop();//计算弹出层的leftgetPosLeft = screenwidth/2 - 260;//计算弹出层的topgetPosTop = screenheight/2 - 150;//css定位弹出层$("#box").css({"left":getPosLeft,"top":getPosTop});//当浏览器窗口大小改变时...$(window).resize(function(){screenwidth = $(window).width();screenheight = $(window).height();mytop = $(document).scrollTop();getPosLeft = screenwidth/2 - 260;getPosTop = screenheight/2 - 150;$("#box").css({"left":getPosLeft,"top":getPosTop+mytop});});//当拉动滚动条时...$(window).scroll(function(){screenwidth = $(window).width();screenheight = $(window).height();mytop = $(document).scrollTop();getPosLeft = screenwidth/2 - 260;getPosTop = screenheight/2 - 150;$("#box").css({"left":getPosLeft,"top":getPosTop+mytop});});//点击链接弹出窗口$("#popup").click(function(){$("#box").fadeIn("fast");//获取页面文档的高度var docheight = $(document).height();//追加一个层,使背景变灰$("body").append("<div id='greybackground'></div>");$("#greybackground").css({"opacity":"0.5","height":docheight});return false;});//点击关闭按钮$("#closeBtn").click(function() {$("#box").hide();//删除变灰的层$("#greybackground").remove();return false;});});

  源代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>jquery pop up</title>  
<script src="jquery-ui-1.10.3/jquery-1.9.1.js" type="text/javascript"></script><style type="text/css">  * { margin:0; padding:0;  } #wrapper {  height:1000px;  }  #box {  display:none;   position:absolute;  width:520px;  height:300px; border:#f60 solid 2px; z-index:200;  background:#fff;  }  #closeBtn {  position:absolute;  right:10px;  top:10px;  cursor:pointer;  }  #greybackground {  background:#000;  display:block;  z-index:100;  width:100%;  position:absolute;  top:0;  left:0; }  </style>  
</head><body><div id="wrapper">   <a href="http://www.netchina.com.cn" id="popup">点击弹出div窗口</a>   </div><div id="box"><span id="closeBtn">关闭</span></div><script type="text/javascript">
$(function(){  var screenwidth,screenheight,mytop,getPosLeft,getPosTop;screenwidth = $(window).width();  screenheight = $(window).height();  mytop = $(document).scrollTop();  getPosLeft = screenwidth/2 - 260;  getPosTop = screenheight/2 - 150;  $("#box").css({"left":getPosLeft,"top":getPosTop}); $(window).resize(function(){  screenwidth = $(window).width();  screenheight = $(window).height();  mytop = $(document).scrollTop();  getPosLeft = screenwidth/2 - 260;  getPosTop = screenheight/2 - 150;  $("#box").css({"left":getPosLeft,"top":getPosTop+mytop});  });  $(window).scroll(function(){  screenwidth = $(window).width();  screenheight = $(window).height();  mytop = $(document).scrollTop();  getPosLeft = screenwidth/2 - 260;  getPosTop = screenheight/2 - 150;  $("#box").css({"left":getPosLeft,"top":getPosTop+mytop});  });  $("#popup").click(function(){  $("#box").fadeIn("fast");  $("body").append("<div id='greybackground'></div>");  var documentheight = $(document).height();  $("#greybackground").css({"opacity":"0.5","height":documentheight});  return false;  });  $("#closeBtn").click(function() { $("#box").hide();  $("#greybackground").remove();  return false;  });  });  </script></body>
</html>

  

JavaScript实现弹出窗口实质上就是在浏览器上画了一个方形区域,并在开始时将其隐藏,只是到某个JavaScript事件时才通过修改css的属性值来将其显示出来。

其大致步骤为:

创建一个装载弹出窗口的div

<html>
<head>
<title>jQuery实例1:浮动窗口</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
#win{
/*边框*/
border:1px red solid;
/*窗口的高度和宽度*/
width : 300px;
height: 200px;
/*窗口的位置*/
position : absolute;
top : 100px;
left: 350px;
/*开始时窗口不可见*/
display : none;
}
/*控制背景色的样式*/
#title{
background-color : blue;
color : red;
/*控制标题栏的左内边距*/
padding-left: 3px;
}
#cotent{
padding-left : 3px;
padding-top :  5px;
}
/*控制关闭按钮的位置*/
#close{
margin-left: 188px;
/*当鼠标移动到X上时,出现小手的效果*/
cursor: pointer;
}
#win{
/*边框*/
border:1px red solid;
/*窗口的高度和宽度*/
width : 300px;
height: 200px;
/*窗口的位置*/
position : absolute;
top : 100px;
left: 350px;
/*开始时窗口不可见*/
display : none;
}
/*控制背景色的样式*/
#title{
background-color : blue;
color : red;
/*控制标题栏的左内边距*/
padding-left: 3px;
}
#cotent{
padding-left : 3px;
padding-top :  5px;
}
/*控制关闭按钮的位置*/
#close{
margin-left: 188px;
/*当鼠标移动到X上时,出现小手的效果*/
cursor: pointer;
}
</style>
<script type="text/javascript" src="jquery-ui-1.10.3/jquery-1.9.1.js"></script>
<script type="text/javascript">
function showWin(){
/*找到div节点并返回*/
var winNode = $("#win");
//方法一:利用js修改css的值,实现显示效果
// winNode.css("display", "block");
//方法二:利用jquery的show方法,实现显示效果
// winNode.show("slow");
//方法三:利用jquery的fadeIn方法实现淡入
winNode.fadeIn("slow");
}
function hide(){
var winNode = $("#win");
//方法一:修改css的值
//winNode.css("display", "none");
//方法二:jquery的fadeOut方式
winNode.fadeOut("slow");
//方法三:jquery的hide方法
winNode.hide("slow");
}
</script>
</head>
<body>
</body>
<a onClick="showWin()" href="#" mce_href="#">弹出窗口</a>
<div id="win">
<div id="title">我是标题栏!<span id="close" onClick="hide()">X</span></div>
<div id="content">我是一个窗口!</div>
</div>
</html>

用jQuery实现弹出窗口/弹出div层相关推荐

  1. html 弹出层 边框半透明,js+CSS实现弹出居中背景半透明div层的方法

    本文实例讲述了js+CSS实现弹出居中背景半透明div层的方法.分享给大家供大家参考.具体实现方法如下: js+CSS弹出居中的背景半透明div层 body{margin:0px;} #bg{widt ...

  2. php的弹出窗口,弹出窗口 PHP

    弹出窗口function PopupCenterWindow(url, name, width, height, scroll) { var win = null; var left = (scree ...

  3. css 弹出窗口之后的笼罩层

    1这个为阴影的底层 background: rgba(0,0,0,0.4);position: absolute;top: 0;left: 0;bottom: 0;right: 0;z-index: ...

  4. js实现弹出窗口的拖拽功能

    弹出窗口的拖拽 拖拽功能运用到的有 onmousedown 事件,onmousemove 事件以及 onmouseup 事件 弹出窗口基本步骤: 1.点击点击弹出窗口按钮弹出窗口: 2.窗口弹出后,鼠 ...

  5. jQuery弹出窗口浏览图片

    效果预览:http://keleyi.com/keleyi/phtml/jqtexiao/3.htm HTML文件代码: 1 <!DOCTYPE HTML> 2 <html> ...

  6. jquery效果 窗口弹出案例

    效果 ①基本效果:show().hide().toggle() ②滑动 slideDown().slideUp().slideToggle() 划上:$("p").slideUp( ...

  7. html5鼠标点击弹出层,jQuery实现单击弹出Div层窗口效果(可关闭可拖动)

    本文实例讲述了jQuery实现单击弹出Div层窗口效果.分享给大家供大家参考.具体如下: 这是一款jquery实现的可拖动可关闭的弹出框效果,网上已经有很多类似效果了,网页上实现这种效果其实并不难,现 ...

  8. jquery 弹出窗口_jQuery弹出窗口和工具提示窗口动画效果

    jquery 弹出窗口 In this tutorial, we are going to discuss about a jQuery plugin for responsive and acces ...

  9. jQuery弹出窗口完整代码

    效果体验:http://keleyi.com/keleyi/phtml/jqtexiao/1.htm 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

最新文章

  1. IOS开发中发送Email的两种方法
  2. Java8新特性系列(Lambda)
  3. 国产Dhyana禅定x86处理器开始启动生产
  4. 计算机应用虚拟仿真实验答案,虚拟仿真 实验教学+.ppt
  5. ThinkPHP函数详解:C方法
  6. android 获取sd卡目录失败_树莓派对SD卡的大小,速度有哪些要求?
  7. 用例设计工具PICT — 输入组合覆盖
  8. 如何使用MySQL进行备份?
  9. bootstrap你让前端小狮子们又喜又恨
  10. python数据类型二(列表和元组)
  11. linux的for循环乘积,最大乘积连续子串 - Triangle23 - OSCHINA - 中文开源技术交流社区...
  12. 即时聊天:前端react+极光IM
  13. 【运筹学】线性规划 单纯形法原理 ( 构造初始可行基 | 基变换 | 最优性检验 | 解的判别 | 检验数 | ( 唯一 / 无穷多 ) 最优解判别定理 | 无界解判别定理 )
  14. peoplesoft笔记
  15. SOLIDWORKS如何实现放样折弯
  16. 解除Word的编辑保护【简单版】
  17. 广告电商系统开发功能只订单处理
  18. Hashtable、HashMap 与 HashTable区别、HashMap、Hashtable和TreeMap、 LinkedHashMap
  19. FPGA学习笔记(十二)IP核之FIFO的学习总结
  20. UPP映象(A C++ GUI lib)

热门文章

  1. oracle追加index,oracle add index
  2. sqlserver55555_sqlserver把小数点后面多余的0去掉
  3. html怎么设置闪烁字,HTML最简单的文字闪烁代码
  4. 电脑知识:如何保养自己的电脑,看完你就懂了!
  5. 写给新入行程序员的10条建议
  6. 电脑计算机网络由基础到深入常用知识集锦!
  7. 程序员幽默:一整天都在修复 bug 是啥感觉?
  8. 收集18个高大上的浏览器小技巧
  9. JS获取请求URL相关参数
  10. MongoDB高可用集群搭建