Native JS for instant preview code

Source: Internet
Author: User

<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> Upload Images </title>
<style>
body,div,img{
margin:0 Auto;
padding:0;
}
html{
width:100%;
height:100%;
}
body{
line-height:20px;
Font-family: "Microsoft Jas Black";
width:1000px;
height:100%;
Background-color: #000;

}
. upload-img{
position:relative;
Text-align:center;
line-height:300px;
Float:left;
margin-top:100px;
Background-color: #fff;
Color: #333;
font-size:30px;
width:300px;
height:300px;
margin-left:15px;
margin-right:15px;
}
. upload-img. imgs{
Position:absolute;
left:0;
top:0;
width:100%;
height:100%;
}
. upload-img:hover{
Color: #f90;
}
. upload-img. btn{
width:100%;
height:100%;
opacity:0;
Filter:alpha (opacity=0);
Z-index:2;
Position:absolute;
left:0;
top:0;
Cursor:pointer;
}
</style>
<body>
<div class= "Upload-img" >
Upload image
<input type= "File" class= "btn" onchange= "uploadimg (this,1);"/>
</div>
<div class= "Upload-img" >
Upload image
<input type= "File" class= "btn" onchange= "uploadimg (this,2);"/>
</div>
<div class= "Upload-img" >
Upload image
<input type= "File" class= "btn" onchange= "uploadimg (this,3);"/>
</div>
<script>
Upload image
function Uploadimg (that,num) {
var Parentnode=that.parentnode;
var images=parentnode.getelementsbytagname ("img");
if (images.length==0) {
Createimg (Parentnode,num);
}
var Img=document.getelementbyid ("img" +num);
var f=that.files;
if (F&&f[0]) {
Checkedimg (THAT,F,IMG);
}else{
Compatible with IE
var Srcval=that.value;
if (!srcval) {
Alert ("You performed a cancel operation");
Img.setattribute ("src", "");
Srcval= "";
}else{
var houzui=srcval.substr (Srcval.lastindexof ("."))
. replace (".", "");
if (houzui== "jpg" | | houzui== "JPEG") {
Img.setattribute ("src", srcval);
}else{
Alert ("Please upload a picture with suffix jpg,jpeg format");
Img.setattribute ("src", "");
Srcval= "";
}
}

}
}
Create a picture
function Createimg (parentnode,num) {
var imgelement=document.createelement ("img");
Imgelement.classname= "IMGs";
Imgelement.id= "img" +num;
Parentnode.appendchild (imgelement);
}
Determine the type of picture, size
function Checkedimg (that,f,img) {
Type of picture
if (f[0].type== "Image/jpeg") {
Image size cannot exceed 3M
if (f[0].size/1024/1024<3) {
var Imgsrc=window. Url.createobjecturl (That.files[0]);
Img.setattribute ("src", imgsrc);
}else{
Alert ("Upload image size cannot exceed 3M");
Empty file fields
F=[];
Img.setattribute ("src", "");
return false;
}
}else{
Alert ("Please upload a picture with suffix jpg,jpeg format");
Empty file fields
F=[];
Img.setattribute ("src", "");
return false;
}
}
</script>
</body>

Native JS for instant preview code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.