input file上传图片预览其实很简单,只是没做过的感觉很神奇,今天我就扒下她神秘的面纱,其实原理真的非常非常非常非常非常非常简单!
点击红框是加载显示图片,X号删除,蓝框是自动在后面添加添加图片框,所有的都是原创,代码其实可以更加精简,看终结版文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <style type="text/css"> .imgbox,.imgbox1 { float: left; margin-right: 20px; margin-top: 20px; position: relative; width: 182px; height: 142px; border: 1px solid red; overflow: hidden; } .imgbox1{border: 1px solid blue; } .imgnum{ left: 0px; top: 0px; margin: 0px; padding: 0px; } .imgnum input,.imgnum1 input { position: absolute; width: 182px; height: 142px; opacity: 0; } .imgnum img,.imgnum1 img { width: 100%; height: 100%; } .close, .close1 { color: red; position: absolute; left: 170px; top: 0px; display: none; } </style> </head> <body> <div id="img"> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div><div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> <div class="imgbox1"> <div class="imgnum"> <input type="file" class="filepath1" /> <span class="close1">X</span> <img src="btn.png" class="img11" /> <img src="" class="img22" /> </div> </div> </div> </body> <script type="text/javascript"> $(function() { $(".filepath").on("change",function() { alert($('.imgbox').length); var srcs = getObjectURL(this.files[0]); //获取路径 $(this).nextAll(".img1").hide(); //this指的是input $(this).nextAll(".img2").show(); //fireBUg查看第二次换图片不起做用 $(this).nextAll('.close').show(); //this指的是input $(this).nextAll(".img2").attr("src",srcs); //this指的是input $(this).val(''); //必须制空 $(".close").on("click",function() { $(this).hide(); //this指的是span $(this).nextAll(".img2").hide(); $(this).nextAll(".img1").show(); }) }) }) function getObjectURL(file) { var url = null; if (window.createObjectURL != undefined) { url = window.createObjectURL(file) } else if (window.URL != undefined) { url = window.URL.createObjectURL(file) } else if (window.webkitURL != undefined) { url = window.webkitURL.createObjectURL(file) } return url }; $(function() { $("#img").on("change",".filepath1",function() { //alert($('.imgbox1').length); var srcs = getObjectURL(this.files[0]); //获取路径 alert(srcs); //this指的是input /* $(this).nextAll(".img22").attr("src",srcs); //this指的是input $(this).nextAll(".img22").show(); //fireBUg查看第二次换图片不起做用*/ var htmlImg='<div class="imgbox1">'+ '<div class="imgnum1">'+ '<input type="file" class="filepath1" />'+ '<span class="close1">X</span>'+ '<img src="btn.png" class="img11" />'+ '<img src="'+srcs+'" class="img22" />'+ '</div>'+ '</div>'; $(this).parent().parent().before(htmlImg); $(this).val(''); //必须制空 $(this).parent().parent().prev().find(".img11").hide(); //this指的是input $(this).parent().parent().prev().find('.close1').show(); $(".close1").on("click",function() { $(this).hide(); //this指的是span $(this).nextAll(".img22").hide(); $(this).nextAll(".img11").show(); if($('.imgbox1').length>1){ $(this).parent().parent().remove(); } }) }) }) </script> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $(function() { $(".filepath").on("change",function() { var srcs = getObjectURL(this.files[0]); $(".mailbox_shc > img").attr("src",srcs); }) }) function getObjectURL(file) { var url = null; if (window.createObjectURL != undefined) { url = window.createObjectURL(file) } else if (window.URL != undefined) { url = window.URL.createObjectURL(file) } else if (window.webkitURL != undefined) { url = window.webkitURL.createObjectURL(file) } return url }; |
1 2 3 4 5 | <input type="file" accept="video/*;capture=camcorder"> <input type="file" accept="audio/*;capture=microphone"> <input type="file" accept="image/*;capture=camera">直接调用相机(测试安卓可以,iphone还是有相册) <input type="file" accept="image/*" />调用相机 图片或者相册 <input type="file" multiple accept="image/*" />调用相册 |
上一篇:苹果手机Safari浏览器iOS 2.0+长按图片储存
下一篇:离开网页弹窗函数onbeforeunload与onunload
支付宝扫一扫打赏
微信扫一扫打赏
共 0 条评论关于"前端通过input file原生简单实现上传图片预览功能"
最新评论