图片拖拽添加
效果图
拖拽不同的目标,生成动态的距离
此小功能是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>