閒來無事,忽然想用原生來實現圖片的多圖上傳。javascript
一、上傳時能夠選擇多圖css
二、上傳以後縮略圖下過圖以下:html
三、點擊縮略圖,大圖展現當前所點擊的圖片,並能夠左右滑動查看其它的縮略圖對應的大圖。效果以下:html5
四、點擊刪除,彈出是否要刪除的彈框,點擊肯定後,刪除。效果圖以下:java
一、限制圖片的張數(4張)web
二、限制單個圖片的大小(1M)bootstrap
三、支持拖拽上傳api
四、上傳後顯示小圖預覽瀏覽器
五、點擊小圖進行大圖預覽app
六、實現agax上傳
一、因爲時間緣由,頁面佈局依賴於bootstrap
二、滾動插件用的swiper.js
三、彈框差價layer
4-一、縮略效果圖以下:
4-二、縮略圖的HTML代碼以下:
<form action="post" class="vaildform fmreset" enctype="multipartform-data"> <div class="form-group clearfix"> <label class="col-lg-2 col-md-2 col-sm-2 control-label" for="userName">圖冊</label> <div class="col-lg-10 col-md-10 col-sm-10 clearfix"> <!-- 圖片展現 --> <div class="atlas-container pull-left clearfix"> <div class="atlas-content"> <i class="js-del-img fm-icon ion-close-circled"></i> <div class="img-container"> <img src=/assets/images/logbg.png alt="圖冊" /> </div> </div> </div> <!-- 圖片上傳 --> <div class="upload-container pull-left"> <input id="fileUpload" type="file" name="" value="" multiple accept="image/png,image/gif,image/jpg,image/jpeg" /> <div class="fileUpload"></div> </div> </div> </div> <div class="form-group clearfix"> <label class="col-lg-2 col-md-2 col-sm-2 control-label" for="userName"></label> <div class="col-lg-10 col-md-10 col-sm-10"> <a id="js-submit" href="javascript:;" class="btn btn-primary">提交</a> </div> </div> </form>
咱們知道form表單要實現文件上傳 form標籤上必須有「enctype="multipartform-data"」這個屬性,上傳類型爲post,實現文件上傳的空間就是
<input id="fileUpload" type="file" name="" value=""/>
一、這是一句圖片文件上傳的控件,可咱們想要實現圖片上傳,就要限制文件的類型,使用屬性「accept」,accept="image/png,image/gif,image/jpg,image/jpeg",這句話用來限制圖片的類型,這裏列舉的類型很少,你們能夠根據本身的須要調整。固然你也能夠寫成「accept="image/*"」,不過這樣寫的話,點擊上傳,出現選擇文件的框會出現很慢。
二、有的同窗會發現,這樣寫只能一次上傳一張圖片,你們別急,當咱們給這個控件加上「multiple 」這個屬性後就能實現多圖上傳啦。詳細上傳控件以下:
<input id="fileUpload" type="file" name="" value="" multiple accept="image/png,image/gif,image/jpg,image/jpeg"/>
4-三、縮略圖的css佈局以下:
// 圖冊容器 .atlas-container{} .atlas-container .atlas-content{ margin-left:15px; float: left; position: relative; } .atlas-container .atlas-content .img-container{ width:115px; height:115px; background:@white; padding:5px; border: 1px solid #e5e5e5; border-radius: 3px; display: -webkit-box; /* 老版本語法: Safari, iOS, Android browser, older WebKit browsers. */ display: -moz-box; /* 老版本語法: Firefox (buggy) */ display: -ms-flexbox; /* 混合版本語法: IE 10 */ display: -webkit-flex; /* 新版本語法: Chrome 21+ */ display: flex; /* 新版本語法: Opera 12.1, Firefox 22+ */ -webkit-box-pack: center; /*子元素水平居中*/ -moz-justify-content: center; /*子元素水平居中*/ -webkit-justify-content: center;/*子元素水平居中*/ justify-content: center; /*子元素水平居中*/ -webkit-box-align: center;/*子元素交叉軸對齊方式*/ -moz-align-items: center;/*子元素交叉軸對齊方式*/ -webkit-align-items: center;/*子元素交叉軸對齊方式*/ align-items: center;/*子元素交叉軸對齊方式*/ cursor:pointer; position:relative; } .atlas-container .atlas-content .img-container img{ max-width:100%; max-height:100%; display:block; } .atlas-container .atlas-content .fm-icon{ cursor:pointer; font-size: 16px; right: -11px; top: -14px; position: absolute; color: rgba(0,0,0,.5); z-index:2; } .atlas-container .atlas-content .fm-icon:hover{color:rgba(0,0,0,.7);} // 上傳容器 .upload-container{ width:120px; height:120px; margin-left:15px; overflow:hidden; position: relative; } #fileUpload{ width:100%; height:100%; top:0; left:0; right:0; bottom:0; opacity:0; cursor:pointer; position:absolute; z-index:2; } .upload-container .fileUpload{ width:100%; height:100%; cursor:pointer; background:@white; border: 1px solid #e5e5e5; border-radius: 3px; -o-border-radius: 3px; -moz-border-radius: 3px; -webkit-border-radius: 3px; position: relative; } .upload-container .fileUpload:before{ content:""; width:32px; height:2px; background:#cbcbcb; top:50%; left:50%; margin-left:-16px; margin-top:-1px; position: absolute; } .upload-container .fileUpload:after{ content:""; width:2px; height:32px; background:#cbcbcb; top:50%; left:50%; margin-left:-1px; margin-top:-16px; position: absolute; }
4-四、大圖HTML佈局以下:
<div class="picture-preview"> <div class="swiper-container"> <!-- 輪播盒子 --> <div class="swiper-wrapper"> </div> <!-- 前進後退按鈕 --> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> <!-- 分頁器 --> <div class="swiper-pagination"></div> </div> </div>
4-五、大圖對應的css樣式以下:
// 大圖預覽 .picture-preview{display:none;max-height:100%;} .picture-preview .preview-container{ display: -webkit-box; /* 老版本語法: Safari, iOS, Android browser, older WebKit browsers. */ display: -moz-box; /* 老版本語法: Firefox (buggy) */ display: -ms-flexbox; /* 混合版本語法: IE 10 */ display: -webkit-flex; /* 新版本語法: Chrome 21+ */ display: flex; /* 新版本語法: Opera 12.1, Firefox 22+ */ -webkit-box-pack: center; /*子元素水平居中*/ -moz-justify-content: center; /*子元素水平居中*/ -webkit-justify-content: center;/*子元素水平居中*/ justify-content: center; /*子元素水平居中*/ -webkit-box-align: center;/*子元素交叉軸對齊方式*/ -moz-align-items: center;/*子元素交叉軸對齊方式*/ -webkit-align-items: center;/*子元素交叉軸對齊方式*/ align-items: center;/*子元素交叉軸對齊方式*/ height:100%; } .picture-preview .preview-container img{ max-width:100%; max-height:100%; display:block; }
隨着和html5的普及,file的各類API愈來愈強大,HTML5 file 的API返回了不少有用的圖片信息,咱們能夠利用這些信息來實現圖片的預覽效果。
一、咱們能夠在控制檯上打印出咱們要上傳的圖片信息:
裏面包含「length」->當前上傳的圖片個數;"lastModified"->最後的更改;「name」->圖片的名字;"size"->圖片的大小,"type"->圖片的類型
5-1,圖片上傳功能
一、縮略圖上傳
imgUpload:function(){ var fileUpload = document.getElementById("fileUpload"); //獲得圖片的上傳控件 fileUpload.addEventListener('change',uploadFile,false); // 監聽上傳控件 var length=0; // 監聽圖片的個數 var size=0; // 監聽單個圖片的大小 var url=''; // 用來保存圖片的位置 var atlasContent = $('.atlas-container').find('.atlas-content'); if(atlasContent){ length = atlasContent.length; mainCtrl.picInit(); // 初始化大圖容器 } // 圖片上傳事件 function uploadFile(){ var files = this.files; // 獲得files var lengthN =parseInt(files.length); // 保存當前上傳的個數 length+=lengthN; // 圖片個數 if(length>4){ // 圖片上傳超過四個 layer.msg('圖片個數不能超過4個',function(){ fileUpload.value=''; // 清空當前的上傳控件, length=length-lengthN; // 圖片未添加,length回到原先的狀態 }); return; // 終止程序 } for(var i=0;i<lengthN;i++){ // 遍歷當前的上傳的個數, size= files[i].size; // 獲得單個圖片的大小 if(size>1024*1024){ // 單個圖片大於1M layer.msg(files[i].name+'這張圖片大於1M',function(){ // 提示哪張圖片的大小超出1M fileUpload.value=''; // 清空當前的上傳控件, length=length-lengthN; // 圖片未添加,length回到原先的狀態 }); return; }else{ var html=''; // 縮略圖 var bigHtml=''; // 大圖 // 終止程序 url = window.URL.createObjectURL(files[i]); // 方法會根據傳入的參數建立一個指向該參數對象的URL. 這個URL的生命僅存在於它被建立的這個文檔裏. 新的對象URL指向執行的File對象或者是Blob對象. html+='<div class="atlas-content">'; html+='<i class="js-del-img fm-icon ion-close-circled"></i>'; html+='<div class="img-container" data-name="'+files[i].name+'">'; html+='<img src='+url+' alt="圖冊"/>'; html+='</div>'; html+='</div>'; bigHtml+='<div class="swiper-slide">'; bigHtml+='<div class="preview-container">'; bigHtml+='<img src='+url+' alt="圖冊"/>'; bigHtml+='</div>'; bigHtml+='</div>'; $('.atlas-container').append(html); // 將縮略圖片寫入 $('.picture-preview .swiper-wrapper').append(bigHtml); // 將大圖寫入 } } if(length===4){ $('.upload-container').hide(); // 圖片上傳控件隱藏 } // 刪除圖片 $('.atlas-container').on('click','.js-del-img',function(){ var index = $(this).index(); var _this = this; var picName = $(this).next('.img-container').data('name'); layer.confirm('肯定刪除 '+picName+' 這張圖片?', { btn: ['肯定','取消'] //按鈕 }, function(){ $(_this).parent('.atlas-content').remove(); // 縮列圖刪除 mainCtrl.picInit(); // 從新渲染 length--; if(length<0){length=0;} $('.upload-container').show(); // 圖片上傳控件顯示 layer.msg('刪除成功', {icon: 1,time:1000}); }, function(index){ layer.close(index); } ); }) }; },
// 大圖初始化 picInit:function(){ var thumbnailObj = $('.atlas-container').find('.atlas-content'); var picLength = thumbnailObj.length; var picHtml=''; var thumbnailImgSrc=''; for(var i=0;i<picLength;i++){ thumbnailImgSrc = thumbnailObj.eq(i).find('img').attr('src'); picHtml+='<div class="swiper-slide">'; picHtml+='<div class="preview-container">'; picHtml+='<img src='+thumbnailImgSrc+' alt="圖冊"/>'; picHtml+='</div>'; picHtml+='</div>'; } $('.picture-preview .swiper-wrapper').empty().append(picHtml); },
注意:因爲大圖和小圖不在同一個容器裏,因此在縮略圖改變的時候,要及時更新大圖的容器
二、點擊縮略圖初始化swiper插件(注意:必定要在layer的success函數裏初始化,否則容易swiper比layer初始化快,swiper就不起做用了)
picturePreview:function(){ $('.atlas-container').on('click','.img-container',function(){ var key = $(this).parent('.atlas-content').index(); // 保存當前小圖的索引值 layer.open({ type: 1, shade: true, shadeClose:true, area: ['100%', '100%'], scrollbar:false, title: ['相冊大圖預覽', 'font-size:22px;text-align:center'], content: $('.picture-preview'), //捕獲的元素,注意:最好該指定的元素要存放在body最外層,不然可能被其它的相對元素所影響 success:function(){ mainCtrl.pictureCarousel(key); }, cancel: function(index){ layer.close(index); } }); }); },
三、swiper滾動初始化
pictureCarousel:function(key){ $('.picture-preview .swiper-container .swiper-wrapper').height($(window).height()-48); // 42是彈框標題的高度 $(window).resize(function(){ // 監聽瀏覽器大小的變化 $('.picture-preview .swiper-container .swiper-wrapper').height($(window).height()-48); // 42是彈框標題的高度 }) var picSwiper = new Swiper('.picture-preview .swiper-container', { pagination : '.picture-preview .swiper-pagination', initialSlide:key, // 起始頁設置 slidesPerView: 1, // 只顯示1個 paginationClickable:true, // 點擊分頁器的指示點分頁器會控制Swiper切換 autoplay: 2000, // 可選選項,自動滑動 autoplayDisableOnInteraction:false, // 用戶操做後還能夠自動切換 grabCursor:true, // 小手形狀 paginationClickable:true, // 分頁器 lazyLoading:true, // 懶加載 loop:true, // 循環 // prevButton:'.picture-preview .swiper-button-prev', // nextButton:'.picture-preview .swiper-button-next', }); $('.picture-preview .swiper-container').hover(function(){ picSwiper.stopAutoplay(); // 鼠標移入禁止自動切換 },function(){ picSwiper.startAutoplay(); // 鼠標移出開啓自動切換 }) $('.picture-preview .swiper-button-prev').click(function(){ // 上一頁 picSwiper.slidePrev(); }) $('.picture-preview .swiper-button-next').click(function(){ // 下一頁 picSwiper.slideNext(); }) },
後續:
4-5,拖拽上傳
4-六、圖片編輯剪切
4-七、圖片編輯旋轉
4-八、圖片編輯大小
4-九、顯示圖片上傳進度
4-十、終止、開始圖片上傳