gpt4 book ai didi

javascript - JS绘制一个箭头从一个div指向另一个

转载 作者:搜寻专家 更新时间:2023-10-31 08:52:10 25 4
gpt4 key购买 nike

我有以下代码:

<div id="parent">
<div class="child" id="air1">Medium 1</div>
<div class="child" id="glass">Medium 2</div>
<div class="child" id="air2">Medium 1</div>
</div>

<style>
#parent {
background: #999;
padding: 0px;
}
#glass {
background: #666;
}
.child {
background: #ccc;
height: 200px;
margin: 0px;
}
</style>

我想使用 svg 从 #air1 到 #glass 绘制一个箭头。我将以下代码添加到 div 中以绘制示例箭头:

<svg width="300" height="100">

<defs>
<marker id="arrow" markerWidth="13" markerHeight="13" refx="2" refy="6" orient="auto">
<path d="M2,2 L2,11 L10,6 L2,2" style="fill:red;" />
</marker>
</defs>

<path d="M30,150 L100,50"
style="stroke:red; stroke-width: 1.25px; fill: none;
marker-end: url(#arrow);"
/>

</svg>

虽然我不希望箭头指向随机方向,但我希望它像这样指向#glass:enter image description here

此外,我将如何着手绘制一个不那么陡峭的箭头: enter image description here

我该怎么做?

最佳答案

您可以通过使用 positioning 来实现(将 svg 插入第一部分并将其设置为 position: absolute;)并调整路径元素的偏移量。要使箭头指向下方,只需为路径描述属性的第二个值使用负值即可。

有关详细信息,请参阅 w3schools about path .

#parent {
background: #999;
padding: 0px;
}
#glass {
background: #666;
}
.child {
background: #ccc;
height: 200px;
margin: 0px;
position: relative;
}
svg {
position: absolute;
left: 0;
}
<div id="parent">
<div class="child" id="air1">Medium 1
<svg width="400" height="200">
<defs>
<marker id="arrow" markerWidth="13" markerHeight="13" refx="2" refy="6" orient="auto">
<path d="M2,2 L2,11 L10,6 L2,2" style="fill:red;" />
</marker>
</defs>
<path d="M-600,-10 L300,195" style="stroke:red; stroke-width: 1.25px; fill: none; marker-end: url(#arrow);" />
</svg>
</div>
<div class="child" id="glass">Medium 2</div>
<div class="child" id="air2">Medium 1</div>
</div>

关于javascript - JS绘制一个箭头从一个div指向另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39444579/

25 4 0
文章推荐: javascript - 清除搜索字段后,如何将
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com