时间: 2020-11-25|42次围观|0 条评论

<template>
  <section class="main">
    <div class="drag-box-left">
      <div class="drag-title">shit~拖动排序</div>
      <div class="drag-list" draggable="true" :key="list.id" v-for="(list,index) in data1" 
             @dragstart="ondragstart($event,index,list)"                          
             @dragend="ondragend($event,index,list)"
        @dragenter="ondragenter($event,index,list)"
        @dragleave="ondragleave($event,index,list)"
        @drop="ondrop($event,index,list)"
        @dragover="ondragover"
      >{
   {list.title}}</div>
    </div>
  </section>
</template>
 

<script>
  export default {
    data() {
      return {
        dragElement: null,
        index: null,
        list: null,
        lock: true,
        data1: [
          { id: 1, title: '1', type: 'bg' },
          { id: 2, title: '2', type: 'bg' },
          { id: 3, title: '3', type: 'bg' },
          { id: 4, title: '4', type: 'fe' },
          { id: 5, title: '5', type: 'fe' },
          { id: 6, title: '6', type: 'fe' }
        ]
      }
    },
    methods: {
      // ondragstart - 用户开始拖动元素时触发
      // ondrag - 元素正在拖动时触发
      // ondragend - 用户完成元素拖动后触发
      // ondragenter - 当被鼠标拖动的对象进入其容器范围内时触发此事件
      // ondragover - 当某被拖动的对象在另一对象容器范围内拖动时触发此事件
      // ondragleave - 当被鼠标拖动的对象离开其容器范围内时触发此事件
      // ondrop - 在一个拖动过程中,释放鼠标键时触发此事件
      ondragstart(ev, index, list) {
        const self = this
        self.dragElement = ev.target
        self.oldIndex = index
        self.oldList = list
      },
      ondragend(ev, index, list) {
        ev.preventDefault()
      },
      ondragenter(ev, index, list) {
        const self = this
        if (self.oldIndex !== index) {
          ev.target.style.backgroundColor = '#999'
        }
      },
      ondragover(ev) {
        ev.preventDefault()
      },
      ondragleave(ev, index, list) {
        ev.target.style.backgroundColor = '#fff'
      },
      ondrop(ev, index, list) {
        const self = this
        if (self.oldIndex !== index && self.oldList.type === list.type) {
          // 当前拖动不等于拖动目标位置
          this.data1.splice(self.oldIndex, 1)
          this.data1.splice(index, 0, self.oldList)
          self.oldIndex = null
          self.oldList = null
        } else if (self.oldList.type !== list.type) {
          ev.preventDefault()
          alert('我是泳池排队最帅的,不能插队')
        }
        ev.target.style.backgroundColor = '#fff'
      }
    }
  }
</script>
 

<style scoped>
  .drag-box-left {
    float: left;
    width: 45%;
  }
  .drag-box-right {
    float: right;
    width: 45%;
  }
  .drag-list {
    border: 1px solid #ddd;
    padding: 10px;
    margin-bottom: 20px;
    transition: border 0.3s;
  }
  .drag-list:hover {
    border: 1px solid #20a0ff;
  }
  .drag-title {
    font-weight: 400;
    line-height: 25px;
    margin: 10px 0;
    font-size: 22px;
    color: #1f2f3d;
  }
</style>
 

 

转载于:https://www.cnblogs.com/huangzuowen/p/10050489.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/97782807

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《Vue拖动功能
 

还没有人抢沙发呢~