符合web标准的可拖动层特效

本文介绍了一种符合Web标准的可拖动层特效实现方案,通过JavaScript编程实现了窗口的创建、拖动、最大化、最小化及关闭等功能,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/wonsoft/article/details/5743841

[xhtml]  view plain copy
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2. <HTML>  
  3. <HEAD>  
  4. <TITLE> 符合web标准的可拖动层特效</TITLE>  
  5. <meta content="text/html;charset=gb2312" http-equiv="content-type">  
  6. <mce:style><!-- 
  7. *{font-size:9pt;font-family:宋体;line-height:130%;} 
  8. --></mce:style><style mce_bogus="1">*{font-size:9pt;font-family:宋体;line-height:130%;}</style>  
  9. </HEAD>  
  10. <BODY>  
  11. <SCRIPT LANGUAGE="JavaScript">  
  12. <!--  
  13. Global = new Object();  
  14. Global.__UniqueID = 0;  
  15. Global.GetUniqueID = function(){ return "__MT_UID_" + Global.__UniqueID ++;};  
  16. function Dialog(title,icon,body){  
  17.  this.Icon = icon || "http://www.jsgod.net/test/images/window_blur.gif";  
  18.  this.Title = title || "Untitled Document";  
  19.  this.Body = body || "Blank Document";  
  20.  this.UniqueID = Global.GetUniqueID();  
  21.  //////////////////////////////////////  
  22.  this.Width = 400;  
  23.  this._mousedown = false;  
  24.  this._offsetX = 0;  
  25.  this._offsetY = 0;  
  26.  this._x = 0;  
  27.  this._y = 0;  
  28.  var i,l;  
  29.  //////////////////////////////////////  
  30.  this.SetRect = function(_width,_top,_left){  
  31.   var obj;  
  32.   if(_width > 200) this.Width = _width;  
  33.   this._x = _left;  
  34.   this._y = _top;  
  35.   obj = document.getElementById(this.UniqueID);  
  36.   if(obj){  
  37.    obj.style.width = this.Width;  
  38.    obj.style.top = _top;  
  39.    obj.style.left = _left;  
  40.   }  
  41.  }  
  42.  this.SetIndex = function(index){  
  43.   if(isNaN(index)) return;  
  44.   var obj = document.getElementById(this.UniqueID);  
  45.   if(obj) obj.style.zIndex = index;  
  46.  }  
  47.  this.BindEvent = function(){  
  48.   var self = this,obj = null;  
  49.   var table = document.getElementById(this.UniqueID);  
  50.   if((null == table)||(table.tagName != "TABLE")) return;  
  51.   if(table.rows && table.rows[0].cells && table.rows[0].cells[0] && table.rows[0].cells[0].firstChild){  
  52.    obj = table.rows[0].cells[0].firstChild;  
  53.    obj.rows[0].cells[2].lastChild.onclick = function(e){  
  54.     var obj = document.getElementById(self.UniqueID);  
  55.     if(obj) obj.parentNode.removeChild(obj);  
  56.    }  
  57.    obj.rows[0].cells[2].firstChild.onclick = function(e){  
  58.     e = window.event || e;  
  59.     var obj = document.getElementById(self.UniqueID);  
  60.     if(obj && obj.rows && obj.rows.length){  
  61.      with(obj.rows[1].style){  
  62.       display = display == "" ? "none" : "";  
  63.      }  
  64.     }  
  65.     obj = (e.target || e.srcElement);  
  66.     if(obj.on == "true"){  
  67.      obj.on = "false";  
  68.      obj.src = "http://www.jsgod.net/test/images/min.gif";  
  69.     }  
  70.     else{  
  71.      obj.on = "true";  
  72.      obj.src = "http://www.jsgod.net/test/images/max.gif";  
  73.     }  
  74.    }  
  75.    if(obj.rows && obj.rows[0].cells && obj.rows[0].cells[0].firstChild) obj = obj.rows[0].cells[0].firstChild;  
  76.    obj.onmousedown = function(e){  
  77.     e = window.event || e;  
  78.     if(e.cancelBubble) e.cancelBubble();  
  79.     //Firefox....  
  80.    };  
  81.    obj.ondblclick = function(){ self.Close();};  
  82.    table.onmousedown = function(){  
  83.     var obj;  
  84.     if(Dialog.ObjectRef && (this != Dialog.ObjectRef)){  
  85.      obj = document.getElementById(Dialog.ObjectRef.UniqueID);  
  86.      if(obj){  
  87.       obj.style.borderColor = "#cccccc";  
  88.       obj.style.zIndex = 90;  
  89.       obj.rows[0].cells[0].firstChild.rows[0].cells[0].firstChild.src = "images/window_blur.gif";  
  90.      }  
  91.     }  
  92.     obj = document.getElementById(self.UniqueID);  
  93.     if(obj){  
  94.      obj.style.borderColor = "lightblue";  
  95.      obj.style.zIndex = 99;  
  96.      obj.rows[0].cells[0].firstChild.rows[0].cells[0].firstChild.src = "images/window.gif";  
  97.     }  
  98.     Dialog.ObjectRef = self;  
  99.    }  
  100.   }  
  101.   if(table.rows){  
  102.    table.rows[0].cells[0].firstChild.rows[0].cells[1].onmousedown = function(e){  
  103.     var obj;  
  104.     e = window.event || e;  
  105.     self._dragable = true;  
  106.     obj = document.getElementById(self.UniqueID);  
  107.     if(null == obj) return;  
  108.     self._x = e.clientX;  
  109.     self._y = e.clientY;  
  110.     self._offsetX = parseInt(obj.style.left);  
  111.     self._offsetY = parseInt(obj.style.top);  
  112.    }  
  113.    table.onmouseover = function(e){  
  114.     e = window.event || e;  
  115.     var obj = document.getElementById(self.UniqueID);  
  116.     if(obj) obj.style.cursor = "default";  
  117.       
  118.    };  
  119.    table.onmouseup = function(e){  
  120.     var obj;  
  121.     e = window.event || e;  
  122.     self._dragable = false;  
  123.     obj = document.getElementById(self.UniqueID);  
  124.     if(obj) obj.style.cursor = "default";  
  125.    }  
  126.    table.rows[0].ondblclick = function(e){  
  127.     obj = document.getElementById(self.UniqueID);  
  128.     if(null == obj) return;  
  129.     if(obj.rows && (obj.rows.length > 1)){  
  130.      with(obj.rows[1].style){  
  131.       display = display == "" ? "none" : "";  
  132.       obj = obj.rows[0].cells[0].firstChild.rows[0].cells[2].firstChild;  
  133.       obj.src = obj.on == "true" ? "http://www.jsgod.net/test/images/min.gif" : "http://www.jsgod.net/test/images/max.gif";  
  134.       obj.on = obj.on == "true" ? "false" : "true";  
  135.      };  
  136.     }  
  137.    }  
  138.   }  
  139.  }  
  140.  this.Close = function(){  
  141.   var table = document.getElementById(this.UniqueID);  
  142.   if(table) table.parentNode.removeChild(table);  
  143.  }  
  144.  this.toString = function(){  
  145.   var shtml = '';  
  146.   shtml += '<table cellpadding="0" cellspacing="0" border="1" onselectstart="return false;" oncontextmenu="return false;" ondragstart="return false;" bgcolor="#ffffff" id="' + this.UniqueID + '" style="border:solid 1px #cccccc;cursor:default;width:' + (this.Width) + 'px;position:absolute;top:' + this._y + 'px;left:' + this._x + 'px;">';  
  147.   shtml += '<tr height="20"><td style="border:0px;" mce_style="border:0px;"><table cellpadding="5" cellspacing="0" border="0" width="100%"><tr><td width="20"><img src="' + this.Icon + '" mce_src="' + this.Icon + '" width="16" height="16"/></td><td align="left"><span style="width:200px;overflow:hidden;" title="' + this.Title + '">' + this.Title + '</span></td><td width="32"><img src="http://www.jsgod.net/test/images/min.gif" mce_src="http://www.jsgod.net/test/images/min.gif" height="13" width="13" title="最小化" on="false" hspace="1" onmouseover="style.borderColor=/'lightblue/'" style="border:solid 1px #ffffff" mce_style="border:solid 1px #ffffff" onmouseout="style.borderColor=/'#ffffff/'"/><img src="http://www.jsgod.net/test/images/close.gif" mce_src="http://www.jsgod.net/test/images/close.gif" width="13" height="13" title="关闭" style="border:solid 1px #ffffff;" mce_style="border:solid 1px #ffffff;" onmouseover="style.borderColor=/'lightblue/'" onmouseout="style.borderColor=/'#ffffff/'"/></td></tr>';  
  148.   shtml += '</table></td></tr>';  
  149.   shtml += '<tr style="display:;" mce_style="display:;"><td style="border:0px;padding:5px;border-top:solid 1px #cccccc;" mce_style="border:0px;padding:5px;border-top:solid 1px #cccccc;" valign="top">' + this.Body + '</td></tr>';  
  150.   shtml += '</table>';  
  151.   return shtml;  
  152.  }  
  153. }  
  154. document.onmousemove = function(e){  
  155.  var _x,_y,obj;  
  156.  e = window.event || e;  
  157.  if(null == Dialog.ObjectRef) return;  
  158.  obj = document.getElementById(Dialog.ObjectRef.UniqueID);  
  159.  if(null == obj) return;  
  160.  if(Dialog.ObjectRef._dragable){  
  161.   obj.style.top = Dialog.ObjectRef._offsetY + e.clientY - Dialog.ObjectRef._y;  
  162.   obj.style.left = Dialog.ObjectRef._offsetX + e.clientX - Dialog.ObjectRef._x;  
  163.   obj.style.cursor = "move";  
  164.  }  
  165. }  
  166. Dialog.ObjectRef = null;  
  167. var dialog = new Dialog("哇哈哈",null,"Matrixy Herry");  
  168. document.write(dialog);  
  169. dialog.BindEvent();  
  170. dialog.SetIndex(1);  
  171. dialog.SetRect(400,100,200);  
  172. var dialog1 = new Dialog("偶来试试",null,'<img src="" width="300" height="200"/>');  
  173. document.write(dialog1);  
  174. dialog1.BindEvent();  
  175. dialog1.SetIndex(2);  
  176. dialog1.SetRect(350,100,250);  
  177. var dialog2 = new Dialog("Hello World");  
  178. document.write(dialog2);  
  179. dialog2.BindEvent();  
  180. dialog2.SetIndex(24);  
  181. dialog2.SetRect(380,10,20);  
  182. var dialog3 = new Dialog("4");  
  183. document.write(dialog3);  
  184. dialog3.BindEvent();  
  185. dialog3.SetRect(390,200,100);  
  186. var dialog4 = new Dialog("5");  
  187. document.write(dialog4);  
  188. dialog4.SetIndex(340);  
  189. dialog4.BindEvent();  
  190. //-->  
  191. </SCRIPT>  
  192. </BODY>  
  193. </HTML>  

 

调用方法: 

以下是引用片段:
var dialog1 = new Dialog("偶来试试",null,’<img src="" width="300" height="200"/>’); 
document.write(dialog1); 
dialog1.BindEvent(); 
dialog1.SetIndex(2); 
dialog1.SetRect(350,100,250);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值