weui框架实现上传、预览和删除图片功能代码
程序员文章站
2022-09-08 21:58:39
jquery weui 是专为微信公众账号开发而设计的一个简洁而强大的ui库,包含全部weui官方的css组件,并且额外提供了大量的拓展组件,丰富的组件库可以极大减少前端开...
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>
补充说明上述代码中引用以下的这四个文件可以到这个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框架实现上传、预览和删除图片功能代码,希望对大家有所帮助