转载:利用微信的weui框架上传、预览和删除图片

转载至:https://blog.csdn.net/sinat_36558867/article/details/79492843

jQuery WeUI 是专为微信公众账号开发而设计的一个框架,jQuery WeUI的官网:http://jqweui.com/

需求:需要在微信公众号网页添加上传图片功能

技术选型:实现上传图片功能可选百度的WebUploader、饿了么的Element和微信的jQuery WeUI,WebUploader的缺点是自定义UI困难,自定义上传过程困难,Element的缺点是Web 控件,与手机端样式不符,而jQuery WeUI专门为微信公众号做的框架,基本不会出现问题,使用起来比较简单。最后当然就选微信的jQuery WeUI

HTML部分:

[html]  view plain  copy
  1.   
  2.         <div class="weui-gallery" id="gallery">  
  3.             <span class="weui-gallery__img" id="galleryImg">span>  
  4.             <div class="weui-gallery__opr">  
  5.                 <a href="javascript:" class="weui-gallery__del">  
  6.                     <i class="weui-icon-delete weui-icon_gallery-delete">i>  
  7.                 a>  
  8.             div>  
  9.         div>  
  10.         <div class="weui-cells weui-cells_form">  
  11.             <div class="weui-cell">  
  12.                 <div class="weui-cell__bd">  
  13.                     <div class="weui-uploader">  
  14.                         <div class="weui-uploader__hd">  
  15.                             <p class="weui-uploader__title">图片上传p>  
  16.                         div>  
  17.                         <div class="weui-uploader__bd">  
  18.                             <ul class="weui-uploader__files" id="uploaderFiles">  
  19.                                   
  20.                             ul>  
  21.                             <div class="weui-uploader__input-box">  
  22.                                 <input id="uploaderInput" class="weui-uploader__input zjxfjs_file" type="file" accept="image/*" multiple="">  
  23.                             div>  
  24.                         div>  
  25.                     div>  
  26.                 div>  
  27.             div>  
  28.         div>  

JS部分:

[html]  view plain  copy
  1. <script type="text/javascript" src="../js/mui.min.js">script>  
  2. <script type="text/javascript" src="../js/jquery.min.js">script>  
  3. <script src="../js/jquery-weui.min.js">script>  
  4. <script>  
  5.     mui.init();  
  6.     $(function() {  
  7.         var tmpl = '
  8. ',  
  9.             $gallery = $("#gallery"),  
  10.             $galleryImg = $("#galleryImg"),  
  11.             $uploaderInput = $("#uploaderInput"),  
  12.             $uploaderFiles = $("#uploaderFiles");  
  13.   
  14.             $uploaderInput.on("change", function(e) {  
  15.                 var src, url = window.URL || window.webkitURL || window.mozURL,  
  16.                 files = e.target.files;  
  17.                 for(var i = 0len = files.length; i < len; ++i) {  
  18.                     var file = files[i];  
  19.   
  20.                     if(url) {  
  21.                         src = url.createObjectURL(file);  
  22.                     } else {  
  23.                         src = e.target.result;  
  24.                     }  
  25.   
  26.                     $uploaderFiles.append($(tmpl.replace('#url#', src)));  
  27.                 }  
  28.             });  
  29.         var index; //第几张图片  
  30.         $uploaderFiles.on("click", "li", function() {  
  31.             index = $(this).index();  
  32.             $galleryImg.attr("style", this.getAttribute("style"));  
  33.             $gallery.fadeIn(100);  
  34.         });  
  35.         $gallery.on("click", function() {  
  36.             $gallery.fadeOut(100);  
  37.         });  
  38.         //删除图片  
  39.         $(".weui-gallery__del").click(function() {  
  40.             $uploaderFiles.find("li").eq(index).remove();  
  41.         });  
  42.     });  
  43. script>  

CSS和JS文件可以去官网下载

[html]  view plain  copy
  1. <link rel="stylesheet" href="../css/mui.min.css">  
  2. <link rel="stylesheet" href="../css/weui.min.css"/>  
  3. <link rel="stylesheet" href="../css/jquery-weui.min.css"/>  
[html]  view plain  copy
  1.  <script type="text/javascript" src="../js/mui.min.js">script>  
  2.  <script type="text/javascript" src="../js/jquery.min.js">script>  
  3.  <script src="../js/jquery-weui.min.js">script>  

效果图:

转载:利用微信的weui框架上传、预览和删除图片_第1张图片

点击加号,上传图片

转载:利用微信的weui框架上传、预览和删除图片_第2张图片

点击图片,可以进行预览、删除

转载:利用微信的weui框架上传、预览和删除图片_第3张图片

你可能感兴趣的:(js)