vue实现拖动盒子修改数组顺序

<div class="g-auto-bottom">
	<ul class="selected">
		<li class="g-project-item active" :class="{'grayLi':index==1||index==0||index==2}"
			v-for="(item,index) in selectList" :key="item.showConfigFieldId"
			:data-index="index" :draggable="index>2"
			@dragstart.stop="changeDragStart($event,'selected',index)"
			@dragend.stop="changeDragEnd($event,index)"
		    @dragover.stop="handleDragOver($event,selectList,'selected',index)">
			{{item.showConfigFieldDesc}}
	    </li>
	</ul>
</div>
///vue data里边需要加上
data() {
  return {
      selectList:[
		{showConfigFieldDesc:'1111'},
		{showConfigFieldDesc:'1222'},
		{showConfigFieldDesc:'1333'},
		{showConfigFieldDesc:'1444'},
	  ],
	  typeDrag: {
        startIndex: -1,//被拖拽元素的索引
         activeIndex: 0,//目标元素的索引
       },
   }
}
//方法里加上methods
methods:{
	changeDragStart(el,active,index) { //开始拖动时的数据
        if(index==0){
          return
        }
        this.activeDrag = active;
        this.startPageX = el.pageX;
        this.startPageY = el.pageY;
        this.findParent(el.target);
        this.typeDrag.startIndex = this.parentnode.getAttribute('data-index');

      },
      changeDragEnd(el,index) {
        if(index==0){
          return
        }
        this.typeDrag.startIndex = -1;
      },
      handleDragOver(el,list,active,index) {
        if(index==0 || index==1 || index==2){ //限制前几个盒子不可拖动
          return
        }
        el.preventDefault();//重要
        el.dataTransfer.dropEffect = 'move';
        this.findParent(el.target);
        const pageX = el.pageX - this.startPageX, pageY = el.pageY - this.startPageY;
        const offsetWidth = this.parentnode.offsetWidth / 2, offsetHeight = this.parentnode.offsetHeight / 2;
        this.typeDrag.activeIndex = this.parentnode ? this.parentnode.getAttribute('data-index') : null;
        if (this.typeDrag.activeIndex != null) {
          let x = this.typeDrag.startIndex, y = this.typeDrag.activeIndex;
          if (pageY > offsetHeight || pageY < -offsetHeight) {
            this.startPageY = el.pageY;
            list.splice(y, 0, ...list.splice(x, 1));
            this.typeDrag.startIndex = y;
          } else if (pageX > offsetWidth || pageX < -offsetWidth) {
            this.startPageX = el.pageX;
            list.splice(y, 0, ...list.splice(x, 1));
            this.typeDrag.startIndex = y;
          }
        }
      },
      findParent(node) {
        if (node.className.indexOf('g-project-item') > -1) {
          this.parentnode = node
        } else {
          this.parentnode = false;
          if (node.parentNode.nodeName.toLowerCase() != 'body') {
            this.findParent(node.parentNode)
          }
        }
      },

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值