VUE+ElementUI+VueDraggable +El-ImageViewer 实现图片批量上传,支持拖拽控制顺序及图片预览

该文章展示了一个使用Vue.js和vuedraggable组件实现的功能,包括图片上传、图片列表拖动排序以及使用El-ImageViewer组件进行图片预览。用户可以选择图片,通过拖动来调整图片列表顺序,点击图片可预览原图。

话不多说,上代码,没安vuedraggable的小伙伴们自行安装一下

npm install vuedraggable

具体实现:

<template>
    <div>
        <el-upload
            :before-upload="handleBeforeUpload"
            :on-change="handleChange"
            multiple
            action="">
            <el-button>选择图片</el-button>
        </el-upload>

        <div class="image-list">
            <draggable v-model="imageList" @end="handleDragEnd">
                <div v-for="(image, index) in imageList" :key="index" class="image-item">
                    <img width="200px" height="150px" :src="image.url" alt="Image" @click="handleImageClick(index)"/>
                </div>
            </draggable>
        </div>

        <el-button @click="handleUpload">上传</el-button>
        <el-image-viewer
            v-if="viewerVisible"
            :on-close="closeViewer"
            :url-list="getImageShowList"
            :initial-index="currentIndex"/>
    </div>
</template>

<script>
import draggable from 'vuedraggable';
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
    components: {
        draggable,
        ElImageViewer
    },
    data() {
        return {
            fileList: [], // 选择的文件列表
            imageList: [], // 图片列表
            viewerVisible: false, // 预览框可见性
            currentIndex: 0 // 预览图片的索引
        };
    },
    computed:{
        getImageShowList(){
            return this.imageList.map(image => image.url);
        }
    },
    methods: {
        handleBeforeUpload(file) {
            // 添加文件到选择的文件列表
            this.fileList.push(file);
            return false; // 阻止自动上传
        },
        handleChange(file, fileList) {
            debugger;
            // 更新图片列表
            this.imageList.push({
                url: URL.createObjectURL(file.raw),
                file: file
            });
        },
        handleDragEnd() {
            // 拖动结束时更新文件列表顺序
            this.fileList = this.imageList.map(image => image.file);
            debugger;
        },
        handleUpload() {
            // 上传所有图片 这里只做了打印,具体上传逻辑请自行实现
            this.fileList.forEach(file => {
                // 使用你的上传逻辑,例如发送请求到后端进行上传
                console.log('Uploading file:', file);
            });
        },
        // 点击图片时显示原图
        handleImageClick(index) {
            // 点击图片时显示原图
            this.currentIndex = index;
            this.viewerVisible = true;
        },
        // 关闭预览
        closeViewer() {
            this.viewerVisible = false;
        }

    }
};
</script>

<style>
.image-list {
    display: flex;
    flex-wrap: nowrap;
}

.image-item {
    display: inline-block;
    margin-right: 10px;

}
</style>

简单的效果图如下:

控制了下图片显示的大小,另支持点击预览功能,又新认识了个很好用的组件:El-ImageViewer。 

`el-image-viewer` 是 Element UI 提供的一个图片查看器组件,允许用户在一个模态框中放大查看图片支持手势滑动切换图片,具有缩放、拖拽、多图浏览等功能,适用于电子商务网站的产品详情页、在线相册展示等场景 [^1]。以下是不同 Vue 版本中使用 `el-image-viewer` 实现图片放大缩小预览的方法: ### Vue 2 + Element UI ```vue <template> <div> <!-- 大图预览 --> <el-image-viewer v-if="showViewer" :on-close="closeViewer" :url-list="viewPhotoList" /> <template v-for="(item, index) in list" :key="index"> <img v-if="index == 0" class="item-img" :src="item" @click="onPreview(list, index)" /> </template> </div> </template> <script> export default { data() { return { showViewer: false, viewPhotoList: [], list: ['image1.jpg', 'image2.jpg', 'image3.jpg'] }; }, methods: { onPreview(url, index) { if (!index && index != 0) { this.viewPhotoList = [url]; } else { let list = []; for (let i = 0; i < url.length; i++) { list.push(url[i]); } this.viewPhotoList = list; } this.showViewer = true; }, closeViewer() { this.showViewer = false; } } }; </script> ``` ### Vue 3 + ElementPlus ```vue <template> <div> <el-image-viewer v-if="showImagePreview" :url-list="showsrcListref" hide-on-click-modal teleported @close="closePreview" style="z-index: 3000" /> <template v-for="(item, index) in list" :key="index"> <img v-if="index == 0" class="item-img" :src="item" @click="onPreview(list, index)" /> </template> </div> </template> <script setup> import { ref } from 'vue'; const showImagePreview = ref(false); const showsrcListref = ref([]); const list = ref(['image1.jpg', 'image2.jpg', 'image3.jpg']); const onPreview = (url, index) => { if (!index && index != 0) { showsrcListref.value = [url]; } else { let list = []; for (let i = 0; i < url.length; i++) { list.push(url[i]); } showsrcListref.value = list; } showImagePreview.value = true; }; const closePreview = () => { showImagePreview.value = false; }; </script> ``` ### 代码解释 - `v-if` 用于控制 `el-image-viewer` 的显示与隐藏。 - `:url-list` 绑定图片路径数组,用于展示图片- `@click` 绑定点击事件,点击图片时触发预览功能。 - `@close` 绑定关闭事件,关闭预览窗口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值