Vant uploader implements the drag-and-drop function for uploading pictures (set as cover)

Vant uploader implements the drag-and-drop function for uploading pictures (set as cover)

The effect diagram is as follows:

<!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="Picture" class="van-image__img">
                            <div class="van-uploader__preview-cover">
                                <div class="preview-cover van-ellipsis" v-if="index == 0">Cover image</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, //Register drag component on current page},
        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: {
            // Delete the image 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) { // Upload the image to the image server this.fileList.push(file) 
                console.log(file)
            },
            // Click to preview previewImg(images, index) {
                let listSrc = [];
                this.fileList.forEach(function(v,i){
                    listSrc.push(v.content)
                })
                vant.ImagePreview({
                    images:listSrc,
                    startPosition: index,
                    closeable:true
                });
            },
            // Get the data just dragged after the drag ends end(e) {
                var _oldIndex = e.oldIndex
                var _oldItem = this.fileList[_oldIndex]
                console.log(_oldItem);
            },
        }
    })
</script>

</html>

This is the end of this article about how to use vant uploader to upload pictures by dragging and dropping (set as cover). For more information about vant uploader to upload pictures, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vant Uploader implements the component of uploading one or more pictures
  • The Uploader of Vue+Vant on the mobile terminal realizes the functions of uploading, compressing and rotating pictures

<<:  HTML table markup tutorial (38): Border color attribute of the header BORDERCOLOR

>>:  Description of the default transaction isolation level of mysql and oracle

Recommend

Installation steps of Ubuntu 20.04 double pinyin input method

1. Set up Chinese input method 2. Set the double ...

Implementing a puzzle game with js

This article shares the specific code of js to im...

Summary of ten principles for optimizing basic statements in MySQL

Preface In the application of database, programme...

Detailed explanation of several ways to create objects and object methods in js

This article is the second article about objects ...

Introducing multiple custom fonts in CSS3

Today I found a problem in HTML. There are many d...

Summary of the characteristics of SQL mode in MySQL

Preface The SQL mode affects the SQL syntax that ...

The use and difference between vue3 watch and watchEffect

1.watch listener Introducing watch import { ref, ...

Specific method to add foreign key constraints in mysql

The operating environment of this tutorial: Windo...

CSS positioning layout (position, positioning layout skills)

1. What is positioning? The position attribute in...

MySQL5.7 master-slave configuration example analysis

MySQL5.7 master-slave configuration implementatio...

How to implement scheduled backup of MySQL database

1. Create a shell script vim backupdb.sh Create t...

Methods and steps to access Baidu Maps API with JavaScript

Table of contents 1. Baidu Map API Access 2. Usin...

Sample code for implementing DIV suspension with pure CSS (fixed position)

The DIV floating effect (fixed position) is imple...

In-depth explanation of MySQL learning engine, explain and permissions

engine Introduction Innodb engine The Innodb engi...