js弹出窗口实现动态文件上传

来源:互联网 发布:js 银行卡 4位 空格 编辑:程序博客网 时间:2024/05/17 07:08
  1. function showWin(title,html,width,height,modal,type,url)  
  2. {  
  3.     //设置窗口大小  
  4.     if(width<400)  
  5.         {  
  6.             width=400;  
  7.         }  
  8.     if(height<200)  
  9.         {  
  10.             height=200;  
  11.         }  
  12.     //body内边大小  
  13.     var cw=document.body.clientWidth;  
  14.     var ch=document.body.clientHeight;  
  15.     //文本框大小  
  16.     var sw=document.body.scrollWidth;  
  17.     var sh=document.body.scrollHeight;  
  18.      //可见区域坐标  
  19.     var st = document.body.scrollTop;  
  20.     var sl = document.body.scrollLeft;  
  21.     var w=cw>sw?cw:sw;  
  22.     var h=ch>sh?ch:sh;  
  23.     //创建模态  
  24.     if(modal)  
  25.     {  
  26.         var mask=document.createElement("div");  
  27.         mask.style.cssText = "position:absolute;left:0;top:0px;background:#fff;filter:Alpha(Opacity=30);opacity:0.5;z-index:100;width:" + w + "px;height:" + h + "px;";  
  28.         document.body.appendChild(mask);  
  29.     }  
  30.     // 创建主窗口  
  31.     var win=document.createElement("div");  
  32.     win.style.cssText="position:absolute;left:" + (sl + cw/2 - width/2) + "px;top:" + (st + ch/2 - height/2) + "px;background:#f0f0f0;z-index:101;width:" + width + "px;height:" + height + "px;border:solid 2px #afccfe;";  
  33.     //标题栏目  
  34.     var tBar=document.createElement("div");  
  35.     tBar.style.cssText="margin:0;width:" + width + "px;height:25px;background:green;cursor:move;";  
  36.     //给标题栏加文字  
  37.     var tText=document.createElement("div");  
  38.     tText.style.cssText="float:left;margin-left:30px;text-align:center;height:25px;line-height:25px;width:"+(width-80)+"px;";  
  39.     tText.innerHTML=title;  
  40.     tBar.appendChild(tText);  
  41.     //添加关闭按钮  
  42.     var  closeButton=document.createElement("div");  
  43.     closeButton.style.cssText="float:right;width:20px;margin:3px;cursor:pointer;color:red;";  
  44.     closeButton.innerHTML="X";  
  45.     tBar.appendChild(closeButton);  
  46.     win.appendChild(tBar);  
  47.     //窗口主体  
  48.     var bodyCon=document.createElement("div");  
  49.     bodyCon.style.cssText="text-align:left;width:" + width + "px;height:" + (height -50) + "px;overflow:auto;";  
  50.     switch(type)  
  51.     {  
  52.     case "file":  
  53.         var formCon=document.createElement("form");  
  54.         formCon.action=url;  
  55.         formCon.method="post";  
  56.         formCon.enctype="multipart/form-data";  
  57.         formCon.target="hidFram";  
  58.         formCon.id="formCon";  
  59.         formCon.name="formCon";  
  60.         formCon.innerHTML="<br/><label>请选择文件:</label>"+html;  
  61.         bodyCon.appendChild(formCon);  
  62.         var framCon=document.createElement("iframe");  
  63.         framCon.name="hidFram";  
  64.         framCon.id="hidFram";  
  65.         framCon.style.cssText="width:400px;height:200px;";  
  66.         bodyCon.appendChild(framCon);  
  67.         //当framCon内容加载完时,设置内容  
  68.         framCon.onload = function(){  
  69.         $res=framCon.contentWindow.document.body.innerHTML;  
  70.             alert($res+"Local iframe is now loaded.");  
  71.             }  
  72.         break;  
  73.         default:  
  74.         bodyCon.innerHTML=html;  
  75.     }  
  76.     win.appendChild(bodyCon);  
  77.     //窗体底部的按钮部分  
  78.     var btnCon = document.createElement('div');  
  79.     btnCon.style.cssText = "width:" + width + "px;height:25px;text-height:20px;background:#EED5B7;text-align:center;padding-top:3px;";  
  80.     var subButton=document.createElement("button");  
  81.     subButton.style.cssText="float:right;";  
  82.     subButton.name="submit";  
  83.     subButton.innerHTML="确定";  
  84.     btnCon.appendChild(subButton);  
  85.     //用户填充按钮之间的空白  
  86.     var nbsp = document.createElement('label');  
  87.     nbsp.innerHTML = "  ";  
  88.     btnCon.appendChild(nbsp);  
  89.     var celButton=document.createElement("button");  
  90.     celButton.style.cssText="float:right;";  
  91.     celButton.name="cancel";  
  92.     celButton.innerHTML="取消";  
  93.     btnCon.appendChild(celButton);  
  94.     win.appendChild(btnCon);  
  95.     document.body.appendChild(win);  
  96.      //添加关闭按钮  
  97.     //var closeBt=document.createElement("button");  
  98.     //closeBt.innerHTML="关闭";  
  99.     //拖动窗口  
  100.     //鼠标坐标  
  101.     var mouseX=0;  
  102.     var mouseY=0;  
  103.     //窗口相对坐标  
  104.     var toTop=0;  
  105.     var toLeft=0;  
  106.     //判断鼠标是否可以移动  
  107.     var moveable=false;  
  108.     //标题栏被按下时初始化数据  
  109.     tBar.onmousedown=function()  
  110.     {  
  111.         var action=getEvent();  
  112.         moveable=true;  
  113.         mouseX=action.clientX;  
  114.         mouseY=action.clientY;  
  115.         toTop=parseInt(win.style.top);  
  116.         toLeft=parseInt(win.style.left);  
  117.         tBar.onmousemove=function()  
  118.         {  
  119.               
  120.             if(moveable)  
  121.                 {  
  122.                    var eve=getEvent();  
  123.                    var x=toLeft+eve.clientX-mouseX;  
  124.                    var y=toTop+eve.clientY-mouseY;  
  125.                    //判断是否在窗口里面  
  126.                    if(x>0&&(x+width<w)&&y>0&&(y+height<h))  
  127.                        {  
  128.                         win.style.left=x+"px";  
  129.                         win.style.top=y+"px";  
  130.                        }  
  131.                 }     
  132.         }  
  133.         //释放鼠标不移动  
  134.         document.onmouseup=function()  
  135.         {  
  136.             moveable=false;  
  137.         }  
  138.     }  
  139.     closeButton.onclick=celButton.onclick=function()  
  140.     {  
  141.           
  142.         document.body.removeChild(win);  
  143.         if(mask)  
  144.             {  
  145.                 document.body.removeChild(mask);  
  146.             }  
  147.     }  
  148.     subButton.onclick=function()  
  149.     {  
  150.         if(type=="file")  
  151.             {  
  152.                 formCon.submit();  
  153.             }  
  154.             document.body.removeChild(win);  
  155.             if(mask)  
  156.                 {  
  157.                     document.body.removeChild(mask);  
  158.                 }  
  159.     }  
  160. }  
  161. //获取事件  
  162. function getEvent()  
  163. {  
  164. return window.event || arguments.callee.caller.arguments[0];  
  165. }  
  166. function show(num,str,html)  
  167. {  
  168.     //num控制显示类型  
  169.     switch(num)  
  170.     {  
  171.         case 1:  
  172.             var content=" <input type='file' name='upfile'/><br/>"  
  173.             showWin('上传文件',content,300,100,true,"file",html);  
  174.         break;  
  175.         default:  
  176.             showWin('消息',str,300,100,true,"file",0);  
  177.     }  
  178. }  
0 0
原创粉丝点击