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)前操作的戶型box的索引
*/
houseUpload(index) {
this.$refs.houseTypeLoad[index].click()
},
/**
* 上傳圖片到服務(wù)器
* @index 當(dāng)前操作的戶型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)前操作的戶型box的索引
* @picIndex 當(dāng)前操作的圖片索引
*/
delHouseImage(index, picIndex) {
this.houseTypeForm[index].imgUrl.splice(picIndex, 1)
},
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請(qǐng)閱讀專題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決el-select數(shù)據(jù)量過(guò)大的3種方案
最近做完一個(gè)小的后臺(tái)管理系統(tǒng),快上線了,發(fā)現(xiàn)一個(gè)問(wèn)題,有2個(gè)select的選項(xiàng)框線上的數(shù)據(jù)量是1w+,而測(cè)試環(huán)境都是幾百的,所以導(dǎo)致頁(yè)面直接卡住了,本文給大家總結(jié)了3種方法,需要的朋友可以參考下2023-09-09
使用vue官方提供的模板vue-cli搭建一個(gè)helloWorld案例分析
這篇文章主要介紹了用vue官方提供的模板vue-cli搭建一個(gè)helloWorld案例,需要的朋友可以參考下2018-01-01
vue3集成Element-Plus之全局導(dǎo)入和按需導(dǎo)入
這篇文章主要給大家介紹了關(guān)于vue3集成Element-Plus之全局導(dǎo)入和按需導(dǎo)入的相關(guān)資料,element-plus正是element-ui針對(duì)于vue3開(kāi)發(fā)的一個(gè)UI組件庫(kù),?它的使用方式和很多其他的組件庫(kù)是一樣的,需要的朋友可以參考下2023-07-07
vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn)
這篇文章主要介紹了vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue?2?如何添加?register-service-worker?實(shí)現(xiàn)緩存請(qǐng)求的問(wèn)題
這篇文章主要介紹了Vue?2?如何添加?register-service-worker?以實(shí)現(xiàn)緩存請(qǐng)求的目的,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
使用Vue實(shí)現(xiàn)網(wǎng)站SEO優(yōu)化的方法示例
在如今這個(gè)數(shù)字化和信息化的時(shí)代,搜索引擎優(yōu)化(SEO)已經(jīng)成為網(wǎng)站成功的關(guān)鍵因素之一,在使用現(xiàn)代化框架如Vue.js進(jìn)行開(kāi)發(fā)時(shí),開(kāi)發(fā)者通常關(guān)注的是構(gòu)建高效的單頁(yè)面應(yīng)用,本文將介紹如何使用Vue來(lái)優(yōu)化網(wǎng)站的SEO,并提供一些示例代碼幫助您實(shí)現(xiàn)這些優(yōu)化2024-11-11
Vue如何實(shí)現(xiàn)pptx在線預(yù)覽
通過(guò)PPTXjs插件,實(shí)現(xiàn)PPTX文件在線預(yù)覽,需下載PPTXjs,將其引入HTML頁(yè)面,并編寫相應(yīng)的HTML和JS代碼,如果是移動(dòng)端還需調(diào)整div大小,這是一種便捷的前端PPTX轉(zhuǎn)HTML技術(shù),適合網(wǎng)頁(yè)展示使用2024-09-09
vue監(jiān)聽(tīng)瀏覽器網(wǎng)頁(yè)關(guān)閉和網(wǎng)頁(yè)刷新事件代碼示例
在前端開(kāi)發(fā)中我們通常會(huì)遇到這樣的需求,用戶離開(kāi)、刷新頁(yè)面前,修改數(shù)據(jù)未進(jìn)行保存操作,需要提示框提醒用戶,這篇文章主要給大家介紹了關(guān)于vue監(jiān)聽(tīng)瀏覽器網(wǎng)頁(yè)關(guān)閉和網(wǎng)頁(yè)刷新事件的相關(guān)資料,需要的朋友可以參考下2023-08-08

