Vue2题目拖拽排序组件

1、组件

<template>
  <div>
    <div>
      <div class="num-draggable">
        <div v-for="(item, index) in numbers" :key="index"
             class="list-item"
             :class="{'unDrag' : item.use === 1}"
             :draggable="item.use == 0"
             @dragstart="dragStart(item)"
             @dragend="dragEnd"
             @drop="drop"
             @dragover="dragEnd"
        >
          0{{ item.value }}
        </div>
      </div>
    </div>

    <div class="quesOpt">
      <div class="opt-content">

        <div class="box-item" v-for="(item, index) in options" :key="index">
          <div class="item-index uncheck-item-list" @dragover="dragEnd" @drop="dropEnter(item)" v-if="item.idx == '0'">
          </div>
          <div class="list-item item-index" draggable="true" @dragstart="backStart(item)" @dragover="dragEnd"
               @drop="dropEnter(item)" v-if="item.idx != '0'">
            0{{ item.idx }}
          </div>
          <div class="choose">
            <div class="pic">
              <img draggable="false" :src="item.filePath">
            </div>
            <div class="word">
              {{ item.title }}
            </div>
          </div>
        </div>
      </div>

    </div>

  </div>
</template>

<script>

export default {
  name: "dragSortQue",
  components: {},
  props: {
    options: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      numbers: [
        // {
        //   value: 1,
        //   use: 0
        // }
      ],
      draggingIndex: -1,
      backItem: null
    };
  },
  mounted() {
    this.initNum();
  },
  methods: {
    initNum() {
      let arr = [];
      this.numbers = [];
      this.options.forEach((item, index) => {
        let x = (index + 1) + ''
        let obj = {
          value: x,
          use: 0
        }
        this.numbers.push(obj);
        if (item.idx != '0'){
          arr.push(item.idx);
        }
      });

      for (let i = 0; i < arr.length; i++) {
        let num = arr[i];
        this.numbers.forEach((item, index) => {
          if (num == item.value){
            item.use = 1;
          }
        })
      }
    },
    dragStart(item) {
      this.draggingIndex = item.value;
    },
    backStart(item) {
      this.backItem = item;
    },
    drop(e) {
      if (this.backItem == null) {
        return;
      }
      this.numbers.forEach((item, index) => {
        if (item.value == this.backItem.idx) {
          item.use = 0;
        }
      });

      this.options.forEach((item, index) => {
        if (item.id == this.backItem.id) {
          item.idx = 0;
        }
      });
      this.$emit('getDrag', this.options)
    },
    dragEnd(e) {
      e.preventDefault();
    },
    dropEnter(obj) {
      if (this.draggingIndex <= 0) {
        return;
      }

      this.numbers.forEach((item, index) => {
        if (item.value == obj.idx) {
          item.use = 0;
        }
        if (item.value == this.draggingIndex) {
          item.use = 1;
        }
      });
      obj.idx = this.draggingIndex;

      this.draggingIndex = -1;

      this.$emit('getDrag', this.options)

    }
  },
  watch: {
    options(newVal, oldVal) {
      this.initNum();
    }
  }
}
</script>

<style scoped>
.list-item {
  width: 44px;
  height: 44px;
  line-height: 44PX;
  background-image: url("../../../../../public/imgs/bgNum.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  color: #fff;
  font-size: 25px;
  font-weight: 700;
  text-align: center;
  margin: 10px;
  cursor: pointer;
}

.uncheck-item-list {
  width: 100%;
  height: 100%;
  line-height: 34PX;
  //border: 1px dashed #c0c4cc;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  color: #fff;
  font-size: 20px;
  font-weight: 500;
  text-align: center;
  margin: 10px;
  cursor: pointer;
}

.item-index {
  position: absolute;
  top: -20px;
  left: -20px;
  z-index: 999;
}

img {
  width: 28.5rem;
  height: 17.8rem;
}

.quesOpt {
  display: flex;
  margin-top: 30px;

  .opt-content {
    display: flex;
    flex-wrap: wrap;

    .box-item {
      position: relative;
      width: 27.5rem;
    }
  }
}

.choose {
  margin-right: 20px;
  padding-bottom: 22px;

  .pic {
    text-align: center;
    position: relative;
    width: 28.5rem;
    height: 17.8rem;
  }

  .word {
    color: black;
    line-height: 22px;
    margin-top: 10px;
    margin-left: 1rem;
  }
}

.num-draggable {
  display: flex;
  justify-content: center;
  width: 95%;
  height: 60px;
  border: 5px solid #00827A;
  border-radius: 30px;
}

.unDrag {
  filter: grayscale(1);
  cursor: none !important;
}
</style>

2、使用

    <drag-sort-que :options="vm.activeOptions" @getDrag="getDrag"></drag-sort-que>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值