javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能
本文實(shí)例為大家分享了javascript實(shí)現(xiàn)移動(dòng)端上傳圖片的具體代碼,供大家參考,具體內(nèi)容如下
核心部分(.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 style="display:flex;flex-flow:wrap;"> <div class="Img" > <label> <img v-if="pic==''" src="./img/addPhotos.jpg"> <img v-else width='100%' height='100%' style="border-radius:5px" 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%' style="border-radius:5px" :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%' style="border-radius:5px" :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%' style="border-radius:5px" :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%' style="border-radius:5px" :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); }
效果圖
更多精彩內(nèi)容請(qǐng)參考專(zhuān)題《ajax上傳技術(shù)匯總》,《javascript文件上傳操作匯總》和《jQuery上傳操作匯總》進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue 使用微信jssdk,調(diào)用微信相冊(cè)上傳圖片功能
- javascript實(shí)現(xiàn)移動(dòng)端 HTML5 圖片上傳預(yù)覽和壓縮功能示例
- Vue + Node.js + MongoDB圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能詳解
- 通過(guò)js實(shí)現(xiàn)壓縮圖片上傳功能
- JS+HTML實(shí)現(xiàn)自定義上傳圖片按鈕并顯示圖片功能的方法分析
- js實(shí)現(xiàn)上傳圖片并顯示圖片名稱
- JS實(shí)現(xiàn)壓縮上傳圖片base64長(zhǎng)度功能
- JS+html5實(shí)現(xiàn)異步上傳圖片顯示上傳文件進(jìn)度條功能示例
- Nodejs實(shí)現(xiàn)圖片上傳、壓縮預(yù)覽、定時(shí)刪除功能
- JavaScript實(shí)現(xiàn)圖片上傳并預(yù)覽并提交ajax
- Js實(shí)現(xiàn)粘貼上傳圖片的原理及示例
相關(guān)文章
微信小程序?qū)崿F(xiàn)多層級(jí)復(fù)選框菜單
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)多層級(jí)復(fù)選框菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07window.location.hash 使用說(shuō)明
location是javascript里面管理地址欄的內(nèi)置對(duì)象.2010-11-11基于JS實(shí)現(xiàn)簡(jiǎn)單滑塊拼圖游戲
本文通過(guò)實(shí)例代碼給大家介紹了JS實(shí)現(xiàn)簡(jiǎn)單滑塊拼圖游戲,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-10-10javascript驗(yàn)證香港身份證的格式或真實(shí)性
本文分享了利用javascript驗(yàn)證香港身份證的格式或真實(shí)性的代碼,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02JS 實(shí)現(xiàn)Json查詢的方法實(shí)例
曾經(jīng)看過(guò)一個(gè)大牛寫(xiě)的實(shí)現(xiàn)Json的一個(gè)模板類(lèi),今天突然沒(méi)事就來(lái)自己試著寫(xiě)寫(xiě)。還好,一些東西還記得,思路還算清晰。直接上代碼了2013-04-04詳解開(kāi)源的JavaScript插件化框架MinimaJS
這篇文章主要介紹了開(kāi)源的JavaScript插件化框架MinimaJS的詳解,詳細(xì)的介紹了MinimaJS的使用,具有一定的參考價(jià)值,有興趣的可以了解一下2017-10-10JavaScript獲取XML數(shù)據(jù)附示例截圖
這篇文章主要介紹了JavaScript獲取XML數(shù)據(jù)的方法,需要的朋友可以參考下2014-03-03js尾調(diào)用優(yōu)化的實(shí)現(xiàn)
這篇文章主要介紹了js尾調(diào)用優(yōu)化的實(shí)現(xiàn),尾調(diào)用(Tail Call)是函數(shù)式編程的一個(gè)重要概念,本文介紹它的含義和用法。感興趣的可以了解一下2019-05-05