简体   繁体   English

用于SVG元素的Javascript工具提示

[英]Javascript tooltips for SVG elements

I have one SVG which works as a top bar with some headlines. 我有一个SVG作为顶部栏,有一些头条新闻。 It consists of many rectangles with some text and I would like to show tooltips for each of them when user mouseover it. 它由许多带有一些文本的矩形组成,我想在用户鼠标悬停时显示每个矩形的工具提示。

I tried to implement something like this but I need to keep the .js code in separate file, because I am generating my svg files dynamically. 我想实现像这样 ,但我需要保持的.js代码在单独的文件,因为我动态生成的我的SVG文件。 However, nothing happens when I mouseover my elements (rectangles in the svg). 但是,当鼠标悬停在我的元素(svg中的矩形)时,没有任何反应。 I think taht the problem micht be with referencing my svg in the script but I am not sure what is wrong. 我认为问题是在脚本中引用我的svg,但我不确定是什么问题。

Here is the exmaple of my code (I deleted some non-important parts to keep it readable.) 这是我的代码的例子(我删除了一些非重要的部分以保持其可读性。)

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>

I know it might look confusing, if so, I will try to replace my text elements with some other stuff to make it more readable, let me know in comments... 我知道它可能看起来很混乱,如果是这样,我会尝试用其他东西替换我的文本元素以使其更具可读性,请在评论中告诉我...

My script.js file 我的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');

}

Could you tell me what am I doing wrong here? 你能告诉我这里我做错了什么吗? Thanks a lot! 非常感谢!

Remove the init function and just find the tooltip element each time. 删除init函数,每次只找到tooltip元素。 So your script will look like: 所以你的脚本看起来像:

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");
}

I think there's also a couple of issues with the SVG (maybe because of the way it's generated). 我认为SVG也存在一些问题(可能是因为它的生成方式)。 The most important point is to not wrap the text in the rect element. 最重要的一点是不要将text包装在rect元素中。 This works: 这有效:

    <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) First off all you should use ';' 1)首先,你应该使用';' ( style="fill:#9CAAC6;" ) in style attribute and every js code ( onmouseout="HideTooltip(evt);" ) (style =“fill:#9CAAC6;”)在style属性和每个js代码中(onmouseout =“HideTooltip(evt);”)

2) Change 2)改变

onmouseout="HideTooltip(evt)"

to

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

3) I think that easy to use jquery instead native js 3)我认为易于使用的jquery而不是原生的js

I) Add an id for your shape(in your case rect) I)为你的形状添加一个id(在你的情况下为rect)

II) Make II)制作

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

As an example take a look at http://jqvmap.com/ . 举个例子来看看http://jqvmap.com/ Tooltip implemented there as i wrote above. 如上所述,工具提示在那里实现。

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

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