vue 拖拽产生连线_基于Vue实现拖拽功能

{bind:function (el,binding) {

let oDiv = el; //当前元素

let self = this; //上下文

oDiv.onmousedown = function (e) {

//鼠标按下,计算当前元素距离可视区的距离

let disX = e.clientX - oDiv.offsetLeft;

let disY = e.clientY - oDiv.offsetTop;

document.onmousemove = function (e) {

//通过事件委托,计算移动的距离

let l = e.clientX - disX;

let t = e.clientY - disY;

//移动当前元素

oDiv.style.left = l + 'px';

oDiv.style.top = t + 'px';

//将此时的位置传出去

binding.value({x:e.pageX,y:e.pageY})

};

document.onmouseup = function (e) {

document.onmousemove = null;

document.onmouseup = null;

};

};

}

}

);

window.onload = function () {

let vm = new Vue({

el: '#box',data: {

val: '123',style: {

width: '100px',height: '100px',background: 'aqua',position: 'absolute',right: '30px',top: 0

}

},methods:{

//接受传来的位置数据,并将数据绑定给data下的val

greet(val){

vm.val = val;

}

},});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用 Vue 实现拖拽产生连线的示例代码: ```html <template> <div class="container"> <div class="box" v-for="(item, index) in boxes" :key="index" :style="{ top: item.top + 'px', left: item.left + 'px' }" @mousedown="startDrag($event, item)"> <span>{{ index }}</span> </div> <svg class="line-container"> <line v-for="(line, index) in lines" :key="index" :x1="line.x1" :y1="line.y1" :x2="line.x2" :y2="line.y2" /> </svg> </div> </template> <script> export default { data() { return { boxes: [ { top: 50, left: 50 }, { top: 50, left: 200 }, { top: 200, left: 50 }, { top: 200, left: 200 } ], lines: [] } }, methods: { startDrag(event, item) { const startX = event.pageX const startY = event.pageY const startTop = item.top const startLeft = item.left const moveHandler = (event) => { const deltaX = event.pageX - startX const deltaY = event.pageY - startY item.top = startTop + deltaY item.left = startLeft + deltaX } const upHandler = () => { document.removeEventListener('mousemove', moveHandler) document.removeEventListener('mouseup', upHandler) this.updateLines() } document.addEventListener('mousemove', moveHandler) document.addEventListener('mouseup', upHandler) }, updateLines() { const boxes = this.$el.querySelectorAll('.box') const lines = [] for (let i = 0; i < boxes.length; i++) { const box1 = boxes[i] const box1Rect = box1.getBoundingClientRect() for (let j = i + 1; j < boxes.length; j++) { const box2 = boxes[j] const box2Rect = box2.getBoundingClientRect() const line = { x1: box1Rect.left + box1Rect.width / 2, y1: box1Rect.top + box1Rect.height / 2, x2: box2Rect.left + box2Rect.width / 2, y2: box2Rect.top + box2Rect.height / 2 } lines.push(line) } } this.lines = lines } } } </script> <style> .container { position: relative; width: 400px; height: 400px; background-color: #f1f1f1; } .box { position: absolute; width: 50px; height: 50px; background-color: #fff; border: 1px solid #ddd; display: flex; justify-content: center; align-items: center; cursor: move; } .box span { font-size: 20px; } .line-container { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; } </style> ``` 这里的核心思路是,当鼠标按下时记录当前元素的位置,然后在鼠标移动时计算偏移量,从而更新元素的位置。每次更新位置时,都重新计算连线的位置。具体实现方法是,使用 `getBoundingClientRect()` 方法获取元素的位置和尺寸,然后计算两个元素的中心点坐标,从而确定连线的起点和终点坐标。最后用 SVG 的 `line` 元素来绘制连线
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值