1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| ~(function(win){ var htmls = '<input type="file" name="" id="" class="imgFiles" style="display: none" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" multiple>'+ '<div class="header">'+ ' <span class="imgTitle">'+ ' 点击添加'+ ' </span>'+ ' <span class="imgClick">'+ ' </span>'+ ' <span class="imgcontent">'+ ' 请上传'+ ' </span>'+ '</div>'+ '<div class="imgAll">'+ ' <ul>'+ ' </ul>'+ '</div>'; var ImgUploadeFiles = function(obj,fn){ var _this = this; this.bom = document.querySelector(obj); if(fn) fn.call(_this); this.ready();
}; ImgUploadeFiles.prototype = { init : function(o){ this.MAX = o.MAX || 5; this.callback = o.callback; this.MW = o.MW || 10000; this.MH = o.MH || 10000; }, ready : function(){ var _self = this; this.dom = document.createElement('div'); this.dom.className = 'imgFileUploade'; this.dom.innerHTML = htmls; this.bom.appendChild(this.dom); this.files = this.bom.querySelector('.imgFiles'); this.fileClick = this.bom.querySelector('.imgClick'); this.fileBtn(this.fileClick,this.files); this.imgcontent = this.bom.querySelector('.imgcontent'); this.imgcontent.innerHTML = '限制上传 <b style="color:red">'+this.MAX+'</b>张 '+_self.MW+' * '+_self.MH+'像素的图片'; }, fileBtn : function(c,f){ var _self = this; var _imgAll = $(c).parent().parent().find('.imgAll ul'); $(c).off().on('click',function(){ $(f).click();
$(f).off().on('change',function(){ var _this = this; _private.startUploadImg(_imgAll,_this.files,_self.MAX,_self.callback,_self.MW,_self.MH); }); }); } }; var _dataArr = []; var _private = { startUploadImg : function(o,files,MAX,callback,W,H){ _dataArr.length = 0; var _this = this; var fileImgArr = []; if(files.length > MAX ){ alert('不能大于'+MAX+'张'); return false; }; var lens = $(o).find('li').length ; if(lens >= MAX ){ alert('不能大于'+MAX+'张'); return false; };
for(var i=0,file;file=files[i++];){
var reader = new FileReader(); reader.onload = (function(file){ return function(ev){ var image = new Image(); image.onload=function(){ var width = image.width; var height = image.height; fileImgArr.push({ fileSrc : ev.target.result, fileName : file.name, fileSize : file.size, height : height, width : width }); }; image.src= ev.target.result;
}; })(file); reader.readAsDataURL(file); } var imgTimeSlice = _this.timeChunk(fileImgArr,function(file){ if(file.width > W || file.height > H){ alert('图片不能大于'+W+'*'+H+'像素'); return false; } var up = new ImgFileupload(o,file.fileName,file.fileSrc,file.fileSize,callback); up.init(); },1); imgTimeSlice(); }, timeChunk : function(arr, fn, count) { var obj, t; var len = arr.length; var start = function() { for (var i = 0; i < Math.min(count || 1, arr.length); i++) { var obj = arr.shift(); fn(obj) } }; return function() { t = setInterval(function() { if (arr.length === 0) { return clearInterval(t); } start(); },200) } } };
var ImgFileupload = function(b,imgName,imgSrc,imgSize,callback){ this.b = b; this.imgName = imgName; this.imgSize = imgSize; this.imgSrc = imgSrc; this.callback = callback; }; var _delId = 1; ImgFileupload.prototype.init =function() { _delId++; var _self = this; this.dom = document.createElement('li'); this.dom.innerHTML = ' <img src="/libs/img-file-update/img/login.gif" alt="" data-src="'+this.imgSrc +'" class="imsg">'+ ' <i class="delImg">'+ ' X'+ ' </i>'; $(this.dom).attr({'data-delId':_delId,'data-delName':this.imgName}); $(this.b).append(this.dom); var _Img = new Image(); _Img.src = $(this.dom).find('img').attr('data-src'); _Img.onload = function(){ $(_self.dom).find('img').attr('src',_Img.src); } _dataArr.push({'delId' :_delId,src : this.imgSrc}); _self.callback(_dataArr); var _delAll = $(this.b).find('.delImg'); for(var i=0;i<_delAll.length;i++){ $(_delAll[i]).off().on('click',function(){ $(this).parent().fadeOut('slow',function(){ $(this).remove(); }); var _deid = $(this).parent().attr('data-delId'); for(var n=0;n<_dataArr.length;n++){ if(_dataArr[n].delId == _deid){ _dataArr.splice(n,1); } } _self.callback(_dataArr) }); }; var _Imgpreview = $(this.b).find('img'); for(var k=0;k<_Imgpreview.length; k++){ $(_Imgpreview[k]).off().on('click',function(){ console.log($(this).attr('src')) }) } }
win.ImgUploadeFiles = ImgUploadeFiles; })(window);
|