原生js實現即時預覽代碼

來源:互聯網
上載者:User

標籤:relative   oat   ast   rcv   indexof   代碼   position   ret   str   

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>上傳圖片</title>
<style>
body,div,img{
margin:0 auto;
padding: 0;
}
html{
width:100%;
height:100%;
}
body{
line-height: 20px;
font-family: "微軟雅黑";
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>
</head>
<body>
<div class="upload-img">
上傳圖片
<input type="file" class="btn" onchange="uploadImg(this,1);" />
</div>
<div class="upload-img">
上傳圖片
<input type="file" class="btn" onchange="uploadImg(this,2);" />
</div>
<div class="upload-img">
上傳圖片
<input type="file" class="btn" onchange="uploadImg(this,3);" />
</div>
<script>
//上傳圖片
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{
//相容IE 
var srcVal=that.value;
if(!srcVal){
alert("你執行了取消操作");
img.setAttribute("src",""); 
srcVal="";
}else{
var houzui=srcVal.substr(srcVal.lastIndexOf("."))
.replace(".","");
if(houzui=="jpg"||houzui=="jpeg"){
img.setAttribute("src",srcVal); 
}else{
alert("請上傳尾碼名是jpg,jpeg格式的圖片");
img.setAttribute("src",""); 
srcVal="";
}
}

}
}
//建立一張圖片
function createImg(parentNode,num){
var imgElement=document.createElement("img");
imgElement.className="imgs";
imgElement.id="img"+num;
parentNode.appendChild(imgElement); 
}
//判斷圖片類型,大小
function checkedImg(that,f,img){
//圖片類型
if(f[0].type=="image/jpeg"){
//圖片大小不能超過3M 
if(f[0].size/1024/1024<3){
var imgSrc=window.URL.createObjectURL(that.files[0]);
img.setAttribute("src",imgSrc); 
}else{
alert("上傳圖片大小不能超過3M");
//清空檔案域
f=[];
img.setAttribute("src",""); 
return false;

}else{
alert("請上傳尾碼名是jpg,jpeg格式的圖片");
//清空檔案域
f=[];
img.setAttribute("src",""); 
return false;


</script>
</body>
</html>

原生js實現即時預覽代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.