jQuery實(shí)現(xiàn)彈出窗口彈出div層的實(shí)例代碼
通過今天的jquery實(shí)例學(xué)習(xí),我們要達(dá)到這樣的效果:點(diǎn)擊頁面的鏈接,彈出一個(gè)div層,同時(shí)頁面的其他部分變灰并且不能點(diǎn)擊;無論是改變?yōu)g覽器窗口大小還是下拉滾動(dòng)條,這個(gè)彈出層都能始終保持居中;點(diǎn)擊頁面的關(guān)閉按鈕,彈出層消失,頁面恢復(fù)原樣。
這里借鑒之前的一篇文章《基于jQuery的固定飄浮層》,使彈出窗口可以始終固定在瀏覽器的正中間。在這里有一個(gè)要點(diǎn),就是如何使頁面的其他地方在彈出窗口的同時(shí)變灰。我使用的方法就是在點(diǎn)擊鏈接彈出div層的時(shí)候,給頁面增加一個(gè)div層,這個(gè)層就“負(fù)責(zé)”使頁面變灰。點(diǎn)擊關(guān)閉后,刪除這個(gè)層就能使頁面恢復(fù)原樣。不知道有沒有更好的方法,有的話請(qǐng)告訴我哦。
其他應(yīng)該沒什么問題了,還是很簡單的,在這里順便貼上jQuery代碼:
$(function(){ var screenwidth,screenheight,mytop,getPosLeft,getPosTop screenwidth = $(window).width(); screenheight = $(window).height(); //獲取滾動(dòng)條距頂部的偏移 mytop = $(document).scrollTop(); //計(jì)算彈出層的left getPosLeft = screenwidth/2 - 260; //計(jì)算彈出層的top getPosTop = screenheight/2 - 150; //css定位彈出層 $("#box").css({"left":getPosLeft,"top":getPosTop}); //當(dāng)瀏覽器窗口大小改變時(shí)... $(window).resize(function(){ <span style="white-space:pre"> </span>screenwidth = $(window).width(); <span style="white-space:pre"> </span>screenheight = $(window).height(); <span style="white-space:pre"> </span>mytop = $(document).scrollTop(); <span style="white-space:pre"> </span>getPosLeft = screenwidth/2 - 260; <span style="white-space:pre"> </span>getPosTop = screenheight/2 - 150; <span style="white-space:pre"> </span>$("#box").css({"left":getPosLeft,"top":getPosTop+mytop}); }); //當(dāng)拉動(dòng)滾動(dòng)條時(shí)... $(window).scroll(function(){ <span style="white-space:pre"> </span>screenwidth = $(window).width(); <span style="white-space:pre"> </span>screenheight = $(window).height(); <span style="white-space:pre"> </span>mytop = $(document).scrollTop(); <span style="white-space:pre"> </span>getPosLeft = screenwidth/2 - 260; <span style="white-space:pre"> </span>getPosTop = screenheight/2 - 150; <span style="white-space:pre"> </span>$("#box").css({"left":getPosLeft,"top":getPosTop+mytop}); }); //點(diǎn)擊鏈接彈出窗口 $("#popup").click(function(){ <span style="white-space:pre"> </span>$("#box").fadeIn("fast"); <span style="white-space:pre"> </span>//獲取頁面文檔的高度 <span style="white-space:pre"> </span>var docheight = $(document).height(); <span style="white-space:pre"> </span>//追加一個(gè)層,使背景變灰 <span style="white-space:pre"> </span>$("body").append("<div id='greybackground'></div>"); <span style="white-space:pre"> </span>$("#greybackground").css({"opacity":"0.5","height":docheight}); <span style="white-space:pre"> </span>return false; }); //點(diǎn)擊關(guān)閉按鈕 $("#closeBtn").click(function() { <span style="white-space:pre"> </span>$("#box").hide(); <span style="white-space:pre"> </span>//刪除變灰的層 <span style="white-space:pre"> </span>$("#greybackground").remove(); <span style="white-space:pre"> </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">點(diǎn)擊彈出div窗口</a> </div> <div id="box"> <span style="white-space:pre"> </span><span id="closeBtn">關(guān)閉</span> </div> </body> </html>
以上所述是小編給大家介紹的jQuery實(shí)現(xiàn)彈出窗口彈出div層的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- jQuery彈出窗口打開鏈接的實(shí)現(xiàn)代碼
- jQuery實(shí)現(xiàn)的模擬彈出窗口功能示例
- jQuery實(shí)現(xiàn)選中彈出窗口選擇框內(nèi)容后賦值給文本框的方法
- jQuery實(shí)現(xiàn)彈出窗口中切換登錄與注冊(cè)表單
- jquery實(shí)現(xiàn)彈出窗口效果的實(shí)例代碼
- jQuery彈出窗口完整代碼(居中,居左,居右)
- Jquery彈出窗口插件 LeanModal的使用方法
- AeroWindow 基于JQuery的彈出窗口插件
- jQuery+jqmodal彈出窗口實(shí)現(xiàn)代碼分明
- jQuery彈出窗口簡單實(shí)現(xiàn)代碼
相關(guān)文章
基于jQuery的左右滾動(dòng)實(shí)現(xiàn)代碼
jQuery左右滾動(dòng),jquery中有自定義動(dòng)畫。要實(shí)現(xiàn)左右滾動(dòng)就是將Html標(biāo)簽的left值進(jìn)行加減。2010-12-12基于jQuery全屏焦點(diǎn)圖左右切換插件responsiveslides
這篇文章主要介紹了基于jQuery全屏焦點(diǎn)圖左右切換插件responsiveslides的相關(guān)資料,需要的朋友可以參考下2015-09-09使用jquery動(dòng)態(tài)加載Js文件和Css文件
這篇文章主要為大家詳細(xì)介紹了使用jquery動(dòng)態(tài)加載Js文件和Css文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-10-10jquery+ajax實(shí)現(xiàn)異步上傳文件顯示進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了jquery+ajax實(shí)現(xiàn)異步上傳文件顯示進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08js與jquery中獲取當(dāng)前鼠標(biāo)的x、y坐標(biāo)位置的代碼
jquery中獲取當(dāng)前鼠標(biāo)的x、y位置位置的代碼,需要的朋友可以參考下。2011-05-05Jquery+Ajax+PHP+MySQL實(shí)現(xiàn)分類列表管理(下)
本文將采用Jquery+Ajax+PHP+MySQL來實(shí)現(xiàn)一個(gè)客戶分類列表的管理,如何利用Ajax和Json技術(shù)讓用戶操作起來覺得更輕松,感興趣的小伙伴們可以參考一下2015-10-10JQuery Tab選項(xiàng)卡效果代碼改進(jìn)版
JQuery制作的選項(xiàng)卡改進(jìn)版,已經(jīng)有人將這個(gè)效果改進(jìn)下封裝成一個(gè)完整的方法。2010-04-04jQuery 點(diǎn)擊圖片跳轉(zhuǎn)上一張或下一張功能的實(shí)現(xiàn)代碼
jQuery獲取當(dāng)前鼠標(biāo)相對(duì)位置坐標(biāo)和點(diǎn)擊圖片跳轉(zhuǎn)上一張或下一張功能2010-03-03