html - 在 SVG 中悬停在多边形上时显示文本

标签 html css svg hover

是否可以在悬停时添加带有 svg 多边形形状的文本?

我的代码是这样的

    <svg>
    <polygon class="st0" points="0,1.833 638,1.833 383,348.833 0,348.833 "/>
    <polygon class="st0" points="0,355.333 649,355.333 891.5,664.833 0,664.833 "/>
    <polygon class="st0" points="392.5,348.833 514.75,181.333 645.25,348.833 "/>
    <polyline class="st0" points="518.875,174.908 644.667,2.021 1139.833,1.52 1139.75,663.583 897.25,663.583 "/>
    </svg>

当我检查许多引用资料时,只有一个多边形的例子,但在我的代码中,我需要 4 个形状,在 SVG 标签中有不同的文本。是否可以在悬停时添加带有多个多边形的文本?

Here a fiddle what I have

当我悬停时我想要这样 enter image description here

如有任何建议,我们将不胜感激

最佳答案

你可以添加

<title>Your text</title>

标签内<svg></svg><poligon></poligon>显示带有文本的默认工具提示的标签。

已更新 jsfiddle

来源:

.st0 {
  fill: #0491B7;
}
.st1 {
  fill: #0491B7;
}
.st1:hover {
  fill: red;
  opacity: 0.5;
}
.st0:hover {
  fill: red;
}

text{
  display:none;
  fill:#fff;
  font-size:2em;
  font-family:sans-serif;
}

text.sub-text{
  display:none;
  fill:#fff;
  font-size:0.8em;
  font-family:sans-serif;
}

g:hover > text{
  display:block;
}
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1139.833px" height="663.313px" viewBox="0 0 1139.833 663.313" style="enable-background:new 0 0 1139.833 663.313;" xml:space="preserve">
 
  <g>
  <polygon class="st0" points="0,0.313 638,0.313 383,347.313 0,347.313 ">
    <title>One</title>
  </polygon>
  <text x="10" y="40"> TITLE </text>
    <text x="10" y="55" class="sub-text">Some Content</text>
    </g>

  <polygon class="st1" points="0,353.813 649,353.813 891.5,663.313 0,663.313 ">
    <title>Two</title>
  </polygon>

  <polygon class="st0" points="392.5,347.313 514.75,179.813 645.25,347.313 ">
    <title>Three</title>
  </polygon>

  <polyline class="st0" points="518.875,173.388 644.667,0.501 1139.833,0 1139.75,662.063 897.25,662.063">
    <title>Four</title>
  </polyline>
</svg>

关于html - 在 SVG 中悬停在多边形上时显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38222838/

相关文章:

javascript - 如何测试一个点是否在 SVG 闭合路径内

javascript - 使 JQuery 下拉代码可重用

html - 如何使用 Bootstrap 跨设备保留网格布局?

CSS : span in left with fixed width, span in right with fixed width, center table 填满剩余空间

javascript - 自动将 SVG 缩放到其父级

HTML:在线渲染 .svg 文件

html - CSS 对齐 html 中的文本 "line"

html - 我怎样才能模仿这个布局

html - textarea 位置不稳定

javascript - 较长名称的 JSTREE 链接/节点在 Chrome 中未正确呈现