javascript 通過封裝div方式彈出div表單_javascript技巧

來源:互聯網
上載者:User

圖1(彈出一個div)

 

圖2(彈出多個)
建構函式:

複製代碼 代碼如下:

var DivWindow= function(popup/*最外層div id*/,popup_drag/*拖動div id*/,popup_exit/*退出按鈕id*/ ,exitButton/*觸發伺服器端退出按鈕id*/,varwidth,varheight,zindex){
this.popup =popup ;
this.height =varheight ; //視窗高度,並沒用來設定視窗高度寬度,用來定位在螢幕的位置
this.width =varwidth ; //視窗寬度
this.popup_exit=popup_exit;
this.exitButton=exitButton;
this.zindex=zindex;
this.init = function(){ //初始化視窗
this.popupShow();
this.startDrag(); //設定拖動
this.setCommond(); //設定關閉
DivWindow.ArrayW.push(document.getElementById(this.popup)); //儲存視窗到數組
};this.init();
};

調用:
複製代碼 代碼如下:

<input type="button" id="show" onclick="javascript:new DivWindow('popup','popup_drag','popup_exit','exitButton','500','700',4);" value='觸發彈出詳細基本資料DIV' />

有的地方可能大家覺得有點奇怪,不要問我問什麼,因為我有這個需要,有時間再修改一下遮罩效果。更詳細代碼請見下載
完整代碼
<!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> <title>視窗類別 </title> <meta http-equiv="Content-Type" content="text/html;charset=gb2312" /> <style type="text/css"> .mask{position: absolute; top: 0px; left: 0px; filter: alpha(opacity=50); -moz-opacity:0.5; opacity:0.5; background-color: #ffffff; z-index: 2; display: none;} /* 彈出基本資料div */ div.sample_popup {height:auto; border: 1px solid #327eca; width: 300px; } div.menu_form_header{ background: url('../images/baseInfo/titleBG.gif') repeat-x; } div.sample_popup div.menu_form_header { border-bottom: 0px; cursor: default; width:100%; height: 22px; line-height: 22px; vertical-align: middle; text-decoration: none; font-family: "Times New Roman", Serif; font-weight: 800; font-size: 13px; color: #206040; } div.menu_form_body { width:100%; height:150px; font-size:12px; background-color:#f1f8fe; } div.sample_popup input.menu_form_exit { float: right; margin: 4px 5px 0px 0px; cursor: pointer; } /*end: 彈出商品資訊div */ </style> <script type="text/javascript"> /************************************************** * DivWindow.js **************************************************/ var DivWindow= function(popup/*最外層div id*/,popup_drag/*拖動div id*/,popup_exit/*退出按鈕id*/ ,exitButton/*觸發伺服器端退出按鈕id*/,varwidth,varheight,zindex){ this.popup =popup ; //視窗名稱 this.height =varheight ; //視窗高度,並沒用來設定視窗高度寬度,用來定位在螢幕的位置 this.width =varwidth ; //視窗寬度 this.popup_exit=popup_exit; this.exitButton=exitButton; this.zindex=zindex; this.init = function(){ //初始化視窗 this.popupShow(); this.startDrag(); //設定拖動 this.setCommond(); //設定關閉 DivWindow.ArrayW.push(document.getElementById(this.popup)); //儲存視窗到數組 };this.init(); }; //儲存視窗到數組 DivWindow.ArrayW = new Array(); //字串串連類 DivWindow.StringBuild = function(){ this.arr = new Array(); this.push = function(str){ this.arr.push(str); }; this.toString = function(){ return this.arr.join(""); }; }; //拖動類 DivWindow.Drag = function(o ,oRoot){ var _self = this; //拖動對象 this.obj = (typeof oRoot != "undefined") ?oRoot : o; this.relLeft = 0; //記錄橫座標 this.relTop = 0; //記錄縱座標 o.onselectstart = function(){ return false; }; o.onmousedown = function(e){ //滑鼠按下 e = _self.fixE(e); _self.relLeft = e.clientX - _self.fixU(_self.obj.style.left); _self.relTop = e.clientY - _self.fixU(_self.obj.style.top); document.onmousemove = function(e){ _self.drag(e); //_self.obj.style.border = "1px dashed #000000"; //_self.obj.style.filter = "alpha(opacity=30)"; //_self.obj.style.opacity = "0.3"; }; document.onmouseup = function(){ _self.end(); //_self.obj.style.border = "1px solid #cccccc"; //_self.obj.style.borderBottom = "2px solid #E0E0E0"; //_self.obj.style.borderRight = "2px solid #E0E0E0"; //_self.obj.style.filter = "alpha(opacity=100)"; //_self.obj.style.opacity = "1"; }; }; this.drag = function(e){ //拖動 e = this.fixE(e); var l = e.clientX - this.relLeft; var t = e.clientY - this.relTop; if (t < 0) { t = 0; //防止頭部消失 } this.obj.style.left = l +"px"; this.obj.style.top = t +"px"; }; this.end = function(){ //結束拖動 document.onmousemove = null; document.onmouseup = null; }; this.fixE = function(e){ //修複事件 if (typeof e == "undefined") e = window.event; return e; }; this.fixU = function(u){ //處理px單位 return parseInt(u.split("p")[0]); }; }; //視窗拖動 DivWindow.prototype.startDrag = function(){ var obj = document.getElementById(this.popup); new DivWindow.Drag(obj.childNodes[0] ,obj); }; //設定視窗優先順序 DivWindow.prototype.setTop = function(){ document.getElementById(this.popup).onclick = document.getElementById(this.popup).onmousedown = function(){ for(var i=0;i<DivWindow.ArrayW.length;i++) { DivWindow.ArrayW[i].style.zIndex = 1; } this.style.zIndex = 100; }; }; //顯示 DivWindow.prototype.popupShow=function() { document.all.mask.style.display="block"; document.all.mask.style.width=window.screen.width +20; document.all.mask.style.height=window.screen.width +20; var element = document.getElementById(this.popup); element.style.position = "absolute"; element.style.visibility = "visible"; element.style.display = "block"; element.style.width=this.width; element.style.height='auto'; element.style.left = (window.screen.width - this.width)/2+"px"; //element.style.top =(window.screen.height-this.height-100)/2+"px"; element.style.top =20+"px"; element.style.zIndex=this.zindex; } //設定關閉 DivWindow.prototype.setCommond = function(){ var _self = this; //根對象 var obj = document.getElementById(this.popup); var exit = document.getElementById(this.popup_exit); var triggServerEvent=document.getElementById(this.exitButton); //設定關閉 exit.onclick = function(){ obj.style.display = "none"; obj.style.visibility = 'hidden'; document.all.mask.style.display='none'//關閉遮罩層 triggServerEvent.click();//觸發伺服器端退出事件 }; }; </script> </head> <body> <div>sffs <input type="button" id="show" onclick="javascript:new DivWindow('popup','popup_drag','popup_exit','exitButton','500','700',4);" value='觸發彈出詳細基本資料DIV' /> <input type="button" id="exitButton" value="aaaa" /> </div> <!-- 遮罩層 --> <div id="mask" class="mask"> </div> <!-- 彈出基本資料詳細DIV層 --> <div class="sample_popup" id="popup" > <div class="menu_form_header" id="popup_drag"> <input type="button" id="popup_exit" value="退出" /> 片區資料 </div> <div class="menu_form_body" > <div id="popDetail"> 片區名稱:<input type="button" id="Button1" onclick="javascript:new DivWindow('Div1','Div2','exit2','exitButton','500','700',5);" value='觸發彈出詳細基本資料DIV2' /> </div> </div> </div> <!-- 彈出基本資料詳細DIV層 --> <div class="sample_popup" id="Div1" > <div class="menu_form_header" id="Div2"> <input type="button" id="exit2" value="退出" /> 片區資料2222 </div> <div class="menu_form_body" > <div id="Div3"> 片區名稱:yj </div> </div> </div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

打包下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.