Home Web Front-end JS Tutorial JS example code to implement multi-image preview upload

JS example code to implement multi-image preview upload

Jun 27, 2017 pm 03:34 PM
javascript Synchronize Preview

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

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


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

/**

 * 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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solve the problem of playing headphones and speakers at the same time in win11 Solve the problem of playing headphones and speakers at the same time in win11 Jan 06, 2024 am 08:50 AM

Generally speaking, we only need to use one of the headphones or speakers at the same time. However, some friends have reported that in the win11 system, they encountered the problem of headphones and speakers sounding at the same time. In fact, we can turn it off in the realtek panel and it will be fine. , let’s take a look below. What should I do if my headphones and speakers sound together in win11? 1. First find and open the "Control Panel" on the desktop. 2. Enter the control panel, find and open "Hardware and Sound" 3. Then find the "Realtek High Definition" with a speaker icon. Audio Manager" 4. Select "Speakers" and click "Rear Panel" to enter the speaker settings. 5. After opening, we can see the device type. If you want to turn off the headphones, uncheck "Headphones".

One or more items in the folder you synced do not match Outlook error One or more items in the folder you synced do not match Outlook error Mar 18, 2024 am 09:46 AM

When you find that one or more items in your sync folder do not match the error message in Outlook, it may be because you updated or canceled meeting items. In this case, you will see an error message saying that your local version of the data conflicts with the remote copy. This situation usually happens in Outlook desktop application. One or more items in the folder you synced do not match. To resolve the conflict, open the projects and try the operation again. Fix One or more items in synced folders do not match Outlook error In Outlook desktop version, you may encounter issues when local calendar items conflict with the server copy. Fortunately, though, there are some simple ways to help

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Teach you how to synchronize the win10 clipboard with your mobile phone Teach you how to synchronize the win10 clipboard with your mobile phone Jan 06, 2024 am 09:18 AM

A very useful function of win10 clipboard is the cross-device cloud storage function, which is very useful and can help users copy and paste simultaneously on PC devices and mobile devices. The setting method is very simple, just set it on the clipboard in the system. Synchronize win10 clipboard to mobile phone 1. First click Start in the lower left corner to enter settings. 2. Then click "System". 3. Select "Clipboard" on the left. 4. Finally, click Login in "Cross-device synchronization" on the right, and then select your mobile phone.

See all articles