在draggable和droppable之间画一条线,可以使用HTML5的Canvas API或者SVG来实现。下面是一个简单的示例,使用Canvas API绘制一条连接draggable和droppable的线。
<div id="draggable1" draggable="true">Draggable 1</div>
<div id="draggable2" draggable="true">Draggable 2</div>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const draggable1 = document.getElementById('draggable1');
const draggable2 = document.getElementById('draggable2');
let startX, startY, endX, endY;
draggable1.addEventListener('dragstart', (event) => {
startX = event.clientX;
startY = event.clientY;
});
draggable1.addEventListener('dragend', () => {
endX = startX;
endY = startY;
drawLine();
});
draggable2.addEventListener('dragstart', (event) => {
startX = event.clientX;
startY = event.clientY;
});
draggable2.addEventListener('dragend', () => {
endX = startX;
endY = startY;
drawLine();
});
function drawLine() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
ctx.stroke();
}
这个示例中,我们使用了Canvas API来绘制一条连接draggable和droppable的线。当用户拖动draggable元素时,我们会记录鼠标的位置,并在拖动结束时更新线条的位置。这样就可以在draggable和droppable之间画一条线了。
需要注意的是,这只是一个简单的示例,实际应用中可能需要更多的逻辑来处理不同的情况,比如拖动时线条的实时更新、线条的样式等等。
领取专属 10元无门槛券
手把手带您无忧上云