jQuery WeUI 是专为微信公众账号开发而设计的一个框架,jQuery WeUI的官网:http://jqweui.com/
需求:需要在微信公众号网页添加上传图片功能
技术选型:实现上传图片功能可选百度的WebUploader、饿了么的Element和微信的jQuery WeUI,WebUploader的缺点是自定义UI困难,自定义上传过程困难,Element的缺点是Web 控件,与手机端样式不符,而jQuery WeUI专门为微信公众号做的框架,基本不会出现问题,使用起来比较简单。最后当然就选微信的jQuery WeUI
HTML部分:
- <!--图片上传-->
- <div class="weui-gallery" id="gallery">
- <span class="weui-gallery__img" id="galleryImg"></span>
- <div class="weui-gallery__opr">
- <a href="javascript:" class="weui-gallery__del">
- <i class="weui-icon-delete weui-icon_gallery-delete"></i>
- </a>
- </div>
- </div>
- <div class="weui-cells weui-cells_form">
- <div class="weui-cell">
- <div class="weui-cell__bd">
- <div class="weui-uploader">
- <div class="weui-uploader__hd">
- <p class="weui-uploader__title">图片上传</p>
- </div>
- <div class="weui-uploader__bd">
- <ul class="weui-uploader__files" id="uploaderFiles">
- </ul>
- <div class="weui-uploader__input-box">
- <input id="uploaderInput" class="weui-uploader__input zjxfjs_file" type="file" accept="image/*" multiple="">
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
JS部分:
- <script type="text/javascript" src="../js/mui.min.js"></script>
- <script type="text/javascript" src="../js/jquery.min.js"></script>
- <script src="../js/jquery-weui.min.js"></script>
- <script>
- mui.init();
- $(function() {
- var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#)"></li>',
- $gallery = $("#gallery"),
- $galleryImg = $("#galleryImg"),
- $uploaderInput = $("#uploaderInput"),
- $uploaderFiles = $("#uploaderFiles");
- $uploaderInput.on("change", function(e) {
- var src, url = window.URL || window.webkitURL || window.mozURL,
- files = e.target.files;
- for(var i = 0, len = files.length; i < len; ++i) {
- var file = files[i];
- if(url) {
- src = url.createObjectURL(file);
- } else {
- src = e.target.result;
- }
- $uploaderFiles.append($(tmpl.replace('#url#', src)));
- }
- });
- var index; //第几张图片
- $uploaderFiles.on("click", "li", function() {
- index = $(this).index();
- $galleryImg.attr("style", this.getAttribute("style"));
- $gallery.fadeIn(100);
- });
- $gallery.on("click", function() {
- $gallery.fadeOut(100);
- });
- //删除图片
- $(".weui-gallery__del").click(function() {
- $uploaderFiles.find("li").eq(index).remove();
- });
- });
- </script>
CSS和JS文件可以去官网下载
- <link rel="stylesheet" href="../css/mui.min.css">
- <link rel="stylesheet" href="../css/weui.min.css"/>
- <link rel="stylesheet" href="../css/jquery-weui.min.css"/>
- <script type="text/javascript" src="../js/mui.min.js"></script>
- <script type="text/javascript" src="../js/jquery.min.js"></script>
- <script src="../js/jquery-weui.min.js"></script>
效果图:
点击加号,上传图片
点击图片,可以进行预览、删除