使用jQuery在上传图片之前实现缩略图预览
转载
使用jQuery在上传图片之前实现缩略图预览
jQuery代码
01 | $( "#uploadImage" ).on( "change" , function (){ |
03 | var files = !! this .files ? this .files : []; |
06 | if (!files.length || !window.FileReader) return ; |
09 | if (/^image/.test( files[0].type)){ |
12 | var reader = new FileReader(); |
15 | reader.readAsDataURL(files[0]); |
18 | reader.onloadend = function (){ |
20 | $( "#uploadPreview" ).css( "background-image" , "url(" + this .result+ ")" ); |
以下是HTML代码,包含一个显示缩略图的div和一个file input field。
HTML代码
1 | < div id = "uploadPreview" ></ div > |
2 | < input id = "uploadImage" type = "file" name = "photoimage" class = "fimg1" onchange = "PreviewImage();" /> |
为我们的缩略图设置CSSwa 。
CSS代码
1 | <strong>#uploadPreview { |
2 | width : 168px ; |
3 | height : 168px ; |
4 | background-position : center center ; |
5 | background- size : cover; |
6 | border : 4px solid #fff ; |
7 | -webkit-box-shadow: 0 0 1px 1px rgba( 0 , 0 , 0 , . 3 ); |
8 | display : inline- block ;</strong> |
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。