jQuery彈出窗口簡(jiǎn)單實(shí)現(xiàn)代碼
今天講了Jquery的彈出窗口的組成和用法:
先把引用文件的代碼寫好:
// 每個(gè)彈窗的標(biāo)識(shí) var x =0; var idzt = new Array(); var Window = function(config){ //ID不重復(fù) idzt[x] = "zhuti"+x; //彈窗ID //初始化,接收參數(shù) this.config = { width : config.width || 300, //寬度 height : config.height || 200, //高度 buttons : config.buttons || '', //默認(rèn)無(wú)按鈕 title : config.title || '標(biāo)題', //標(biāo)題 content : config.content || '內(nèi)容', //內(nèi)容 isMask : config.isMask == false?false:config.isMask || true, //是否遮罩 isDrag : config.isDrag == false?false:config.isDrag || true, //是否移動(dòng) }; //加載彈出窗口 var w = ($(window).width()-this.config.width)/2; var h = ($(window).height()-this.config.height)/2; var nr = "<div class='zhuti' id='"+idzt[x]+"' bs='"+x+"' style='width:"+this.config.width+"px; height:"+this.config.height+"px; background-color:white; left:"+w+"px; top:"+h+"px;'></div>"; $("body").append(nr); //加載彈窗標(biāo)題 var content ="<div id='title"+x+"' class='title' bs='"+x+"'>"+this.config.title+"<div id='close"+x+"' class='close' bs='"+x+"'>×</div></div>"; //加載彈窗內(nèi)容 var nrh = this.config.height - 75; content = content+"<div id='content"+x+"' bs='"+x+"' class='content' style='width:100%; height:"+nrh+"px;'>"+this.config.content+"</div>"; //加載按鈕 content = content+"<div id='btnx"+x+"' bs='"+x+"' class='btnx'>"+this.config.buttons+"</div>"; //將標(biāo)題、內(nèi)容及按鈕添加進(jìn)窗口 $('#'+idzt[x]).html(content); //創(chuàng)建遮罩層 if(this.config.isMask) { var zz = "<div id='zz'></div>"; $("body").append(zz); $("#zz").css('display','block'); } //最大最小限制,以免移動(dòng)到頁(yè)面外 var maxX = $(window).width()-this.config.width; var maxY = $(window).height()-this.config.height; var minX = 0, minY = 0; //窗口移動(dòng) if(this.config.isDrag) { //鼠標(biāo)移動(dòng)彈出窗 $(".title").bind("mousedown",function(e){ var n = $(this).attr("bs"); //取標(biāo)識(shí) //使選中的到最上層 $(".zhuti").css("z-index",3); $('#'+idzt[n]).css("z-index",4); //取初始坐標(biāo) var endX = 0, //移動(dòng)后X坐標(biāo) endY = 0, //移動(dòng)后Y坐標(biāo) startX = parseInt($('#'+idzt[n]).css("left")), //彈出層的初始X坐標(biāo) startY = parseInt($('#'+idzt[n]).css("top")), //彈出層的初始Y坐標(biāo) downX = e.clientX, //鼠標(biāo)按下時(shí),鼠標(biāo)的X坐標(biāo) downY = e.clientY; //鼠標(biāo)按下時(shí),鼠標(biāo)的Y坐標(biāo) //綁定鼠標(biāo)移動(dòng)事件 $("body").bind("mousemove",function(es){ endX = es.clientX - downX + startX; //X坐標(biāo)移動(dòng) endY = es.clientY - downY + startY; //Y坐標(biāo)移動(dòng) //最大最小限制 if(endX > maxX) { endX = maxX; } else if(endX < 0) { endX = 0; } if(endY > maxY) { endY = maxY; } else if(endY < 0) { endY = 0; } $('#'+idzt[n]).css("top",endY+"px"); $('#'+idzt[n]).css("left",endX+"px"); window.getSelection ? window.getSelection().removeAllRanges():document.selection.empty(); //取消選中文本 }); }); //鼠標(biāo)按鍵抬起,釋放移動(dòng)事件 $("body").bind("mouseup",function(){ $("body").unbind("mousemove"); }); } //關(guān)閉窗口 $(".close").click(function(){ var m = this.getAttribute("bs"); //找標(biāo)識(shí) $('#'+idzt[m]).remove(); //移除彈窗 $('#zz').remove(); //移除遮罩 }) x++; //標(biāo)識(shí)增加 }
這個(gè)JS文件把彈出窗口的內(nèi)容,樣式,位置,按鈕,以及遮罩層都做了處理,在引用前好好看看里面的代碼,最好都能弄懂,里面也做了詳細(xì)的注釋,希望可以幫的你。
下面是CSS樣式表:
.zhuti { position:absolute; z-index:3; font-size:14px; border-radius:5px; box-shadow:0 0 5px white; overflow:hidden; color:#333; } .title { background-color:#3498db; vertical-align:middle; height:35px; width:100%; line-height:35px; text-indent:1em; } .close{ float:right; width:35px; height:35px; font-weight:bold; line-height:35px; vertical-align:middle; color:white; font-size:18px; } .close:hover { cursor:pointer; } .content { text-indent:1em; padding-top:10px; } .btnx { height:30px; width:100%; text-indent:1em; } .btn { height:28px; width:80px; float:left; margin-left:20px; color:#333; } #zz { width:100%; height:100%; opacity:0.15; display:none; background-color:#ccc; z-index:2; position:absolute; top:0px; left:0px; }
這個(gè)樣式表把每個(gè)標(biāo)簽和所需要的樣式都寫好了,這樣就能節(jié)省主要頁(yè)面的代碼量,并且讓主頁(yè)面看起來(lái)非常的整齊,如果要改,只需要在CSS樣式表中修改即可,注意:不管要引用什么文件,必須把Jquery文件放在最前面?。?!
下面是主頁(yè)面代碼:
<!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>無(wú)標(biāo)題文檔</title> <script type="text/javascript" src="jquery-1.11.2.min.js"> </script> <script type="text/javascript" src="tanchuang.js"> </script> <link href="tanchuang.css" rel="external nofollow" rel="stylesheet" type="text/css" /> <style type="text/css"> *{ margin: 0px auto; } </style> </head> <body style="background-color:#999"> <div style="width:200px; margin-top:10px"> <input type="button" value="彈出窗口" id="btntc" style="width:100px; height:30px; font-size:18px;" /> </div> </body> <script type="text/javascript"> $(document).ready(function(e) { $('#btntc').click(function(){ var html = "<div style='color:red'>這是測(cè)試的彈窗</div>"; var button ="<input type='button' value='確定' /><input type='button' value='取消' />"; var win = new Window({ width : 400, //寬度 height : 300, //高度 title : '測(cè)試彈窗', //標(biāo)題 content : html, //內(nèi)容 isMask : false, //是否遮罩 buttons : button, //按鈕 isDrag:true, //是否移動(dòng) }); }) }); </script> </html>
同樣的,主頁(yè)面里面也加了詳細(xì)的注釋,這樣便于日后的理解,希望可以幫的自己和大家。讓我們看看效果吧:
點(diǎn)擊彈出窗口之后的效果:
我們可以看到每個(gè)彈出窗口都可以移動(dòng),并且可以彈出無(wú)數(shù)個(gè)窗口,如果把遮罩層改成true,那樣就不能再出現(xiàn)第二個(gè)彈出窗口了。
一定要記住遮罩層的實(shí)用,這樣可以避免很多BUG如果要引用彈出窗口一定要測(cè)試好了再使用,以防出現(xiàn)問(wèn)題,切記!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery實(shí)現(xiàn)彈出窗口彈出div層的實(shí)例代碼
- jQuery彈出窗口打開(kāi)鏈接的實(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)代碼分明
相關(guān)文章
jquery 綁定回車動(dòng)作撲捉回車鍵觸發(fā)的事件
這篇文章主要介紹了jquery如何撲捉回車鍵觸發(fā)的事件,需要的朋友可以參考下2014-03-03jQuery的實(shí)現(xiàn)原理的模擬代碼 -4 重要的擴(kuò)展函數(shù) extend
在上兩篇文章中,我們看到每次要通過(guò) jQuery 的原型增加共享方法的時(shí)候,都需要通過(guò) jQuery.fn 一個(gè)個(gè)進(jìn)行擴(kuò)展,非常麻煩.2010-08-08Jquery1.9.1源碼分析系列(六)延時(shí)對(duì)象應(yīng)用之jQuery.ready
這篇文章主要介紹了Jquery1.9.1源碼分析系列(六)延時(shí)對(duì)象應(yīng)用之jQuery.ready的相關(guān)資料,需要的朋友可以參考下2015-11-11jQuery仿Excel表格編輯功能的實(shí)現(xiàn)代碼
Handsontable 是一個(gè)相當(dāng)給力的 jQuery 插件,它實(shí)現(xiàn)了 HTML 頁(yè)面中的表格編輯功能,并且是仿 Excel 的編輯效果。2013-05-05jQuery實(shí)現(xiàn)html雙向綁定功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)html雙向綁定功能,涉及jQuery針對(duì)HTML頁(yè)面元素事件綁定相關(guān)操作技巧,需要的朋友可以參考下2017-10-10