vue實(shí)現(xiàn)圖片上傳預(yù)覽功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)圖片上傳預(yù)覽的具體代碼,供大家參考,具體內(nèi)容如下
效果圖

html結(jié)構(gòu)
<ul class="gallery-window-map" style="flex-wrap:wrap;">
<!--點(diǎn)擊上傳按鈕-->
<li class="house-pic-item" v-if="!(!item.isNew&&editBtnType[index])" @click="houseUpload(index)">
<div class="pic-box">
<span class="iconfont icon-zengjia"></span>
<h5 class="btn-tit">點(diǎn)擊上傳</h5>
</div>
</li>
<!--展示區(qū)域-->
<viewer :images="item.imgUrl">
<li class="house-pic-item" v-for="(picItem, picIndex) in item.imgUrl" :key="picIndex">
<img :src="picItem" alt="" :key="picItem" width="120" height="90" :ref="'showImg_'+index">
<div class="mask">
<div class="ico-box">
<span class="font-btn" @click="clickWatchImg('showImg_'+index,picIndex)">
<i class="iconfont icon-fangda"></i>
</span>
<span class="font-btn" v-if="!(!item.isNew&&editBtnType[index])" @click="delHouseImage(index,picIndex)">
<i class="iconfont icon-shanchu"></i>
<i class="line"></i>
</span>
</div>
</div>
</li>
</viewer>
</ul>
css樣式
.gallery-window-map{
display: -webkit-box;
display: -ms-flexbox;
display: flex;
margin-top: 10px;
}
.house-pic-item {
position: relative;
display: inline-block;
margin-right: 13px;
width: 120px;
height: 90px;
background-color: #e3e3e3;
}
.pic-box {
width: 100%;
text-align: center;
}
.icon-zengjia {
position: relative;
top: 12px;
font-size: 26px;
color: #b2b2b2;
}
.btn-tit {
height: 38px;
line-height: 38px;
font-size: 12px;
color: #999;
}
.mask {
display: none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(34, 34, 34, 0.6);
}
.font-btn {
display: inline-block;
height: 40px;
width: 50%;
padding: 0 20px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.font-btn:last-child {
position: relative;
}
.icon-fangda,
.icon-shanchu {
font-size: 22px;
color: #fff;
}
.line {
content: '';
display: inline-block;
position: absolute;
left: 0;
top: 10px;
width: 1px;
height: 20px;
background: #fff;
}
/**
* 模擬點(diǎn)擊上傳圖片按鈕
* @index 當(dāng)前操作的戶(hù)型box的索引
*/
houseUpload(index) {
this.$refs.houseTypeLoad[index].click()
},
/**
* 上傳圖片到服務(wù)器
* @index 當(dāng)前操作的戶(hù)型box的索引
*/
upLoadHouse(e, index) {
let _that = this
const file = e.target.files[0]
if (!file) {
return
}
new ImageCompressor(file, {
quality: 0.9,
maxWidth: 2000,
maxHeight: 2000,
success(result) {
// debugger
const formData = new FormData()
formData.append('file', result, result.name)
formData.append('watermark', false)
// Send the compressed image file to server with XMLHttpRequest.
if (result.size > 1 * 1024 * 1024 || result.size < 3 * 1024) {
_that.$message('圖片大小要在3K~1M之間')
return
} else {
_that.$ajax.post('/img/upload', formData).then(res => {
res = res.data
if (res.images && res.images.length > 0) {
if (res.images[0].src !== 'file size is too small') {
let item = res.images[0].src
console.log(item)
_that.houseTypeForm[index].imgUrl.unshift(item)
}
}
})
}
},
error(e) {
console.log(e.message)
}
})
},
/**
* 打開(kāi)圖片查看器
*/
clickWatchImg(str, picIndex) {
console.log('=================')
console.log(picIndex)
console.log(this.$refs[str][picIndex])
this.$refs[str][picIndex].click()
},
/**
* 刪除指定圖片,操作表單數(shù)據(jù)
* @index 當(dāng)前操作的戶(hù)型box的索引
* @picIndex 當(dāng)前操作的圖片索引
*/
delHouseImage(index, picIndex) {
this.houseTypeForm[index].imgUrl.splice(picIndex, 1)
},
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請(qǐng)閱讀專(zhuān)題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用axios獲取不到響應(yīng)頭Content-Disposition的問(wèn)題及解決
這篇文章主要介紹了vue使用axios獲取不到響應(yīng)頭Content-Disposition的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
詳解Vue.js組件可復(fù)用性的混合(mixin)方式和自定義指令
本篇文章主要介紹了詳解Vue.js組件可復(fù)用性的混合(mixin)方式和自定義指令,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
Vuex狀態(tài)機(jī)的快速了解與實(shí)例應(yīng)用
Vuex是專(zhuān)門(mén)為Vuejs應(yīng)用程序設(shè)計(jì)的狀態(tài)管理工具,這篇文章主要給大家介紹了關(guān)于Vuex狀態(tài)機(jī)快速了解與實(shí)例應(yīng)用的相關(guān)資料,需要的朋友可以參考下2021-06-06
詳解Vue 2中的? initState 狀態(tài)初始化
這篇文章主要介紹了詳解Vue 2中的initState狀態(tài)初始化,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
vue使用$store.commit() undefined報(bào)錯(cuò)的解決
這篇文章主要介紹了vue使用$store.commit() undefined報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
詳解基于webpack和vue.js搭建開(kāi)發(fā)環(huán)境
本篇文章主要介紹了詳解基于webpack和vue.js搭建開(kāi)發(fā)環(huán)境 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Slots Emit和Props穿透組件封裝實(shí)現(xiàn)摸魚(yú)加鐘
這篇文章主要為大家介紹了Slots Emit和Props穿透組件封裝實(shí)現(xiàn)示例詳解,為摸魚(yú)加個(gè)鐘,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)的實(shí)例詳解(params/query)
在使用`router.push`進(jìn)行路由跳轉(zhuǎn)到另一個(gè)組件時(shí),可以通過(guò)`params`或`query`來(lái)傳遞參數(shù),這篇文章主要介紹了vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)(params/query),需要的朋友可以參考下2023-09-09

