Vue實(shí)現(xiàn)多圖添加顯示和刪除
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)多圖添加顯示和刪除的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

首先給一個(gè)input[type="file"],然后隱藏掉,當(dāng)點(diǎn)擊加號(hào)所在的區(qū)域時(shí),觸發(fā)文件選擇的點(diǎn)擊事件。
注意:取src的值時(shí)用v-bind:src="imgsrc";用src="imgsrc"或者src="{{imgsrc}}"會(huì)報(bào)錯(cuò)。
代碼:(有些樣式省略,主要是添加和刪除方法)
<template>
<div id="originality">
<div class="ipt">主圖:</div>
<div class="picture">
<div class="Mainpicture">
<div class="iconfont icon-jia" @click="uploadPic('filed')"></div>
</div>
<!--主圖可以添加多個(gè)圖片-->
<div id="img" v-for="(imgsrc,index) in imgsrcs">
<img id="imgshow" :src="imgsrc">
<div class="iconfont icon-cha" @click="deleteMulPic(index)"</div>
</div>
</div>
<input id="filed" type="file" multiple="multiple" accept="image/*,application/pdf" style="display: none;" @change="changeMulPic()">
</div>
</template>
<script>
export default {
name: "originality",
components: {
},
data() {
return {
imgsrcs: []
}
},
methods: {
uploadPic: function(val) {
document.getElementById(val).click();
},
changeMulPic: function() {
var file = $("#filed").get(0).files[0];
$("#img").show();
this.imgsrcs.push(window.URL.createObjectURL(file))
},
deleteMulPic: function(index) {
this.imgsrcs.splice(index, 1);
}
}
}
</script>
<style scoped>
.Mainpicture {
float: left;
width: 100px;
height: 100px;
background: rgba(255, 255, 255, 1);
border-radius: 2px;
border: 1px solid #E5E9F2;
}
.picture {
min-height: 100px;
}
.files {
display: none;
float: left;
}
#img {
margin-left: 20px;
float: left;
width: 100px;
height: 100px;
border-radius: 2px;
border: 1px solid #E5E9F2;
}
.icon-cha {
cursor: pointer;
position: absolute;
width: 10px;
height: 10px;
margin-left: 85px;
margin-top: -100px;
color: #BFC5D1;
}
#imgshow {
width: 100px;
height: 100px;
}
.icon-jia {
text-align: center;
width: 20px;
height: 20px;
line-height: 20px;
color: #BFC5D1;
padding: 40px;
cursor: pointer;
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在vue3項(xiàng)目中給頁(yè)面添加水印的實(shí)現(xiàn)方法
這篇文章主要給大家介紹一下在vue3項(xiàng)目中添加水印的實(shí)現(xiàn)方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來看看吧2023-08-08
vue項(xiàng)目引入本地bootstrap不生效問題及解決
這篇文章主要介紹了vue項(xiàng)目引入本地bootstrap不生效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
如何解決element-ui中select下拉框popper超出彈框問題
這篇文章主要介紹了如何解決element-ui中select下拉框popper超出彈框問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Vue獲取當(dāng)前系統(tǒng)日期(年月日)的示例代碼
發(fā)中會(huì)有要獲取當(dāng)前日期的需求,有的是獲取到當(dāng)前月份,有的是精確到分秒,在 Vue 開發(fā)中,獲取當(dāng)前時(shí)間是一項(xiàng)常見的需求,本文將深入探討Vue獲取當(dāng)前系統(tǒng)日期(年月日),幫助您更好地利用當(dāng)前時(shí)間,需要的朋友可以參考下2024-01-01
vue自定v-model實(shí)現(xiàn)表單數(shù)據(jù)雙向綁定問題
vue.js的一大功能便是實(shí)現(xiàn)數(shù)據(jù)的雙向綁定。這篇文章主要介紹了vue自定v-model實(shí)現(xiàn) 表單數(shù)據(jù)雙向綁定的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
vue獲取token實(shí)現(xiàn)token登錄的示例代碼
最近新做了個(gè)vue項(xiàng)目,正好項(xiàng)目中有登錄部分,本文就詳細(xì)的介紹一下登錄部分的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2021-11-11

