溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能的方法

發(fā)布時(shí)間:2020-08-19 10:10:17 來(lái)源:億速云 閱讀:300 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

核心部分(.html)

window.onload = function() {
 let fileTag = document.getElementById('file');
 // console.log(fileTag)
 // console.log(this.pic)
 let that = this
 
 fileTag.onchange = function() {
  let file = fileTag.files[0];
  let fileReader = new FileReader();
  // console.log(file)
  console.log(fileReader)
  // console.log(that.pic)
  fileReader.onloadend = function() {
  console.log(1212)
  if (fileReader.readyState == fileReader.DONE) {
   // console.log(that.pic)
   console.log(fileReader)
   
   if(that.pic=="") {
   console.log(1111)
   that.pic = fileReader.result
   that.input1 = false
   
   that.upLoad(fileReader.result)
   return
   }else{
   if(that.pic2=='') {
    console.log(2222)
    that.pic2 = fileReader.result
    that.input2 = false
    return
   }else {
    if(that.pic3=='') {
    console.log(3333)
    that.pic3 = fileReader.result
    that.input3 = false
    return
    }else {
    if(that.pic4=='') {
     console.log(4444)
     that.pic4 = fileReader.result
     that.input4 = false
     return
    }else {
     console.log(5555)
     if(that.pic5=='') {
     that.pic5 = fileReader.result
     that.input5 = false
     return
     }
     
    }
    }
   }
   
   }
   // console.log(that.pic)
  }
  };
  
  fileReader.readAsDataURL(file);
 }
} 

vue項(xiàng)目代碼

主要是HTML頁(yè)面設(shè)計(jì)

<div class="imgBox" @click="upImg">
 <div >
 <div class="Img" >
  <label>
  <img v-if="pic==''" src="./img/addPhotos.jpg">
  <img v-else width='100%' height='100%'  id="img" :src="pic">
  <input v-if="input1" class="myInput" type="file" id="file" accept="image/*" multiple required>
  </label>
  <div v-if="pic!=''" class="myDel" @click="delImg(1)">X</div>
  
 </div>
 <div class="Img" v-if="pic!='' || !input2">
  <label>
  <img v-if="pic2==''" src="./img/addPhotos.jpg">
  <img v-else width='100%' height='100%'  :src="pic2">
  <input v-if="input2" class="myInput" type="file" id="file" accept="image/*" multiple required>
  </label>
  <div v-if="pic2!=''" class="myDel" @click="delImg(2)">X</div>
 </div>
 <div class="Img" v-if="(pic!='' && pic2!='' ) || !input3">
  <label>
  <img v-if="pic3==''" src="./img/addPhotos.jpg">
  <img v-else width='100%' height='100%'  :src="pic3">
  <input v-if="input3" class="myInput" type="file" id="file" accept="image/*" multiple required>
  </label>
  <div v-if="pic3!=''" class="myDel" @click="delImg(3)">X</div>
 </div>
 <div class="Img" v-if="(pic!='' && pic2!='' && pic3!='') || !input4">
  <label>
  <img v-if="pic4==''" src="./img/addPhotos.jpg">
  <img v-else width='100%' height='100%'  :src="pic4">
  <input v-if="input4" class="myInput" type="file" id="file" accept="image/*" multiple required>
  </label>
  <div v-if="pic4!=''" class="myDel" @click="delImg(4)">X</div>
 </div>
 <div class="Img" v-if="(pic!='' && pic2!='' && pic3!='' && pic4!='') || !input5">
  <label> 
  <img v-if="pic5==''" src="./img/addPhotos.jpg">
  <img v-else width='100%' height='100%'  :src="pic5">
  <input v-if="input5" class="myInput" type="file" id="file" accept="image/*" multiple required>
  </label>
  <div v-if="pic5!=''" class="myDel" @click="delImg(5)">X</div>
 </div>
 </div>
</div>

data()部分

pic:'',
pic2:'',
pic3:'',
pic4:'',
pic5:'',
input1:true,
input2:true,
input3:true,
input4:true,
input5:true

methods:{}部分

upImg() {
  
 // window.onload = function() {
 let fileTag = document.getElementById('file');
 // console.log(fileTag)
 // console.log(this.pic)
 let that = this
 
 fileTag.onchange = function() {
  let file = fileTag.files[0];
  let fileReader = new FileReader();
  // console.log(file)
  console.log(fileReader)
  // console.log(that.pic)
  fileReader.onloadend = function() {
  console.log(1212)
  if (fileReader.readyState == fileReader.DONE) {
   // console.log(that.pic)
   console.log(fileReader)
   
   if(that.pic=="") {
   console.log(1111)
   that.pic = fileReader.result
   that.input1 = false
   
   that.upLoad(fileReader.result)
   return
   }else{
   if(that.pic2=='') {
    console.log(2222)
    that.pic2 = fileReader.result
    that.input2 = false
    return
   }else {
    if(that.pic3=='') {
    console.log(3333)
    that.pic3 = fileReader.result
    that.input3 = false
    return
    }else {
    if(that.pic4=='') {
     console.log(4444)
     that.pic4 = fileReader.result
     that.input4 = false
     return
    }else {
     console.log(5555)
     if(that.pic5=='') {
     that.pic5 = fileReader.result
     that.input5 = false
     return
     }
    }
    }
   }
   }
   // console.log(that.pic)
  }
  };
  fileReader.readAsDataURL(file);
 }
 // } 
},
delImg(num) {
 if(num==1) {
 this.pic = ''
 this.input1 = true
 }
 if(num==2) {
 this.pic2 = ''
 this.input2 = true
 }
 if(num==3) {
 this.pic3 = ''
 this.input3 = true
 }
 if(num==4) {
 this.pic4 = ''
 this.input4 = true
 }
 if(num==5) {
 this.pic5 = ''
 this.input5 = true
 }
}

css部分(style)

#order .imgBox {
 margin-bottom: 100px;
 padding: 20px;
 box-sizing: border-box;
 background: #fff;
 height: 250px;
}

#order .Img {
 width: 90px;
 height: 90px;
 background: #fff;
 text-align: center;
 line-height: 132px;
 box-sizing: border-box;
 border-radius: 5px;
 border: 1px solid rgba(0,0,0,.2);
 position: relative;
 margin: 0 20px 20px 0;
}

#order .myInput {
 opacity:0;
 position:absolute;
 top:0;
 top:0;
 width:90px;
 height:90px;
 left:0;
}

#order .myDel {
 color: #fff;
 position: absolute;
 right: -10px;
 top: -7px;
 width: 25px;
 height: 25px;
 border-radius: 25px;
 line-height: 25px;
 background: rgba(0,0,0,.5);
}

效果圖

javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能的方法

javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能的方法

javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能的方法

以上是javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI