这篇文章主要介绍了jQuery怎么实现弹出窗口弹出div层的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇jQuery怎么实现弹出窗口弹出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(){ <span > </span>screenwidth = $(window).width(); <span > </span>screenheight = $(window).height(); <span > </span>mytop = $(document).scrollTop(); <span > </span>getPosLeft = screenwidth/2 - 260; <span > </span>getPosTop = screenheight/2 - 150; <span > </span>$("#box").css({"left":getPosLeft,"top":getPosTop+mytop}); }); //当拉动滚动条时... $(window).scroll(function(){ <span > </span>screenwidth = $(window).width(); <span > </span>screenheight = $(window).height(); <span > </span>mytop = $(document).scrollTop(); <span > </span>getPosLeft = screenwidth/2 - 260; <span > </span>getPosTop = screenheight/2 - 150; <span > </span>$("#box").css({"left":getPosLeft,"top":getPosTop+mytop}); }); //点击链接弹出窗口 $("#popup").click(function(){ <span > </span>$("#box").fadeIn("fast"); <span > </span>//获取页面文档的高度 <span > </span>var docheight = $(document).height(); <span > </span>//追加一个层,使背景变灰 <span > </span>$("body").append("<div id='greybackground'></div>"); <span > </span>$("#greybackground").css({"opacity":"0.5","height":docheight}); <span > </span>return false; }); //点击关闭按钮 $("#closeBtn").click(function() { <span > </span>$("#box").hide(); <span > </span>//删除变灰的层 <span > </span>$("#greybackground").remove(); <span > </span>return false; }); });
html代码:
<!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=http://blog.soso.com/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=http://blog.soso.com/qz.q/"#" id="popup">点击弹出div窗口</a> </div> <div id="box"> <span > </span><span id="closeBtn">关闭</span> </div> </body> </html>
关于“jQuery怎么实现弹出窗口弹出div层”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“jQuery怎么实现弹出窗口弹出div层”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。