简体   繁体   English

在SVG路径中嵌套文本

[英]Nest text inside SVG path

Is it possible to nest text (like a text element) inside an SVG path element? 是否可以在SVG path元素中嵌套文本(如text元素)?

I am asking because I would like a text balloon to show up when hovering over a path, something like this: 我在问,因为我希望在悬停在路径上时显示一个文本气球,如下所示:

path#mypath:hover text {
    display:block;
}

I would like to avoid using JavaScript, but I understand that may be the only option. 我想避免使用JavaScript,但我知道这可能是唯一的选择。

According to this: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path your can not nest <text> inside <path> 根据这个: https//developer.mozilla.org/en-US/docs/Web/SVG/Element/path你不能在<path>里面嵌套<text> <path>

You could however use an adjacent element as the trigger for the hover effect: http://jsfiddle.net/93ufH/1/ 但是,您可以使用相邻元素作为悬停效果的触发器: http//jsfiddle.net/93ufH/1/

<svg>
    <rect x="10" y="10" width="100" height="100"/>
    <text x="0" y="50" font-family="Verdana" font-size="55" fill="blue" > 
        Hello
    </text>
</svg>

CSS CSS

svg text{
    visibility:hidden;
}

svg rect:hover + text{
    visibility:visible;
}

If you want to display tooltip, You can insert title element into target shape element, like this. 如果要显示工具提示,可以将title元素插入目标形状元素,如下所示。

<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <circle r="50" cx="50" cy="50">
        <title>tooltip</title>
    </circle>
</svg>

Yes you can use <text> inside <svg> . 是的,您可以在<svg>使用<text> <svg>

You can use: 您可以使用:

svg text:hover{
    fill:red;
}

To apply css rules when hovered. 在悬停时应用css规则。 Here is working JSFiddle . 这是JSFiddle的工作。 But if you want to display a bubble notification that changes position based on hovered element, you must use JavaScript. 但是,如果要显示基于悬停元素更改位置的气泡通知,则必须使用JavaScript。 If the position does not change then you can do it in CSS by simply hiding and displaying a div. 如果位置没有改变,那么只需隐藏并显示div就可以在CSS中完成。

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

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