vue+element ui实现图片上传并拖拽进行图片排序

本文介绍了如何在Vue2项目中使用Vue-dragging插件进行图片上传和排序,包括安装、引入步骤,以及在ElementUI组件中实现自定义拖拽功能的详细代码示例。
摘要由CSDN通过智能技术生成

用到的技术栈:

  • vue2
  • element Ui
  • vue-dragging

如何使用:

第一步: 安装

npm install awe-dnd --save

第二步: 引入

main.js 文件
// 引入组件
import VueDND from 'awe-dnd'
// 添加至全局
Vue.use(VueDND)

具体项目代码

<el-form-item label="封面图" prop="region">
    <div style="width: 100%;display: flex;">
       <div class="imgs">
             <!-- 上传后的图片显示在这里 -->
             <!-- : v-dragging="{ item: item, list: coverFileList, group: 'cover' }"
                这种形式进行指令绑定,其中 item 就是单个对象,而 list 则是数据列表,group 则是用来声明一个组,来保证可以在一个页面中进行多个数据源的操作。 -->
             <div :style="{ backgroundImage: 'url(' + item.url + ')' }" v-for="(item, index) in coverFileList"
                v-dragging="{ item: item, list: coverFileList, group: 'cover' }" :key="index">
                <i class="el-icon-circle-close" @click="coverDelete(index)"></i>
             </div>
             <!-- 调用element ui上传组件 -->
             <!-- show-file-list	是否显示已上传文件列表 -->
             <!-- 这里不用组件的显示,用我们自己写的,便于实现拖住 -->
             <el-upload drag :action="action" multiple :name="file_name" :show-file-list="false"
                list-type="picture-card" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
                accept=".jpg,.png,.jpeg" :on-error="coverError">
                <span>上传图片</span>
             </el-upload>
         </div>
      </div>
    <div style="font-size: 14px;color: #909399;">注:最多可上传9张图片,单张大小需限制在10M以内,拖动可进行排序</div>
</el-form-item>
<script>
	export default{
		name: 'addResource',
		data(){
			retutn{
				coverFileList: [], //封面图图片集
			}
		},
		methods:{
			//上传成功
			handleAvatarSuccess(res, file){
				if (res.status == 0) {
            		this.$message.error('上传失败');
            		return
         		}
         		let item = Object.assign({ 'name': file.name, 'url': res.data.url })
         		this.coverFileList.push(item);
			},
			//上传文件之前 根据自己业务补充
			 beforeAvatarUpload (file){
			 	//判断上传有没有超过次数限制
			 	//验证图片格式和大小等逻辑
			 },
			 //上传失败函数
			 coverError (err, file, fileList){
			 	this.$message.error('上传失败');
			 }//删除上传的图片
			 coverDelete (index) {
			 	this.coverFileList.splice(index, 1);
			 }
		}
	}
</script>
<style lang="scss" scoped>
 .imgs {
         display: flex;
         flex-wrap: wrap;

         div {
            width: 148px;
            height: 148px;
            border-radius: 6px;
            margin-right: 10px;
            background-size: cover;
            background-repeat: no-repeat;
            // border: 1px solid #c0ccda;
            position: relative;
            margin-bottom: 20px;

            i {
               font-size: 20px;
               position: absolute;
               right: -5px;
               top: -5px;

               &:hover {
                  cursor: pointer;
                  color: #409EFF;
               }
            }
         }
      }
</style>

效果展示:

Video_23-11-13_10-17-30


end~~~

如有错误或观点不一致的请评论留言共同讨论,本人前端小白一枚,根据自己实际项目遇到的问题进行总结分享,谢谢大家的阅读!

  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Element UI 提供了 el-dialog 组件,但是默认情况下不支持拖拽,需要自己进行实现。下面是实现 el-dialog 拖拽的步骤: 1. 在 el-dialog 的 title 区域添加一个拖拽的区域,可以使用 CSS 的 cursor 属性来设置鼠标样式。 ```html <template> <el-dialog :visible.sync="dialogVisible" title="可拖拽的对话框"> <div class="dialog-header" @mousedown="startDrag"> <span>可拖拽的对话框</span> </div> <div> <!-- 对话框内容 --> </div> </el-dialog> </template> <style> .dialog-header { cursor: move; } </style> ``` 2. 在 startDrag 方法中记录鼠标按下的位置和对话框的位置。 ```js export default { data() { return { dialogVisible: false, dragging: false, // 是否正在拖拽 dragX: 0, // 鼠标按下时的位置 dragY: 0, dialogLeft: 0, // 对话框的位置 dialogTop: 0, }; }, methods: { startDrag(e) { this.dragging = true; this.dragX = e.clientX; this.dragY = e.clientY; this.dialogLeft = this.$refs.dialog.getBoundingClientRect().left; this.dialogTop = this.$refs.dialog.getBoundingClientRect().top; }, }, }; ``` 3. 在监听 document 的 mousemove 和 mouseup 事件,在 mousemove 事件中计算出对话框应该移动到的位置,在 mouseup 事件中停止拖拽。 ```js methods: { startDrag(e) { // ... document.addEventListener('mousemove', this.drag); document.addEventListener('mouseup', this.stopDrag); }, drag(e) { if (this.dragging) { const offsetX = e.clientX - this.dragX; const offsetY = e.clientY - this.dragY; this.$refs.dialog.style.left = `${this.dialogLeft + offsetX}px`; this.$refs.dialog.style.top = `${this.dialogTop + offsetY}px`; } }, stopDrag() { this.dragging = false; document.removeEventListener('mousemove', this.drag); document.removeEventListener('mouseup', this.stopDrag); }, } ``` 完整代码如下: ```html <template> <el-dialog :visible.sync="dialogVisible" title="可拖拽的对话框" ref="dialog"> <div class="dialog-header" @mousedown="startDrag"> <span>可拖拽的对话框</span> </div> <div> <!-- 对话框内容 --> </div> </el-dialog> </template> <style> .dialog-header { cursor: move; } </style> <script> export default { data() { return { dialogVisible: false, dragging: false, // 是否正在拖拽 dragX: 0, // 鼠标按下时的位置 dragY: 0, dialogLeft: 0, // 对话框的位置 dialogTop: 0, }; }, methods: { startDrag(e) { this.dragging = true; this.dragX = e.clientX; this.dragY = e.clientY; this.dialogLeft = this.$refs.dialog.getBoundingClientRect().left; this.dialogTop = this.$refs.dialog.getBoundingClientRect().top; document.addEventListener('mousemove', this.drag); document.addEventListener('mouseup', this.stopDrag); }, drag(e) { if (this.dragging) { const offsetX = e.clientX - this.dragX; const offsetY = e.clientY - this.dragY; this.$refs.dialog.style.left = `${this.dialogLeft + offsetX}px`; this.$refs.dialog.style.top = `${this.dialogTop + offsetY}px`; } }, stopDrag() { this.dragging = false; document.removeEventListener('mousemove', this.drag); document.removeEventListener('mouseup', this.stopDrag); }, }, }; </script> ``` 这样就实现了 el-dialog 的拖拽功能。注意,由于拖拽是直接修改 DOM 元素的样式,如果使用了动画或者过渡效果,可能会有一些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值