当前位置:Gxlcms > JavaScript > weui框架实现上传、预览和删除图片功能代码

weui框架实现上传、预览和删除图片功能代码

时间:2021-07-01 10:21:17 帮助过:39人阅读

jQuery WeUI 是专为微信公众账号开发而设计的一个简洁而强大的UI库,包含全部WeUI官方的CSS组件,并且额外提供了大量的拓展组件,丰富的组件库可以极大减少前端开发时间。

jQuery WeUI 的最大特点是它只提供UI组件,并不会对项目所使用的框架和其他库有任何的限制,几乎可以在任何环境下使用。无论你的项目是基于jQuery,还是 React, Angular, Vue, 你都会发现 jQuery WeUI 能非常方便的和他们结合使用。既是你的项目是一个有很悠久历史的老项目,也几乎可以做到拿来即用。

weui框架暂时只有css文件,并没有js文件实现其功能,我在其html+css后面增加了js实现其功能,为大家提供方便,也为自己保存记录。

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="../css/mui.min.css" rel="external nofollow" rel="stylesheet" />
    <link rel="stylesheet" href="weui.min.css" rel="external nofollow" rel="external nofollow" />
    <link rel="stylesheet" href="jquery-weui.css" rel="external nofollow" rel="external nofollow" />
  </head>
  <body>
    <div class="weui-gallery" id="gallery">
      <span class="weui-gallery__img" id="galleryImg"></span>
      <div class="weui-gallery__opr">
        <a href="javascript:" rel="external nofollow" 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 class="weui-uploader__info">0/2</div>
            </div>
            <div class="weui-uploader__bd">
              <ul class="weui-uploader__files" id="uploaderFiles">
                <li class="weui-uploader__file" style="background-image:url(../images/applogo180x180.png)"></li>
                <li class="weui-uploader__file" style="background-image:url(../images/applogo180x180.png)"></li>
                <li class="weui-uploader__file" style="background-image:url(../images/applogo180x180.png)"></li>
                <li class="weui-uploader__file weui-uploader__file_status" style="background-image:url(../images/applogo180x180.png)">
                  <div class="weui-uploader__file-content">
                    <i class="weui-icon-warn"></i>
                  </div>
                </li>
                <li class="weui-uploader__file weui-uploader__file_status" style="background-image:url(../images/applogo180x180.png)">
                  <div class="weui-uploader__file-content">50%</div>
                </li>
              </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>
    <script src="../js/mui.min.js"></script>
    <script type="text/javascript" src="jquery-2.1.4.js"></script>
    <script type="text/javascript" src="jquery-weui.js"></script>
    <script type="text/javascript">
      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() { //这部分刚才放错地方了,放到$(function(){})外面去了
          console.log('删除');
          $uploaderFiles.find("li").eq(index).remove();
        });
      });
    </script>
  </body>
</html>

补充说明上述代码中引用以下的这四个文件可以到http://jqweui.com这个weui的官网去下载

<link rel="stylesheet" href="weui.min.css" rel="external nofollow" rel="external nofollow" />
 <link rel="stylesheet" href="jquery-weui.css" rel="external nofollow" rel="external nofollow" />
 <script type="text/javascript" src="jquery-2.1.4.js">
 </script><script type="text/javascript" src="jquery-weui.js"></script>

总结

以上所述是小编给大家介绍的weui框架实现上传、预览和删除图片功能代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

人气教程排行