Home Web Front-end JS Tutorial Javascript implements input file upload image preview effect

Javascript implements input file upload image preview effect

Dec 28, 2016 pm 04:48 PM

The example of this article introduces the detailed code of javascript to realize the preview effect of input file upload image, and shares it with everyone for your reference. The specific content is as follows

Running effect diagram:

javascript实现input file上传图片预览效果

Specific implementation code:

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

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

<!DOCTYPE html>

<html>

  

<head>

 <meta charset="utf-8">

 <title></title>

 <script type="text/javascript" src="jquery-1.11.1.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($(&#39;.imgbox&#39;).length);

   var srcs = getObjectURL(this.files[0]); //获取路径

   $(this).nextAll(".img1").hide(); //this指的是input

   $(this).nextAll(".img2").show(); //fireBUg查看第二次换图片不起做用

   $(this).nextAll(&#39;.close&#39;).show(); //this指的是input

   $(this).nextAll(".img2").attr("src",srcs); //this指的是input

   $(this).val(&#39;&#39;); //必须制空

   $(".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($(&#39;.imgbox1&#39;).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=&#39;<div class="imgbox1">&#39;+

     &#39;<div class="imgnum1">&#39;+

     &#39;<input type="file" class="filepath1" />&#39;+

     &#39;<span class="close1">X</span>&#39;+

     &#39;<img src="btn.png" class="img11" />&#39;+

     &#39;<img src="&#39;+srcs+&#39;" class="img22" />&#39;+

     &#39;</div>&#39;+

     &#39;</div>&#39;;

  

   $(this).parent().parent().before(htmlImg);

   $(this).val(&#39;&#39;); //必须制空

   $(this).parent().parent().prev().find(".img11").hide(); //this指的是input

   $(this).parent().parent().prev().find(&#39;.close1&#39;).show();

  

   $(".close1").on("click",function() {

    $(this).hide();  //this指的是span

    $(this).nextAll(".img22").hide();

    $(this).nextAll(".img11").show();

    if($(&#39;.imgbox1&#39;).length>1){

     $(this).parent().parent().remove();

    }

  

   })

  })

 })

  

</script>

  

</html>

Copy after login

I hope this article will be helpful for everyone to learn javascript programming.

For more javascript implementation of input file upload image preview effects, please pay attention to 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1228
24
Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript Engines: Comparing Implementations JavaScript Engines: Comparing Implementations Apr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript: Exploring the Versatility of a Web Language JavaScript: Exploring the Versatility of a Web Language Apr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

How do I install JavaScript? How do I install JavaScript? Apr 05, 2025 am 12:16 AM

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.

See all articles