图片拖拽添加

图片拖拽添加

效果图
拖拽不同的目标,生成动态的距离
功能截图
此小功能是vue写的

<div>
    <div class="login">
      <div>
        <div v-for="(item,index) in oldList" :key="'info1'+index">
          <img style="width:40px;" draggable="true" @mousedown="down(item.id)" :src="item.src" alt />
        </div>
        <div v-for="(item,index) in list" :key="'info2'+index">
          <!-- :style="{top:toptop+'px'}" -->
          <!-- :class="sstt[index]" -->
          <img
            class="box1"
            :id="item.id"
            draggable="true"
            :style="{top:item.toptop+'px'}"
            @drag="drop($event,index)"
            @dragstart="start($event,item)"
            @dragend="mouseup22(index)"
            :src="item.src"
            alt
          />
        </div>
      </div>
      <div>
        <img class="img_class" src="@/assets/img/1.jpg" alt />
      </div>
    </div>
    <el-table :data="list" style="width: 100%" border>
      <el-table-column prop="id" label="id" width="180"></el-table-column>
      <el-table-column prop="label" label="种类" width="180"></el-table-column>
      <el-table-column prop="left" label="" width="180"></el-table-column>
      <el-table-column prop="top" label=""></el-table-column>
    </el-table>
  </div>

js代码

export default {
  data() {
    return {
      list: [],
      sstt: ['ss1', 'ss2', 'ss3', 'ss4'],
      boxjq: null,
      topX: 0,
      topY: 0,
      allindex: 0,
      oldList: [
        { id: 1, src: require('@/assets/img/3.jpg') },
        { id: 2, src: require('@/assets/img/4.jpg') },
        { id: 3, src: require('@/assets/img/5.jpg') },
        { id: 4, src: require('@/assets/img/6.jpg') },
      ],
      toptop: 0,
    }
  },
  methods: {
    drop(e, index) {
      let pid = this.list[index].id
      this.boxjq = document.getElementById(pid)
      // console.log(item)
      let x = e.pageX
      let y = e.pageY
      // console.log(x + '-' + y)
      if (x === 0 && y === 0) {
        return
      }
      x = x - this.topX
      y = y - this.topY
      this.boxjq.style.left = x + 'px'
      this.boxjq.style.top = y + 'px'
      this.list[index].left = x
      this.list[index].top = y
    },
    mouseup22(index) {
      this.$nextTick(() => {
        this.list[index].left = this.boxjq.style.left
        this.list[index].top = this.boxjq.style.top
      })
      console.log(this.list)
    },
    start(e) {
      this.topX = e.offsetX
      this.topY = e.offsetY
    },
    down(id) {
      console.log(id)
      let pid = this.allindex++
      if (id == 1) {
        this.list.push({ id: 100 + pid, type: 1,label:'红绿灯', src: require('@/assets/img/3.jpg'),left:'',top:'', toptop: 0 })
      }
      if (id == 2) {
        this.list.push({ id: 200 + pid, type: 2, label:'转向灯',src: require('@/assets/img/4.jpg'),left:'',top:'', toptop: 100 })
      }
      if (id == 3) {
        this.list.push({ id: 300 + pid, type: 3,label:'自行车灯', src: require('@/assets/img/5.jpg'),left:'',top:'', toptop: 200 })
      }
      if (id == 4) {
        this.list.push({ id: 400 + pid, type: 4, label:'行人灯',src: require('@/assets/img/6.jpg'),left:'',top:'', toptop: 300 })
      }
    },
  },
}

css样式

<style lang="scss" scoped>
.img_class {
  width: 1500px;
}
.login {
  position: relative;
  width: 8000px;
  height: 600px;
  background-color: #999;
  display: flex;
}
.box1 {
  position: absolute;
  width: 40px;
  // height: 150px;
  top: 0px;
  left: 0px;
  background: red;
}
.sstt1 {
  top: 0px;
}
.sstt2 {
  margin-top: 100px;
}
.sstt3 {
  top: 200px;
}
.sstt4 {
  top: 300px;
}
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex-Gdd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值