简体   繁体   English

如何连接两个 HTML 元素避免其他元素

[英]How to connect two HTML elements avoiding other elements

I've this HTML/CSS structure: Codepen我有这个 HTML/CSS 结构: Codepen

I need to connect two HTML elements with a line when I choose them (clicked on first element and then second element).当我选择它们时,我需要用一条线连接两个 HTML 元素(点击第一个元素,然后点击第二个元素)。

I've already tried to draw straight line between them and it's successful.我已经尝试在它们之间划一条直线并且它成功了。 But the problem is, this line should avoid other HTML elements.但问题是,这一行应该避免其他 HTML 元素。

I'm choosing two elements like this:我选择了两个这样的元素:

let selected;
let count = 0;

$('a').on('click', function(){
    selected = $('.selected');
    if (!$(this).hasClass('selected') && count !== 2){
        count++;
        $(this).addClass('selected count' + count);
    } else {
        $(this).removeClass();
        count--;
    }

    if (count === 2) {
        // Here I'll draw line
    }
});

What you're describing would be extremely difficult to accomplish using html in the way you are.您所描述的内容将很难以您现在的方式使用 html 来完成。 If you wanted a curving line, that avoids other elements, you'd most likely have to use an SVG path and some sort of pathfinding algorithm to calculate where it should go.如果你想要一条避免其他元素的曲线,你很可能必须使用 SVG 路径和某种寻路算法来计算它应该去哪里。 Instead, I would recommend you try a solution using JavaScript and HTML5 Canvas.相反,我建议您尝试使用 JavaScript 和 HTML5 Canvas 的解决方案。 A good starting place might be examples from D3.js.一个好的起点可能是来自 D3.js 的示例 D3 is meant for exactly the sort of chart you're looking to create. D3正好适用于您要创建的图表类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM