您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)vue.js如何實(shí)現(xiàn)圖片上傳預(yù)覽及圖片更換功能,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
效果圖:
樣式以及效果圖一并展示
1.HTML
<div class="rz-picter"> <img :src="avatar" class="img-avatar"> <input type="file" name="avatar" id="uppic" accept="image/gif,image/jpeg,image/jpg,image/png" @change="changeImage($event)" ref="avatarInput" class="uppic"> </div>
2.js
data() { return { avatar: require('../assets/jia.jpg'), } },
圖片一定要以require 的方式引入 這里的圖片僅用來(lái)美化 并不是上傳的圖片
3.js 寫(xiě)方法
changeImage(e) { var file = e.target.files[0] var reader = new FileReader() var that = this reader.readAsDataURL(file) reader.onload = function(e) { that.avatar = this.result } },
就這么幾段代碼一個(gè)上傳功能就寫(xiě)好了
4.切記我這里用了css來(lái)將input type = file
的樣式重新優(yōu)化了,并且將圖片做了定位 所以點(diǎn)擊中間圖片就可以觸發(fā)上傳
你需要你的需求去定義你的樣式
5.運(yùn)用原生javascript我做了一個(gè)判斷圖片是否上傳的的判斷
if(document.getElementById('uppic').value.length == '') { Toast('請(qǐng)上傳圖片'); return }
6.附上css代碼
.uppic { height: 3rem; width: 6rem; margin: 0 auto; opacity: 0; z-index: 99999; } .img-avatar { position: absolute; }
附上寫(xiě)此功能所有代碼
<template> <div class="renzheng"> <div class="rz-notice" v-bind:> <p>名片用來(lái)鑒別是相關(guān)人員,溫馨提示:<img src="../assets/static/img-icon/lan_cha.png" @click="hiddenwords"></p> </div> <van-tabs v-model="active" swipeable> <van-tab v-for="(item,index) in navArr" :title="item.name" class="v-tab"> <div v-if="index==0"> <div class="color-black" >請(qǐng)上傳本人名片照片</div> <div class="rz-picter"> <img :src="avatar" class="img-avatar"> <input type="file" name="avatar" id="uppic" accept="image/gif,image/jpeg,image/jpg,image/png" @change="changeImage($event)" ref="avatarInput" class="uppic"> </div> <div class="cuxian"></div> <div class="my-content-list"> <div class="color-black"><span >真實(shí)姓名:</span><input v-model="yoursname" placeholder="請(qǐng)輸入您的真實(shí)姓名" /></div> </div> <div class="my-content-list"> <div class="color-black"><span >手機(jī)號(hào)碼:</span><input type="number" max="11" v-model="yoursphone" placeholder="請(qǐng)輸入您的手機(jī)號(hào)碼" /></div> </div> <div class="yzbtn" @click="submitBtn"> 立即認(rèn)證 </div> </div> <div v-if="index==1"> <div class="color-black" >請(qǐng)上傳本人身份證照片</div> <div class="rz-picter"> <img src="../assets/jia.jpg" /> <p>上傳人像頁(yè)</p> </div> <div class="rz-picter"> <img src="../assets/jia.jpg" /> <p>上傳國(guó)輝頁(yè)</p> </div> <div class="cuxian"></div> <div class="my-content-list"> <div class="color-black"><span >真實(shí)姓名:</span><input placeholder="請(qǐng)輸入您的真實(shí)姓名" /></div> </div> <div class="my-content-list"> <div class="color-black"><span >手機(jī)號(hào)碼:</span><input type="number" placeholder="請(qǐng)輸入您的手機(jī)號(hào)碼" /></div> </div> <div class="yzbtn"> 立即認(rèn)證 </div> </div> </van-tab> </van-tabs> </div> </template> <script> import { Tab, Tabs } from 'vant'; import { Toast } from 'vant' export default { name: 'renzheng', data() { return { yoursname: '', yoursphone: '', avatar: require('../assets/jia.jpg'), isno: 'block', active: 0, navArr: [{ name: "身份認(rèn)證" }, { name: "實(shí)名認(rèn)證" } ], } }, methods: { hiddenwords() { this.isno = 'none' }, submitBtn() { if(this.yoursname == '') { Toast("請(qǐng)?zhí)顚?xiě)你的真實(shí)姓名"); return; } if(this.yoursphone == '' || this.yoursphone == null) { Toast("請(qǐng)?zhí)顚?xiě)你的手機(jī)號(hào)碼"); return; } if(document.getElementById('uppic').value.length == '') { Toast('請(qǐng)上傳圖片'); return; } }, changeImage(e) { var file = e.target.files[0] var reader = new FileReader() var that = this reader.readAsDataURL(file) reader.onload = function(e) { that.avatar = this.result } }, }, mounted: function() { } } </script> <style> .rz-notice { padding: 0.2rem 0.3rem; display: flex; flex-direction: row; justify-content: space-between; align-items: center; background: #F0F5FB; color: #28D9EF; } .rz-notice img { height: 0.22rem; width: 0.22rem; } .rz-picter { height: 3rem; width: 6rem; margin: 0.3rem auto; border: 0.01rem solid #ededed; display: flex; flex-direction: column; align-items: center; justify-content: center; } .uppic { height: 3rem; width: 6rem; margin: 0 auto; opacity: 0; z-index: 99999; } .img-avatar { position: absolute; } /*立即驗(yàn)證*/ .yzbtn { width: 90%; height: 0.8rem; background: #FF6600; border-radius: 0.06rem; margin: 30px auto; text-align: center; line-height: 0.8rem; color: #FFFFFF } /*修改原有tab樣式*/ .van-tab { color: #A3A3A3!important; } .van-tab--active { color: #000!important; } .van-tabs__line { background-color: #FF6600!important; width: 0.7rem!important; text-align: center!important; align-items: center; margin-left: 1.5rem; } .van-toast { background-color: #FF6600; color: #FFFFFF } .my-content-list { padding: 0.3rem; display: flex; flex-direction: row; justify-content: space-between; border-bottom: 0.01rem solid #EDEDED; } </style>
關(guān)于“vue.js如何實(shí)現(xiàn)圖片上傳預(yù)覽及圖片更換功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(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)容。