遮罩層點(diǎn)擊按鈕彈出并且具有拖動(dòng)和關(guān)閉效果(兩種方法)
首先給大家展示演示效果:
基于JavaScript的網(wǎng)頁彈出層,鼠標(biāo)按在彈出層的標(biāo)題欄處,可以拖動(dòng)該浮動(dòng)層隨意移動(dòng)位置,不需要時(shí)也可以關(guān)閉,操作體驗(yàn)舒服,兼容性好,IE/火狐等眾多瀏覽器下運(yùn)行穩(wěn)定、反應(yīng)快速。代碼表現(xiàn)方面,簡(jiǎn)潔務(wù)實(shí),不玩虛的,拿去學(xué)習(xí)也相當(dāng)不錯(cuò)。
js代碼
示例一:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>彈出層并可拖拽</title> <style> html,body{height:100%;overflow:hidden;} body,div,h2{margin:0;padding:0;} body{font:12px/1.5 Tahoma;} li{ list-style-type:none} center{padding-top:10px;} button{cursor:pointer;} #overlay{position:absolute;top:0;left:0;width:100%;height:100%;background:#000;opacity:0.5;filter:alpha(opacity=50);display:none;} #win{position:absolute;top:50%;left:50%;width:400px;height:200px;background:#fff;border:4px solid #f90;margin:-102px 0 0 -202px;display:none;} h2{font-size:12px;height:18px;text-align:right;background:#FC0;border-bottom:3px solid #f90;padding:5px;cursor:move;} h2 span{color:#f90;cursor:pointer;background:#fff;border:1px solid #f90;padding:0 2px;} </style> <script> window.onload = function () { var oWin = document.getElementById("win"); var oLay = document.getElementById("overlay"); var oBtn = document.getElementsByTagName("button")[0]; var oClose = document.getElementById("close"); var oH2 = oWin.getElementsByTagName("h2")[0]; var bDrag = false; var disX = disY = 0; oBtn.onclick = function () { oLay.style.display = "block"; oWin.style.display = "block" }; oClose.onclick = function () { oLay.style.display = "none"; oWin.style.display = "none" }; oClose.onmousedown = function (event) { (event || window.event).cancelBubble = true; }; oH2.onmousedown = function (event) { var event = event || window.event; bDrag = true; disX = event.clientX - oWin.offsetLeft; disY = event.clientY - oWin.offsetTop; this.setCapture && this.setCapture(); return false }; document.onmousemove = function (event) { if (!bDrag) return; var event = event || window.event; var iL = event.clientX - disX; var iT = event.clientY - disY; var maxL = document.documentElement.clientWidth - oWin.offsetWidth; var maxT = document.documentElement.clientHeight - oWin.offsetHeight; iL = iL < 0 ? 0 : iL; iL = iL > maxL ? maxL : iL; iT = iT < 0 ? 0 : iT; iT = iT > maxT ? maxT : iT; oWin.style.marginTop = oWin.style.marginLeft = 0; oWin.style.left = iL + "px"; oWin.style.top = iT + "px"; return false }; document.onmouseup = window.onblur = oH2.onlosecapture = function () { bDrag = false; oH2.releaseCapture && oH2.releaseCapture(); }; }; </script> <meta charset="utf-8"> </head> <body> <div id="overlay"></div> <div id="win"><h2><span id="close">×</span></h2> <li><a target='_blank'>http://www.100sucai.com</a></li> <li><a title="CSS3和Html5">CSS3和Html5</a></li> <li><a title="圖表與圖形">圖表與圖形</a></li> </div> <center><button>彈出層</button></center> </body> </html><br />
jq代碼:
代碼示例二:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>jQuery彈出層效果</title> <meta content="網(wǎng)頁特效,特效代碼,jQuery,css特效,Js代碼,廣告幻燈,圖片切換" name="keywords" /> <meta content="jQuery彈出層效果,有關(guān)閉按鈕,代碼簡(jiǎn)單易懂,你可以隨意修改彈出層的參數(shù)。" name="description" /> <script src="/uploads/common/js/jquery-1.4.2.min.js" type="text/javascript"></script> <style> .black_overlay{ display: none; position: absolute; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.8; opacity:.80; filter: alpha(opacity=80); } .white_content { display: none; position: absolute; top: 10%; left: 10%; width: 80%; height: 80%; border: 16px solid lightblue; background-color: white; z-index:1002; overflow: auto; } .white_content_small { display: none; position: absolute; top: 20%; left: 30%; width: 40%; height: 50%; border: 16px solid lightblue; background-color: white; z-index:1002; overflow: auto; } </style> <script type="text/javascript"> //彈出隱藏層 function ShowDiv(show_div,bg_div){ document.getElementById(show_div).style.display='block'; document.getElementById(bg_div).style.display='block' ; var bgdiv = document.getElementById(bg_div); bgdiv.style.width = document.body.scrollWidth; // bgdiv.style.height = $(document).height(); $("#"+bg_div).height($(document).height()); }; //關(guān)閉彈出層 function CloseDiv(show_div,bg_div) { document.getElementById(show_div).style.display='none'; document.getElementById(bg_div).style.display='none'; }; </script> </head> <body> <input id="Button1" type="button" value="點(diǎn)擊彈出層" onclick="ShowDiv('MyDiv','fade')" /> <!--彈出層時(shí)背景層DIV--> <div id="fade" class="black_overlay"> </div> <div id="MyDiv" class="white_content"> <div style="text-align: right; cursor: default; height: 40px;"> <span style="font-size: 16px;" onclick="CloseDiv('MyDiv','fade')">關(guān)閉</span> </div> 目前來說,我還是喜歡這個(gè)自己改造的彈出層。自己在項(xiàng)目中也用的是這個(gè)。 </div> </body> </html>
以上通過jq和js分別實(shí)現(xiàn)了遮罩層點(diǎn)擊按鈕彈出并且具有拖動(dòng)和關(guān)閉效果,希望對(duì)大家有所幫助。
- 用html5 js實(shí)現(xiàn)點(diǎn)擊一個(gè)按鈕達(dá)到瀏覽器全屏效果
- js實(shí)現(xiàn)點(diǎn)擊左右按鈕輪播圖片效果實(shí)例
- 回車直接實(shí)現(xiàn)點(diǎn)擊某按鈕的效果即觸發(fā)單擊事件
- C#鍵盤輸入回車鍵實(shí)現(xiàn)點(diǎn)擊按鈕效果的方法
- 原生js實(shí)現(xiàn)淘寶首頁點(diǎn)擊按鈕緩慢回到頂部效果
- 基于jquery實(shí)現(xiàn)左右按鈕點(diǎn)擊的圖片切換效果
- javascript點(diǎn)擊按鈕實(shí)現(xiàn)隱藏顯示切換效果
- js點(diǎn)擊按鈕實(shí)現(xiàn)帶遮罩層的彈出視頻效果
- 圖片輪換效果實(shí)現(xiàn)代碼(點(diǎn)擊按鈕停止執(zhí)行)
- js點(diǎn)擊按鈕實(shí)現(xiàn)水波紋效果代碼(CSS3和Canves)
相關(guān)文章
jQuery ajax全局函數(shù)處理session過期后的ajax跳轉(zhuǎn)問題
這篇文章主要介紹了基于jQuery的全局ajax函數(shù)處理session過期后的ajax操作的相關(guān)資料,需要的朋友可以參考下2016-06-06jQuery實(shí)現(xiàn)仿美橙互聯(lián)兩級(jí)導(dǎo)航菜單的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)仿美橙互聯(lián)兩級(jí)導(dǎo)航菜單的方法,實(shí)例分析了jQuery操作css及setTimeout等實(shí)現(xiàn)導(dǎo)航菜單的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03jquery簡(jiǎn)單倒計(jì)時(shí)實(shí)現(xiàn)方法
這篇文章主要介紹了jquery簡(jiǎn)單倒計(jì)時(shí)實(shí)現(xiàn)方法,涉及jQuery定時(shí)函數(shù)操作及日期與實(shí)現(xiàn)的運(yùn)算技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12Easyui筆記2:實(shí)現(xiàn)datagrid多行刪除的示例代碼
本篇文章主要介紹了Easyui筆記2:實(shí)現(xiàn)datagrid多行刪除,詳細(xì)介紹了完成一個(gè)多行勾選并刪除的功能。有興趣的可以了解一下。2017-01-01jQuery自動(dòng)完成插件completer附源碼下載
這篇文章主要介紹了jQuery自動(dòng)完成插件completer的相關(guān)資料,需要的朋友可以參考下2016-01-01jQuery UI工具提示框部件Tooltip Widget
這篇文章介紹了jQuery UI工具提示框部件Tooltip Widget,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06jquery ready函數(shù)、css函數(shù)及text()使用示例
想必大家對(duì)jquery的ready函數(shù)、css函數(shù)、text()并不陌生吧,其實(shí)很好理解的,接下來有個(gè)不錯(cuò)的示例,如果你對(duì)此理解還是很模糊可以參考下2013-09-09