用div层代替传统的弹出窗口已经变得很普遍了,因为div层是网页的一部分,不会像传统的弹出窗口那样容易被浏览器拦截。我们常见的弹出div层就是在页面加载后或者点击页面的某个链接时弹出一个div层,同时页面的其他地方会变灰。那么今天我就试着用jquery来实现这个效果。
  通过今天的jquery实例学习,我们要达到这样的效果:点击页面的链接,弹出一个div层,同时页面的其他部分变灰并且不能点击;无论是改变浏览器窗口大小还是下拉滚动条,这个弹出层都能始终保持居中;点击页面的关闭按钮,弹出层消失,页面恢复原样。
  点击查看效果>>>
  这里借鉴之前的一篇文章《基于jQuery的固定飘浮层》,使弹出窗口可以始终固定在浏览器的正中间。在这里有一个要点,就是如何使页面的其他地方在弹出窗口的同时变灰。我使用的方法就是在点击链接弹出div层的时候,给页面增加一个div层,这个层就“负责”使页面变灰。点击关闭后,删除这个层就能使页面恢复原样。不知道有没有更好的方法,有的话请告诉我哦。
  其他应该没什么问题了,还是很简单的,在这里顺便贴上jquery代码:
  $(function(){
  var screenwidth,screenheight,mytop,getPosLeft,getPosTop
  screenwidth = $(window).width();
  screenheight = $(window).height();
  //获取滚动条距顶部的偏移
  mytop = $(document).scrollTop();
  //计算弹出层的left
  getPosLeft = screenwidth/2 - 260;
  //计算弹出层的top
  getPosTop = 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" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>jquery pop up</title>
  <script src=blog.soso/qz.q/"jquery.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=blog.soso/qz.q/"#" 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});jquery是什么有什么作用
  $(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>