layui实现多图片上传并限制上传的图片数量实现多图片上传并限制上传的图片数量
废话不多说了,直接上代码吧!废话不多说了,直接上代码吧!
//给图片添加删除
function mouseChange() {
$(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
if (event.type === "mouseenter") {
//鼠标悬浮
$(this).children(".info").fadeIn("fast");
$(this).children(".handle").fadeIn("fast");
} else if (event.type === "mouseleave") {
//鼠标离开
$(this).children(".info").hide();
$(this).children(".handle").hide();
}
});
}
mouseChange();
//json的length
function getJsonLength(jsonData){
var jsonLength = 0;
for(var item in jsonData){
jsonLength++;
}
return jsonLength;
}
//多图片上传
var ImgList = $('#uploader-list'),uploadListIns =upload.render({
elem: '#chooseImg',
url: $("#projectUrl").val()+ '/img/imgUpload.do',
accept: 'images',
acceptMime: 'image/jpg,image/png,image/jpeg',
exts: 'jpg|png|jpeg',
size: 1024,
multiple: true,
auto: true,
choose: function(obj){
var files = obj.pushFile(); //将每次选择的文件追加到文件队列
var len = getJsonLength(files);
//读取本地文件
obj.preview(function (index, file, result) {
if (parseInt(len)+parseInt(coachPicsArray.length)-count > 6){
layer.msg("门店图片不能超过6张");
//遍历
$.each(files,function(_key){
var key = _key;
var value = files[_key];
if(_key == index)
{ //删除
delete files[_key];
}
});
}else {
var reader = new FileReader();
reader.onload = function (e) {
var image = new Image();
image.onload = function () {
var realWidth = image.width;
var realHeight = image.height;
var tr = $(['<div id="upload-' + index + '" class="file-iteme">' +
'<div class="removeIcon handle"> <i class="layui-icon" style="color: white ;margin-right: 40%"></i></div>' +
'<img style="color: white;width: 120px" "showBig(this)" src=' + result + ' id="img-' + index + '">' +
'</div>'].join(''));
//删除