用vant uploader怎么实现上传图片并可拖曳图片设封面
Admin 2022-06-28 群英技术资讯 364 次浏览
效果图如下所示:
<!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, //当前页面注册拖拽组件 }, 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) { // 上传图片到图片服务器 this.fileList.push(file) console.log(file) }, // 点击预览 previewImg(images, index) { let listSrc = []; this.fileList.forEach(function(v,i){ listSrc.push(v.content) }) vant.ImagePreview({ images:listSrc, startPosition: index, closeable:true }); }, // 拖拽结束后 获取刚刚拖动的数据 end(e) { var _oldIndex = e.oldIndex var _oldItem = this.fileList[_oldIndex] console.log(_oldItem); }, } }) </script> </html>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
js中!的用法是比较灵活的,它除了做逻辑运算常常会用!做类型判断,可以用!与上对象来求得一个布尔值,
这篇文章主要介绍了JavaScript二叉树及各种遍历算法详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
这篇文章主要介绍了vue3+typeScript穿梭框的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
怎样用JS实现复选框的全选功能?复选框的全选功能便于多选操作,也是比较实用的一个功能,对此这篇文章就给大家分享JS实现复选框的全选的代码,感兴趣的朋友就接着往下看吧。
这篇文章主要为大家详细介绍了vue车牌输入组件的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008