gpt4 book ai didi

SVG 元素的 Javascript 工具提示

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:34:30 25 4
gpt4 key购买 nike

我有一个 SVG,它用作带有一些标题的顶部栏。它由许多带有一些文本的矩形组成,当用户将鼠标悬停在它上面时,我想为每个矩形显示工具提示。

我尝试实现类似 this 的东西但我需要将 .js 代码保存在单独的文件中,因为我正在动态生成我的 svg 文件。但是,当我将鼠标悬停在我的元素(svg 中的矩形)上时,没有任何反应。我认为问题可能在于在脚本中引用我的 svg,但我不确定哪里出了问题。

这是我的代码示例(为了保持可读性,我删除了一些不重要的部分。)

SVG:

    <svg contentScriptType="text/ecmascript" width="760" 
xmlns:xlink="http://www.w3.org/1999/xlink" zoomAndPan="magnify" contentStyleType="text/css"
height="140" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg"
onload="init(evt)" version="1.0">
<script xlink:href="script.js" xlink:actuate="onLoad" xlink:type="simple" xlink:show="other" type="text/ecmascript" xmlns:xlink="http://www.w3.org/1999/xlink"/><rect x="0" width="120" height="140" y="0"
style="fill:#DEE7EF"/><rect x="120" y="0" width="30" style="fill:#9CAAC6"
onmouseout="HideTooltip(evt)" height="140" onmousemove="ShowTooltip(evt,
&apos;BlueServiceESB#BlueListener&apos;)"><text fill="black" x="0" id="tooltip" font-
size="10" y="0" visibility="hidden">BlueServiceESB#BlueListener</text></rect></svg>

我知道它可能看起来令人困惑,如果是这样,我会尝试用其他一些东西替换我的文本元素以使其更具可读性,请在评论中告诉我...

我的script.js文件

// tooltip
function ShowTooltip(evt, mouseovertext)
{
tooltip.setAttribute("x",evt.clientX+11);
tooltip.setAttribute("y",evt.clientY+27);
tooltip.firstChild.data = mouseovertext;
tooltip.setAttribute("visibility","visible");
}

function HideTooltip(evt)
{
tooltip.setAttribute("visibility","hidden");
}

// init for tooltip functions
function init(evt)
{
if ( window.svgDocument == null )
{
svgDocument = evt.target.ownerDocument;
}

tooltip = svgDocument.getElementById('tooltip');

}

你能告诉我我做错了什么吗?非常感谢!

最佳答案

去掉init函数,每次只找tooltip元素。所以你的脚本看起来像:

function ShowTooltip(evt, mouseovertext) {
var tooltip = document.getElementById('tooltip');
tooltip.setAttribute("x", evt.clientX + 11);
tooltip.setAttribute("y", evt.clientY + 27);
tooltip.firstChild.data = mouseovertext;
tooltip.setAttribute("visibility", "visible");
}

function HideTooltip(evt) {
var tooltip = document.getElementById('tooltip');
tooltip.setAttribute("visibility", "hidden");
}

我认为 SVG 也存在一些问题(可能是因为它的生成方式)。最重要的一点是不要将 text 包裹在 rect 元素中。这有效:

    <svg width="760" height="140" xmlns="http://www.w3.org/2000/svg" version="1.0">

<rect x="0" width="120" height="140" y="0" style="fill:#DEE7EF" />
<rect x="120" y="0" width="30" height="140" style="fill:#9CAAC6"
onmouseout="HideTooltip(evt)"
onmousemove="ShowTooltip(evt, &apos;BlueServiceESB#BlueListener&apos;)" />

<text x="0" y="0" width="20" height="10" fill="black" id="tooltip" font-size="10" visibility="hidden">BlueServiceESB#BlueListener</text>
</svg>

关于SVG 元素的 Javascript 工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17542441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com