完美js模拟网页对话框效果(可拖曳)

JavaScript代码
  1. /**  
  2.     Powered By CMSDream Copyright © 2007-2008 All rights reserved.  
  3.     23:25 2008-5-3  
  4. **/  
  5. var msgbox_scripts = document.getElementsByTagName("script");   
  6. msgbox_path = msgbox_path.substring(0, msgbox_path.lastIndexOf('/')+1);   
  7.   
  8. var CMSMsgBox = function(oWidth, oHeight, oSkin, oTitle){   
  9.     this.Id = 'msgbox_'+Math.random().toString().replace('\.''_');   
  10.     /*  
  11.      * 位置  
  12.      * =================  
  13.      * ===== 1 2 3 =====  
  14.      * ===== 4 5 6 =====  
  15.      * ===== 7 8 9 =====  
  16.      * =================  
  17.      */  
  18.     this.Position = 5;   
  19.   
  20.     this.Skin = oSkin || 'default';   
  21.     this.Width = oWidth || 500;   
  22.     this.Height = oHeight || 260;   
  23.        
  24.     this.Title = oTitle || 'CMSDream:';   
  25.     this.Value = '';   
  26.     this.Float = true;   
  27.     this.Mask = false;   
  28.     this.Drag = true;   
  29.     this.Return = false;   
  30.     this.BoxDiv = null;   
  31. };   
  32.   
  33. CMSMsgBox.prototype.show = function(){   
  34.     /* 加载样式表 */  
  35.     if(!this.$('msgbox_styles')){   
  36.         var css = document.createElement('link');   
  37.         css.setAttribute('href', msgbox_path + 'msgbox.css');   
  38.         css.setAttribute('id''msgbox_styles');   
  39.         css.setAttribute('rel''stylesheet');   
  40.         css.setAttribute('type''text/css');   
  41.         document.getElementsByTagName('head')[0].appendChild(css);   
  42.     }   
  43.     if(this.$(this.Id))return false;   
  44.     this.Return = true;   
  45.     this.BoxDiv = this.createDiv(document.body, 'div''block'this.Id, 'msgbox_main_div');       
  46.     if(this.Mask)this.createMask();    
  47.     this.showBox();   
  48. };   
  49.   
  50. CMSMsgBox.prototype.showBox = function(){   
  51.     var self = this;   
  52.     var div = new Array();   
  53.        
  54.     div['box'] = this.createDiv(this.BoxDiv, 'div''block'this.Id+'bgcolor_div''msgbox_box msgbox_box_'+this.Skin);   
  55.     if(this.Width)div['box'].style.width = this.Width+'px';   
  56.     if(this.Height && this.Skin != 'loading')div['box'].style.height = this.Height+'px';   
  57.        
  58.     div['title'] = this.createDiv(div['box'], 'div'nullnull'msgbox_title');   
  59.   
  60.     /* =====================================  
  61.      * 拖曳效果  
  62.      *=====================================**/  
  63.     if(this.Drag){   
  64.         div['move'] = this.createDiv(this.BoxDiv, 'div''none'null'msgbox_move_div');         
  65.         div['title'].style.cursor = 'move';   
  66.         div['title'].onmousedown = function(e){   
  67.             var X = document.all ? event.clientX : e.pageX;   
  68.             var Y = document.all ? event.clientY : e.pageY;   
  69.             var T = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;   
  70.             var cLeft = div['code'].offsetLeft + div['box'].offsetLeft;   
  71.             var cTop = div['code'].offsetTop + div['box'].offsetTop - T;   
  72.             if((X > cLeft && X < (cLeft + div['code'].offsetWidth)) && (Y > cTop && Y < (cTop + div['code'].offsetHeight)))   
  73.             return;   
  74.             this.style.zIndex += 10;       
  75.             this.setAttribute('MD''1');   
  76.             this.setAttribute('XCX', parseInt(X - div['box'].offsetLeft).toString());   
  77.             this.setAttribute('XCY', parseInt(Y - div['box'].offsetTop).toString());   
  78.             div['move'].style.width = parseInt(div['box'].offsetWidth-10) + 'px';   
  79.             div['move'].style.height = parseInt(div['box'].offsetHeight - 10) + 'px';   
  80.             div['move'].style.left = div['box'].offsetLeft + 'px';   
  81.             div['move'].style.top = div['box'].offsetTop + 'px';   
  82.             div['move'].style.display = 'block';   
  83.         };   
  84.         document.onmousemove = function(e){   
  85.             if(div['title'].getAttribute('MD')!='1')return;   
  86.             var XCX = div['title'].getAttribute('XCX');   
  87.             var XCY = div['title'].getAttribute('XCY');   
  88.             var X = document.all ? event.clientX : e.pageX;   
  89.             var Y = document.all ? event.clientY : e.pageY;   
  90.             div['move'].style.left = parseInt(X - XCX) + 'px';   
  91.             div['move'].style.top = parseInt(Y - XCY) + 'px';   
  92.         };   
  93.         document.onmouseup = function(e){   
  94.             if(div['title'].getAttribute('MD')!='1')return;   
  95.             div['title'].setAttribute('MD''0');   
  96.             div['title'].setAttribute('XCX''0');   
  97.             div['title'].setAttribute('XCY''0');   
  98.             div['box'].style.left = div['move'].offsetLeft + 'px';   
  99.             div['box'].style.top = div['move'].offsetTop + 'px';   
  100.             if(self.IsObject(div['mask'])){   
  101.                 div['mask'].style.left = div['move'].offsetLeft + 'px';   
  102.                 div['mask'].style.top = div['move'].offsetTop + 'px';   
  103.             }      
  104.             div['move'].style.display = 'none';   
  105.         };   
  106.     }   
  107.     div['span'] = this.createDiv(div['title'], 'span''block'this.Id+'_msgTitle');   
  108.     div['span'].innerHTML = this.Title;   
  109.   
  110.     /* =====================================  
  111.      * 关闭按钮  
  112.      *=====================================**/  
  113.     div['code'] = this.createDiv(div['title'], 'code''block');   
  114.     div['code'].setAttribute('title''close');   
  115.     div['code'].style.position = 'relative';   
  116.     div['code'].innerHTML = "close";   
  117.     div['code'].onclick = function(){self.close();};   
  118.        
  119.     /* =====================================  
  120.      * 文本区域  
  121.      *=====================================**/  
  122.     div['text'] = this.createDiv(div['box'], 'div''block'this.Id+'_msgContent''msgbox_content');   
  123.     div['value'] = this.createDiv(div['text'], 'div''block'this.Id+'_msgValue''msgbox_container');   
  124.     div['clear'] = this.createDiv(div['text'], 'div''block'null'msgbox_clear');   
  125.     if(this.Height && this.Skin != 'loading'){   
  126.         div['value'].style.height = (div['box'].offsetHeight - div['title'].offsetHeight - 16) + 'px';   
  127.         div['value'].style.overflowY = 'auto';   
  128.     }   
  129.     div['value'].innerHTML = this.Value;   
  130.   
  131.     /* =====================================  
  132.      * 创建一个iframe档住select   
  133.      *=====================================**/  
  134.     if(document.all && !this.Mask){   
  135.         div['mask'] = this.createDiv(this.BoxDiv, 'iframe''block'null'msgbox_frame');   
  136.         div['mask'].style.width = div['box'].offsetWidth+'px';   
  137.         div['mask'].style.height = div['box'].offsetHeight+'px';   
  138.     }      
  139.        
  140.        
  141.     mDiv_Float();      
  142.     if(this.Float){   
  143.         if(window.addEventListener){   
  144.             window.addEventListener('resize', mDiv_Float, false);   
  145.             window.addEventListener('scroll', mDiv_Float, false);   
  146.         }else{   
  147.             window.attachEvent('onresize', mDiv_Float);   
  148.             window.attachEvent('onscroll', mDiv_Float);   
  149.         }   
  150.     }   
  151.   
  152.     /* =====================================  
  153.      * 漂浮效果  
  154.      *=====================================**/  
  155.     function mDiv_Float(){   
  156.         var t = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;   
  157.         var h = document.documentElement.clientHeight ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : document.body.clientHeight;   
  158.         var w = document.documentElement.clientWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;   
  159.            
  160.         switch(self.Position){   
  161.             case 1:  {var l = 1;var r = t + 1;break;}   
  162.             case 2:  {var l = parseInt((w/2)-(div['box'].offsetWidth/2));var r = t + 1;break;}   
  163.             case 3:  {var l = parseInt(w-div['box'].offsetWidth-1);var r = t + 1;break;}   
  164.             case 4:  {var l = 1;var r = parseInt(t+((h/2)-(div['box'].offsetHeight/2)));break;}   
  165.             case 5:  {var l = parseInt((w/2)-(div['box'].offsetWidth/2));var r = parseInt(t+((h/2)-(div['box'].offsetHeight/2)));break;}   
  166.             case 6:  {var l = parseInt(w-div['box'].offsetWidth-1);var r = parseInt(t+((h/2)-(div['box'].offsetHeight/2)));break;}   
  167.             case 7:  {var l = 1;var r = parseInt(t+(h-div['box'].offsetHeight-1));break;}   
  168.             case 8:  {var l = parseInt((w/2)-(div['box'].offsetWidth/2));var r = parseInt(t+(h-div['box'].offsetHeight-1));break;}   
  169.             case 9:  {var l = parseInt(w-div['box'].offsetWidth-1);var r = parseInt(t+(h-div['box'].offsetHeight-1));break;}   
  170.             default: {var l = parseInt((w/2)-(div['box'].offsetWidth/2));var r = parseInt(t+((h/2)-(div['box'].offsetHeight/2)));break;}   
  171.         }   
  172.            
  173.         div['box'].style.left = l+'px';   
  174.         div['box'].style.top = r+'px';   
  175.         if(document.all && !self.Mask){   
  176.             div['mask'].style.left = l+'px';   
  177.             div['mask'].style.top = r+'px';   
  178.         }   
  179.     }   
  180. };   
  181.   
  182. /* 蒙板 */  
  183. CMSMsgBox.prototype.createMask = function(){   
  184.     var self = this;   
  185.     var div = new Array(1);   
  186.     if(document.all)   
  187.     div['mask_ifr'] = this.createDiv(this.BoxDiv, 'iframe''block'null'msgbox_mask_frame');   
  188.     div['mask_div'] = this.createDiv(this.BoxDiv, 'div''block'null'msgbox_mask_div');   
  189.        
  190.     function window_onresize(){   
  191.         var h = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);           
  192.         var w = document.all ? Math.min(document.documentElement.scrollWidth, document.body.scrollWidth) :    
  193.                 Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);   
  194.            
  195.         div['mask_div'].style.width = w + 'px';   
  196.         div['mask_div'].style.height = h + 'px';   
  197.         if(document.all){   
  198.             div['mask_ifr'].style.width = div['mask_div'].offsetWidth + 'px';   
  199.             div['mask_ifr'].style.height = div['mask_div'].offsetHeight + 'px';   
  200.         }   
  201.     }   
  202.        
  203.     if(window.addEventListener){   
  204.         window.addEventListener('scroll', window_onresize, false);   
  205.         window.addEventListener('resize', window_onresize, false);   
  206.     }else{   
  207.         window.attachEvent('onscroll', window_onresize);   
  208.         window.attachEvent('onresize', window_onresize);   
  209.     }   
  210.     if(this.Skin=='loading')div['mask_div'].ondblclick = function(){self.close();};   
  211.     window_onresize();   
  212. };   
  213.   
  214. /* 创建一个元素  */  
  215. CMSMsgBox.prototype.createDiv = function(iParent, element, display, id, clsname){   
  216.     var ii = document.createElement(element);   
  217.     if(typeof(display)=='string')ii.style.display = display;   
  218.     if(typeof(clsname)=='string')ii.className = clsname;   
  219.     if(typeof(id)=='string')ii.setAttribute('id', id);   
  220.     iParent.appendChild(ii);   
  221.     return ii;   
  222. };   
  223.   
  224. /* 关闭 */  
  225. CMSMsgBox.prototype.close = function(){    
  226.     if(this.IsObject(this.BoxDiv) && this.BoxDiv.parentNode){   
  227.         this.BoxDiv.parentNode.removeChild(this.BoxDiv);   
  228.     }   
  229. };   
  230.   
  231. CMSMsgBox.prototype.$ = function(o){   
  232.     return document.getElementById(o);   
  233. };   
  234.   
  235. CMSMsgBox.prototype.IsObject = function(o){   
  236.     return typeof(o)!='undefined' && o!=null;   
  237. };   
  238.   
  239. CMSMsgBox.prototype.setBackgroundColor=function(iColor){   
  240.     if(!this.$(this.Id+'bgcolor_div'))return;   
  241.     this.$(this.Id+'bgcolor_div').style.backgroundColor = iColor;   
  242. };   
  243.   
  244. CMSMsgBox.prototype.setColor=function(iColor){   
  245.     if(!this.$(this.Id+'_msgValue'))return;   
  246.     this.$(this.Id+'_msgValue').style.color = iColor;   
  247. };   
  248.   
  249. CMSMsgBox.prototype.setValue=function(iValue){   
  250.     if(!this.$(this.Id+'_msgValue'))return;   
  251.     this.$(this.Id+'_msgValue').innerHTML = iValue;   
  252. };   
  253.   
  254. CMSMsgBox.prototype.setTitle=function(iTitle){   
  255.     if(!this.$(this.Id+'_msgTitle'))return;   
  256.     this.$(this.Id+'_msgTitle').innerHTML = iTitle;   
  257. };  

效果图片:

效果预览

下载源代码

引用通告地址: 点击获取引用地址
标签: javascript,拖曳,模拟
评论: 0 | 引用: 0 | 阅读: 177 | 打印 | 打包
发表评论
昵 称: 密 码:
网 址: 邮 箱:
验证码: 验证码图片 选 项:
头 像:
内 容: