Home > Web Front-end > JS Tutorial > body text

JS example code to implement multi-image preview upload

零下一度
Release: 2017-06-27 15:34:24
Original
1681 people have browsed it

这篇文章主要介绍了JS实现多张图片预览同步上传功能的相关资料,需要的朋友可以参考下

废话不多说了,直接给大家贴代码了,具体代码如下所示:


/**
 * Created by liujing on 2017/5/10.
 */
$(document).ready(function($) {
  function changef(which,bulk,name_n){
    var bulka = bulk;
    var thisid = which.attr("id");
    var f = which.prop ('files')[0];
    var filename = f.name;
    var mime = filename.toLowerCase().substr(filename.lastIndexOf("."));
    var liid = $("#"+thisid).closest("li").attr("id");
    var liidb = parseInt(liid)+1;
    if(mime==".jpg" || mime==".png"){
      var src = window.URL.createObjectURL(f);
      var name = $("#logo"+liid).val().split("\\").pop();
      $('#logotips'+liid).css({"background":'url('+src+')','backgroundSize':100+'%'});
      $('#up_flower'+liid).empty().append(name);
      $(&#39;#&#39;+liid).append(&#39;<a class="edit_text">✖</a>&#39;);
      bulka.append(&#39;<li class="uploading" id="&#39;+liidb+&#39;"><a class="a_a" id="logotips&#39;+liidb+&#39;"><input class="input_hide logo&#39;+liidb+&#39;" type="file" name="&#39;+name_n+&#39;" id="logo&#39;+liidb+&#39;"></a></li>&#39;);
      $(&#39;#logotips&#39;+liidb).css({"background":&#39;url("../../img/c_up.png")&#39;,&#39;backgroundSize&#39;:100+&#39;%&#39;});
    }
    $(&#39;.logo&#39;+liid).addClass("newc"+liid);
    $(&#39;.newc&#39;+liid).removeClass("logo"+liid);
    $(&#39;.newc&#39;+liid).on(&#39;change&#39;,function(){
      var files = $(this).prop(&#39;files&#39;)[0];
      var srcb = window.URL.createObjectURL(files);
      var liid = $("#"+thisid).closest("li").attr("id");
      var liidb = parseInt(liid)+1;
      var name = $("#logo"+liid).val().split("\\").pop();
      $(&#39;#logotips&#39;+liid).css({"background":&#39;url(&#39;+srcb+&#39;)&#39;,&#39;backgroundSize&#39;:100+&#39;%&#39;});
      $(&#39;#up_flower&#39;+liid).empty().append(name);
    })
    $(&#39;.edit_text&#39;).on(&#39;click&#39;,function(){
      $(this).parent().remove();
    })
  } 
  function changelast(which ,bulk){
    var bulka = bulk;
    var thisid = which.attr("id");
    var f = which.prop (&#39;files&#39;)[0];
    var filename = f.name;
    var mime = filename.toLowerCase().substr(filename.lastIndexOf("."));
    var liid = $("#"+thisid).closest("li").attr("id");
    var liidb = parseInt(liid)+1;
    if(mime==".jpg" || mime==".png"){
      var src = window.URL.createObjectURL(f);
      var name = $("#logo"+liid).val().split("\\").pop();
      $(&#39;#logotips&#39;+liid).css({"background":&#39;url(&#39;+src+&#39;)&#39;,&#39;backgroundSize&#39;:100+&#39;%&#39;});
      $(&#39;#up_flower&#39;+liid).empty().append(name);
      $(&#39;#&#39;+liid).append(&#39;<a class="edit_text">✖</a>&#39;);
    }
    $(&#39;.logo&#39;+liid).addClass("newc"+liid);
    $(&#39;.newc&#39;+liid).removeClass("logo"+liid);
    $(&#39;.newc&#39;+liid).on(&#39;change&#39;,function(){
      var files = $(this).prop(&#39;files&#39;)[0];
      var srcb = window.URL.createObjectURL(files);
      var liid = $("#"+thisid).closest("li").attr("id");
      var liidb = parseInt(liid)+1;
      var name = $("#logo"+liid).val().split("\\").pop();
      $(&#39;#logotips&#39;+liid).css({"background":&#39;url(&#39;+srcb+&#39;)&#39;,&#39;backgroundSize&#39;:100+&#39;%&#39;});
      $(&#39;#up_flower&#39;+liid).empty().append(name);
    })
    $(&#39;.edit_text&#39;).on(&#39;click&#39;,function(){
      $(this).parent().remove();
    })
  } 
    $(".logo1").one("change",function(){
      that = $(this); var bulk = $("#add_beo"); var name = $(this).attr(&#39;name&#39;); changef(that,bulk,name);
    });
    $("#add_beo").one("change",".logo2",function(){
      that = $(this); var bulk = $("#add_beo"); var name = $(this).attr(&#39;name&#39;); changef(that,bulk,name);
    });
    $("#add_beo").one("change",".logo3",function(){
      that = $(this); var bulk = $("#add_beo"); var name = $(this).attr(&#39;name&#39;); changef(that,bulk,name);
    });
    $("#add_beo").one("change",".logo4",function(){
      that = $(this); var bulk = $("#add_beo"); var name = $(this).attr(&#39;name&#39;); changef(that,bulk,name);
    });
    $("#add_beo").one("change",".logo5",function(){
      that = $(this); var bulk = $("#add_beo"); changelast(that , bulk);
    });
});
Copy after login

The above is the detailed content of JS example code to implement multi-image preview upload. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!