Vue2.0 實(shí)現(xiàn)移動(dòng)端圖片上傳功能
本文主要介紹VUE2.0圖片上傳功能的實(shí)現(xiàn)。原理是通過(guò)js控制和input標(biāo)簽的方式完成這一效果,無(wú)需加載其他組件。
效果圖如下:

1.DOM代碼
1.1input標(biāo)簽
由于我們是通過(guò)input標(biāo)簽的方式進(jìn)行圖片上傳的,但是input標(biāo)簽的樣式有點(diǎn)丑,所以我們隱藏該樣式display: none
<input @change="fileChange($event)" type="file" id="upload_file" multiple style="display: none"/>
1.2添加圖片按鈕
如果需要用到此方法,只需要在你的上傳按鈕的地方調(diào)用@click=”chooseType”即可,其他部分代碼為樣式布局僅供參考。
<div class="add" @click="chooseType"> <div class="add-image" align="center"> <i class="icon-camera"></i> //按鈕中的圖片是一個(gè)icon字體圖標(biāo) <p class="font13">添加圖片</p> </div> </div>
1.3圖片預(yù)覽區(qū)域
如果需要用到此方法,只需要在你的預(yù)覽區(qū)域進(jìn)行v-for循環(huán)輸出上傳的圖片集合即可。
<div class="add-img" v-show="imgList.length"> <p class="font14">圖片(最多6張,還可上傳<span v-text="6-imgList.length"></span>張)</p> <ul class="img-list"> <li v-for="(url,index) in imgList"> <img class="del" src="../../assets/img/home/btn_clean.png" @click.stop="delImg(index)"/> //del刪除樣式,icon字體圖標(biāo)需要自己找哦 <img :src="url.file.src"> </li> </ul> </div>
1.4圖片預(yù)覽區(qū)域-拓展(1.3為簡(jiǎn)單運(yùn)用,如果有時(shí)間后續(xù)會(huì)將完整的案例上傳)
如果需要用到此方法,只需要在你的預(yù)覽區(qū)域進(jìn)行v-for循環(huán)輸出上傳的圖片集合即可。本案例還運(yùn)用的Y-DUI的lightbox組件,如有需要請(qǐng)參照?qǐng)D片預(yù)覽的調(diào)用方式。此處,也調(diào)用了vue的懶加載和css背景圖自適應(yīng)的方法。
<div class="add-img" v-show="imgList.length">
<p class="font14">圖片(最多6張,還可上傳<span v-text="6-imgList.length"></span>張)</p>
<ul class="img-list">
<li v-for="(url,index) in imgList">
<img class="del" src="../../assets/img/home/btn_clean.png" @click.stop="delImg(index)"/>
<yd-lightbox>
<div class="app-bg">
<yd-lightbox-img class="app-bg" :original="url.file.src" v-lazy:background-image="{src: url.file.src, error: require('../../assets/img/common/img_placeholder400.png'), loading: require('../../assets/img/common/img_placeholder400.png')}"></yd-lightbox-img>
</div>
</yd-lightbox>
</li>
</ul>
</div>
2.JS代碼塊
tips:此處的提示彈窗調(diào)用的Y-DUI的提示框,可以改成自己的提示框。
<script>
export default {
name: "Feedback",
data() {
return {
showFace: false,
imgList: [],
size: 0,
limit:6, //限制圖片上傳的數(shù)量
tempImgs:[]
}
},
methods: {
chooseType() {
document.getElementById('upload_file').click();
},
fileChange(el) {
if (!el.target.files[0].size) return;
this.fileList(el.target);
el.target.value = ''
},
fileList(fileList) {
let files = fileList.files;
for (let i = 0; i < files.length; i++) {
//判斷是否為文件夾
if (files[i].type != '') {
this.fileAdd(files[i]);
} else {
//文件夾處理
this.folders(fileList.items[i]);
}
}
},
//文件夾處理
folders(files) {
let _this = this;
//判斷是否為原生file
if (files.kind) {
files = files.webkitGetAsEntry();
}
files.createReader().readEntries(function (file) {
for (let i = 0; i < file.length; i++) {
if (file[i].isFile) {
_this.foldersAdd(file[i]);
} else {
_this.folders(file[i]);
}
}
});
},
foldersAdd(entry) {
let _this = this;
entry.file(function (file) {
_this.fileAdd(file)
})
},
fileAdd(file) {
if (this.limit !== undefined) this.limit--;
if (this.limit !== undefined && this.limit < 0) return;
//總大小
this.size = this.size + file.size;
//判斷是否為圖片文件
if (file.type.indexOf('image') == -1) {
this.$dialog.toast({mes: '請(qǐng)選擇圖片文件'});
} else {
let reader = new FileReader();
let image = new Image();
let _this = this;
reader.readAsDataURL(file);
reader.onload = function () {
file.src = this.result;
image.onload = function(){
let width = image.width;
let height = image.height;
file.width = width;
file.height = height;
_this.imgList.push({
file
});
console.log( _this.imgList);
};
image.src= file.src;
}
}
},
delImg(index) {
this.size = this.size - this.imgList[index].file.size;//總大小
this.imgList.splice(index, 1);
if (this.limit !== undefined) this.limit = 6-this.imgList.length;
},
displayImg() {
}
}
}
</script>
3.CSS樣式代碼塊,僅供參考
太太懶了,沒(méi)有一一區(qū)分
.app-bg >>>img{
width: 100%;
height: 100%;
-webkit-transform: scale(1.03);
-moz-transform: scale(1.03);
-ms-transform: scale(1.03);
-o-transform: scale(1.03);
transform: scale(1.03);
}
textarea {
padding: 10px;
}
.text-length {
font-size: 14px;
z-index: 999;
width: 100%;
text-align: right;
margin-bottom: 10px;
color: #e4e4e4;
}
.warning {
color: #fe9900;
}
.add-img {
width: 100%;
padding: 10px;
}
.add-img p {
color: #999;
}
.mui-content {
padding-bottom: 60px;
}
.mui-content .idea {
margin-top: 8px;
background-color: #FFFFFF;
}
.good-item {
text-align: center;
width: 33%;
max-width: 100%;
overflow: hidden;
padding-right: 10px;
padding-bottom: 10px;
float: left;
}
.good-item span {
font-size: 15px;
height: 30px;
line-height: 30px;
border-radius: 50px;
display: block;
width: 100%;
color: #333;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
border: 1px solid #CCCCCC;
}
.mui-table {
padding-top: 10px;
color: #333;
padding-left: calc(0.5% + 10px);
}
.h-line-behind {
line-height: 40px;
padding-left: 10px;
}
.question {
border: 0;
margin-bottom: 10px;
}
.add {
display: inline-block;
margin-bottom: 20px;
}
.add-image {
padding-top: 15px;
margin-left: 10px;
width: 80px;
top: 20px;
height: 80px;
border: 1px dashed rgba(0, 0, 0, .2);
}
.add-image .icon-camera {
font-size: 24px;
}
.good-item .choose {
color: #fff;
background-color: #87582E;
color: #FFF;
border: 0;
}
.mui-btn-block {
border: 0;
border-radius: 0;
background-color: #87582E;
color: #fff;
margin-bottom: 0;
bottom: 0;
}
.mui-buttom {
position: fixed;
width: 100%;
bottom: 0;
z-index: 999;
}
/*九宮格*/
.img-list {
overflow: hidden;
}
.img-list > li {
float: left;
width: 32%;
text-align: center;
padding-top: 31%;
margin-left: 1%;
margin-top: 1%;
position: relative;
}
.img-list > li > div {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.img-list > li > div .app-bg {
width: 100%;
height: 100%;
}
.mui-fullscreen {
z-index: 9999;
}
.del {
position: absolute;
width: 18px;
top: 0;
right: 0;
z-index: 999
}
總結(jié)
以上所述是小編給大家介紹的Vue2.0 移動(dòng)端圖片上傳功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
VeeValidate在vue項(xiàng)目里表單校驗(yàn)應(yīng)用案例
這篇文章主要介紹了VeeValidate在vue項(xiàng)目里表單校驗(yàn)應(yīng)用案例,VeeValidate是Vue.js的驗(yàn)證庫(kù),它有很多驗(yàn)證規(guī)則,并支持自定義規(guī)則,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-05-05
vue實(shí)現(xiàn)todolist單頁(yè)面應(yīng)用
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)todolist單頁(yè)面應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Vue 3 中使用 Element Plus 的 `el-t
在 Vue 3 中使用 Element Plus 的 `el-table` 組件實(shí)現(xiàn)自適應(yīng)高度,你可以根據(jù)容器的高度動(dòng)態(tài)設(shè)置表格的高度,下面通過(guò)示例代碼給大家展示,感興趣的朋友一起看看吧2024-12-12
window.onresize在vue中只能使用一次,自適應(yīng)resize報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了window.onresize在vue中只能使用一次,自適應(yīng)resize報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
如何用vue-cli3腳手架搭建一個(gè)基于ts的基礎(chǔ)腳手架的方法
這篇文章主要介紹了如何用vue-cli3腳手架搭建一個(gè)基于ts的基礎(chǔ)腳手架的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue使用v-model進(jìn)行跨組件綁定的基本實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue使用v-model進(jìn)行跨組件綁定的基本實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Vue 框架之動(dòng)態(tài)綁定 css 樣式實(shí)例分析
這篇文章主要介紹了Vue 框架之動(dòng)態(tài)綁定 css 樣式的方法,本文通過(guò)分享小實(shí)例給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11

