用于SVG元素的Javascript工具提示

时间:2013-07-09 07:28:50

标签: javascript svg

我有一个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');

}

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

2 个答案:

答案 0 :(得分:2)

删除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>

答案 1 :(得分:-1)

1)首先,你应该使用';' (style =“fill:#9CAAC6;”)在style属性和每个js代码中(onmouseout =“HideTooltip(evt);”)

2)改变

onmouseout="HideTooltip(evt)"

onmouseout="HideTooltip(evt); return false;"

3)我认为易于使用的jquery而不是原生的js

I)为你的形状添加一个id(在你的情况下为rect)

II)制作

$('#rect_id').on('mouseover',function(e){
   // do your stuff here
});
$('#rect_id').on('mouseout',function(e){
   // do your stuff here
});

作为示例,请查看http://jqvmap.com/。如上所述,工具提示在那里实现。

相关问题