最近給衛(wèi)生局做一個(gè)表格上傳/可視化系統(tǒng),算是小有成果。今天把項(xiàng)目中的文件拖拽上傳模塊分離出來(lái),做了一個(gè)獨(dú)立的小demo,并把相關(guān)代碼打包上傳到了我的github中,為了其他學(xué)習(xí)者和開(kāi)發(fā)者提供拙見(jiàn)。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、重慶小程序開(kāi)發(fā)公司、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了美蘭免費(fèi)建站歡迎大家使用!
gitHub地址:https://github.com/codeplay2015/dragToUpload
由于代碼中我的注釋很詳盡,所以具體邏輯實(shí)現(xiàn)及不介紹了,大家直接看代碼及能明白?,F(xiàn)在簡(jiǎn)單列一個(gè)功能清單和一些用到的知識(shí)點(diǎn)清單:
知識(shí)點(diǎn):
截圖:
整體界面
點(diǎn)擊‘拖拽上傳'按鈕
拖拽文件到虛線框,文件拖入會(huì)邊框變紅提示
上傳成功,彈出提示
代碼:
1. html:
Title 拖拽上傳演示模板。點(diǎn)擊下方按鈕,彈出模態(tài)框
CSS
.overlay{ z-index: 99; position:fixed; display: none; top:0; left:0; width: 100%; height: 100%; background-color: #333; opacity:0.5; } .dropbox{ z-index: 100; display: none; position: fixed; width:500px; height:520px; margin:auto; top:0; right:0; bottom: 0; left:0; background-color: #fff; border-radius:6px; transition-duration: 0.9s; -webkit-transition-duration: 0.9s; overflow:hidden; text-align: center; } .items-container{ padding: 10px; } .content{ border: 3px dashed gray; border-radius: 10px; margin: 10px 20px; height:400px; overflow: auto; padding:2px 8px; } .head{ margin:0px; font-size:30px; color:#aaa; } .footer{ margin:5px auto } .btn{ border-radius: 20px; box-sizing: border-box; border-width: 2px; background-color: transparent; font-size: 14px; font-weight: 500; padding: 7px 18px } /*畫(huà)一個(gè)叉號(hào),表示推出界面*/ .css-close{display:inline-block; width:15px; height:2px; background:#000; font-size:0; line-height:0;vertical-align:middle;-webkit-transform: rotate(45deg);} .css-close:after { content:'.'; display:block; width:15px; height:2px; background:#000;-webkit-transform: rotate(90deg);} /*表格樣式*/ .table{ width:100%; border-collapse: collapse; } #content tr:first-child td{ border-top-width: 0px; } #content tr td:last-child{ cursor: pointer; color: red; } #content tr td{ padding: 8px; white-space: nowrap; overflow: hidden; text-overflow:ellipsis; border-top:1px solid #9A9A9A; } #content tr:hover{ background-color: #d5d5d5; } #content tr:active{ background-color: #9A9A9A; } a:link{ color:blue; } a:visited{ color:blue; } a:hover{ color:blue; } a:active{ color:red; }
js代碼:
function showModal() { //打開(kāi)上傳框 var modal = document.getElementById('modal'); var overlay = document.getElementsByClassName('overlay')[0]; overlay.style.display = 'block'; modal.style.display = 'block'; } function closeModal() { //關(guān)閉上傳框 var modal = document.getElementById('modal'); var overlay = document.getElementsByClassName('overlay')[0]; overlay.style.display = 'none'; modal.style.display = 'none'; } //用DOM2級(jí)方法為右上角的叉號(hào)和黑色遮罩層添加事件:點(diǎn)擊后關(guān)閉上傳框 document.getElementsByClassName('overlay')[0].addEventListener('click', closeModal, false); document.getElementById('close').addEventListener('click', closeModal, false); //利用html5 FormData() API,創(chuàng)建一個(gè)接收文件的對(duì)象,因?yàn)榭梢远啻瓮献?,這里采用單例模式創(chuàng)建對(duì)象Dragfiles var Dragfiles=(function (){ var instance; return function(){ if(!instance){ instance = new FormData(); } return instance; } }()); //為Dragfiles添加一個(gè)清空所有文件的方法 FormData.prototype.deleteAll=function () { var _this=this; this.forEach(function(value,key){ _this.delete(key); }) } //添加拖拽事件 var dz = document.getElementById('content'); dz.ondragover = function (ev) { //阻止瀏覽器默認(rèn)打開(kāi)文件的操作 ev.preventDefault(); //拖入文件后邊框顏色變紅 this.style.borderColor = 'red'; } dz.ondragleave = function () { //恢復(fù)邊框顏色 this.style.borderColor = 'gray'; } dz.ondrop = function (ev) { //恢復(fù)邊框顏色 this.style.borderColor = 'gray'; //阻止瀏覽器默認(rèn)打開(kāi)文件的操作 ev.preventDefault(); var files = ev.dataTransfer.files; var len=files.length, i=0; var frag=document.createDocumentFragment(); //為了減少js修改dom樹(shù)的頻度,先創(chuàng)建一個(gè)fragment,然后在fragment里操作 var tr,time,size; var newForm=Dragfiles(); //獲取單例 var it=newForm.entries(); //創(chuàng)建一個(gè)迭代器,測(cè)試用 while(i'+time+' '+size+' 刪除 '; console.log(size+' '+time); frag.appendChild(tr); //添加文件到newForm newForm.append(files[i].name,files[i]); //console.log(it.next()); i++; } this.childNodes[1].childNodes[1].appendChild(frag); //為什么是‘1'?文檔里幾乎每一樣?xùn)|西都是一個(gè)節(jié)點(diǎn),甚至連空格和換行符都會(huì)被解釋成節(jié)點(diǎn)。而且都包含在childNodes屬性所返回的數(shù)組中.不同于jade模板 } function blink() { document.getElementById('content').style.borderColor = 'gray'; } //ajax上傳文件 function upload(){ if(document.getElementsByTagName('tbody')[0].hasChildNodes()==false){ document.getElementById('content').style.borderColor = 'red'; setTimeout(blink,200); return false; } var data=Dragfiles(); //獲取formData $.ajax({ url: 'upload', type: 'POST', data: data, async: true, cache: false, contentType: false, processData: false, success: function (data) { alert('succeed!') //可以替換為自己的方法 closeModal(); data.deleteAll(); //清空f(shuō)ormData $('.tbody').empty(); //清空列表 }, error: function (returndata) { alert('failed!') //可以替換為自己的方法 } }); } // 用事件委托的方法為‘刪除'添加點(diǎn)擊事件,使用jquery中的on方法 $(".tbody").on('click','tr td:last-child',function(){ //刪除拖拽框已有的文件 var temp=Dragfiles(); var key=$(this).prev().prev().prev().text(); console.log(key); temp.delete(key); $(this).parent().remove(); }); //清空所有內(nèi)容 function clearAll(){ if(document.getElementsByTagName('tbody')[0].hasChildNodes()==false){ document.getElementById('content').style.borderColor = 'red'; setTimeout(blink,300); return false; } var data=Dragfiles(); data.deleteAll(); //清空f(shuō)ormData //$('.tbody').empty(); 等同于以下方法 document.getElementsByTagName('tbody')[0].innerHTML=''; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。