vant uploader實(shí)現(xiàn)上傳圖片拖拽功能(設(shè)為封面)
效果圖如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="public/vant/index.css" rel="external nofollow" >
<script src="public/vue.min.js"></script>
<script src="public/vant/vant.min.js"></script>
<script src="public/sortable/Sortable.min.js"></script>
<script src="public/draggle/vuedraggable.umd.js"></script>
</head>
<style>
.img_uploader{display: flex;}
.img_upload span {display: -webkit-box;display: -webkit-flex;display: flex;-webkit-flex-wrap: wrap;flex-wrap: wrap;}
.img_upload_item {position: relative;margin: 0 8px 8px 0;cursor: pointer;}
.preview-cover {position: absolute;bottom: 0;box-sizing: border-box;width: 100%;padding: 4px;color: #fff;font-size: 12px;text-align: center;background: rgba(0, 0, 0, 0.3);}
.van-image__error,.van-image__img,.van-image__loading {object-fit: cover;}
</style>
<body>
<div id="app">
<div class="img_uploader">
<van-uploader :after-read="onRead"> </van-uploader>
<draggable class="img_upload" v-model="fileList" :group="{name:'imgs'}" @end="end" animation="200">
<transition-group>
<div class="img_upload_item" v-for="(item,index) in fileList" :key="index" @click="previewImg(fileList,index)">
<div class="van-image van-uploader__preview-image">
<img :src="item.content" alt="圖片" class="van-image__img">
<div class="van-uploader__preview-cover">
<div class="preview-cover van-ellipsis" v-if="index == 0">封面圖</div>
</div>
</div>
<div class="van-uploader__preview-delete" @click="delImg(index)">
<i class="van-icon van-icon-cross van-uploader__preview-delete-icon"></i>
</div>
</div>
</transition-group>
</draggable>
</div>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
components: {
vuedraggable: window.vuedraggable, //當(dāng)前頁(yè)面注冊(cè)拖拽組件
},
data() {
return {
fileList: [
{content: 'https://img01.yzcdn.cn/vant/leaf.jpg',name: '12'},
{content: 'images/banner1.png',name: '12'},
{content: 'images/banner2.png',name: '12'},
{content: 'images/banner3.png',name: '12'},
{content: 'images/banner4.png',name: '12'},
],
}
},
methods: {
// 刪除圖片
delImg(index) {
if (isNaN(index) || index >= this.fileList.length) {
return false
}
let tmp = []
for (let i = 0, len = this.fileList.length; i < len; i++) {
if (this.fileList[i] !== this.fileList[index]) {
tmp.push(this.fileList[i])
}
}
this.fileList = tmp
},
onRead(file) { // 上傳圖片到圖片服務(wù)器
this.fileList.push(file)
console.log(file)
},
// 點(diǎn)擊預(yù)覽
previewImg(images, index) {
let listSrc = [];
this.fileList.forEach(function(v,i){
listSrc.push(v.content)
})
vant.ImagePreview({
images:listSrc,
startPosition: index,
closeable:true
});
},
// 拖拽結(jié)束后 獲取剛剛拖動(dòng)的數(shù)據(jù)
end(e) {
var _oldIndex = e.oldIndex
var _oldItem = this.fileList[_oldIndex]
console.log(_oldItem);
},
}
})
</script>
</html>
到此這篇關(guān)于vant uploader實(shí)現(xiàn)上傳圖片拖拽功能(設(shè)為封面)的文章就介紹到這了,更多相關(guān)vant uploader上傳圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript實(shí)現(xiàn)獲取瀏覽器版本、操作系統(tǒng)類型
這篇文章主要給大家分享一段javascript實(shí)現(xiàn)獲取瀏覽器版本、操作系統(tǒng)類型的封裝好的代碼,使用非常方便,也很實(shí)用,推薦給大家。2015-01-01
Web網(wǎng)站都變成灰色有哪些方法可以快速實(shí)現(xiàn)(解決方案)
有些時(shí)候我們需要把網(wǎng)站頁(yè)面變成黑白色或灰色,特別是對(duì)于一些需要悼念的日子,以及一些影響力很大的偉人逝世或紀(jì)念日的時(shí)候,都會(huì)讓網(wǎng)站的全部網(wǎng)頁(yè)變成灰色(黑白色),以表示我們對(duì)逝者或者英雄的緬懷和悼念2022-12-12
使用JSX實(shí)現(xiàn)Carousel輪播組件的方法(前端組件化)
做這個(gè)輪播圖的組件,我們先從一個(gè)最簡(jiǎn)單的 DOM 操作入手。使用 DOM 操作把整個(gè)輪播圖的功能先實(shí)現(xiàn)出來(lái),然后在一步一步去考慮怎么把它設(shè)計(jì)成一個(gè)組件系統(tǒng)2021-04-04
JS判斷文本框內(nèi)容改變事件的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)JS判斷文本框內(nèi)容改變事件的簡(jiǎn)單實(shí)例進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-03-03
javascript:void(0)用法及常見(jiàn)問(wèn)題分析
javascript:void(0) 在某些情況下會(huì)有瀏覽器不兼容的bug。下面我們先來(lái)看下javascript:void(0) 的基礎(chǔ)介紹及用法,然后再來(lái)看使用它會(huì)出現(xiàn)什么問(wèn)題,該怎么解決,感興趣的朋友跟隨小編一起看看吧2023-10-10
js對(duì)字符串進(jìn)行編碼的方法總結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇js對(duì)字符串進(jìn)行編碼的方法總結(jié)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11

