如何将一组 SVG 路径附近的所有 space 变成可点击的 link,而不仅仅是 SVG 笔划本身?
How to make the all the space near a group of SVG paths into a clickable link, not just the SVG stroke itself?
我有几个 svg <path>
元素组合成两个单独的组 <g>
元素(ID 是 po
和 ph
)。两者都在另一个组中,所有内容都在一个 <svg>
.
中
HTML 看起来像这样:
<!DOCTYPE html>
<html lang="en">
<head>
<link href="stripped_back_CSS.css" rel="stylesheet">
</head>
<body>
<div id="container">
<div id="content" class="hide">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
<style type="text/css">
.st5{fill:none;stroke:red;stroke-miterlimit:10;}
</style>
<g id="page">
<g id="po">
<path class="st5" d="M382.1,465.4c1.1,2.6,1.3,7.3,1.4,9.2c0.1-3.3-4-13,2.7-13.1c5.3-0.1,4.4,8.5-2.7,6.7"/>
<path class="st5" d="M395.3,464.1c-4.9,0.9-2.3,8,2.3,5.7c4.7-2.3-0.3-7.5-3.8-3.8"/>
</g>
<g id="ph">
<path class="st5" d="M392.4,522c0,3.2,0.4,6.6,1.1,9.7c-3.7-3.9-2.3-16.4,4.9-11.3c4.1,2.8-3.3,7.3-6.6,5.5"/>
<path class="st5" d="M402.8,519.3c1,3.1,0.3,6.4,2.2,9.3"/>
<path class="st5" d="M410.5,517.6c-1.2,3.6-0.9,8.7,1.1,12.1"/>
<path class="st5" d="M403.9,525.3c1.8-0.8,4-1.9,6-2.2"/>
</g>
</g>
</svg>
</div>
</div>
</body>
</html>
和 CSS 文件如下:
#content {
text-align: center
}
#content svg {
background: #fff;
width: auto;
height: 152vh;
margin: 8vh 0 8vh 0;
box-shadow: -9px -9px 20px 0 rgba(0, 0, 0, .12);
max-width: 90%
}
这会产生以下 svg 字符:
我想将这些文本组中的每一组都变成可点击的 link。但是,如果我尝试简单地用 <a></a>
标签围绕组标签,那么文本笔划确实是可点击的,但笔划之间的 space 不是。例如,如果光标悬停在 内部 字母 'O' 上,则在该位置没有任何内容可点击。如何让每组字符的“通用区域”可以点击?
谢谢!
最简单的方法之一是在 <g>
中使用 <rect />
。
你可以把opacity
设为0,然后把你所有的<path>
都写上去。你可以给 <rect />
高度和宽度等于你想要点击的区域/任何鼠标事件。
<g id="po">
<rect x="" y="" width="" height="" style="fill-opacity:0; stroke-opacity:0" />
<path class="st5" d="M382.1,465.4c1.1,2.6,1.3,7.3,1.4,9.2c0.1-3.3-4-13,2.7-13.1c5.3-0.1,4.4,8.5-2.7,6.7"/>
<path class="st5" d="M395.3,464.1c-4.9,0.9-2.3,8,2.3,5.7c4.7-2.3-0.3-7.5-3.8-3.8"/>
</g>
只需在您希望作为单个元素处理的其他元素上方或下方添加一个透明矩形 link。您可能会发现在顶部(即在 SVG 文件的后面)导致的问题较少。见下文。
<g id="po">
<path class="st5" d="M382.1,465.4c1.1,2.6,1.3,7.3,1.4,9.2c0.1-3.3-4-13,2.7-13.1c5.3-0.1,4.4,8.5-2.7,6.7"/>
<path class="st5" d="M395.3,464.1c-4.9,0.9-2.3,8,2.3,5.7c4.7-2.3-0.3-7.5-3.8-3.8"/>
<a xlink:href="http://www.whosebug.com" target="_blank">
<rect x="382" y="461" width="18" height="14" fill="transparent"/>
</a>
</g>
<a>
元素可以围绕 <rect>
、<g>
或围绕组的所有子元素。没关系。
#content {
text-align: center
}
#content svg {
background: #fff;
width: auto;
height: 152vh;
margin: 8vh 0 8vh 0;
box-shadow: -9px -9px 20px 0 rgba(0, 0, 0, .12);
max-width: 90%
}
<div id="container">
<div id="content" class="hide">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
<style type="text/css">
.st5{fill:none;stroke:red;stroke-miterlimit:10;}
</style>
<g id="page">
<g id="po">
<path class="st5" d="M382.1,465.4c1.1,2.6,1.3,7.3,1.4,9.2c0.1-3.3-4-13,2.7-13.1c5.3-0.1,4.4,8.5-2.7,6.7"/>
<path class="st5" d="M395.3,464.1c-4.9,0.9-2.3,8,2.3,5.7c4.7-2.3-0.3-7.5-3.8-3.8"/>
<a xlink:href="http://www.whosebug.com"">
<rect x="382" y="461" width="18" height="14" fill="transparent"/>
</a>
</g>
<g id="ph">
<path class="st5" d="M392.4,522c0,3.2,0.4,6.6,1.1,9.7c-3.7-3.9-2.3-16.4,4.9-11.3c4.1,2.8-3.3,7.3-6.6,5.5"/>
<path class="st5" d="M402.8,519.3c1,3.1,0.3,6.4,2.2,9.3"/>
<path class="st5" d="M410.5,517.6c-1.2,3.6-0.9,8.7,1.1,12.1"/>
<path class="st5" d="M403.9,525.3c1.8-0.8,4-1.9,6-2.2"/>
<a xlink:href="http://www.whosebug.com">
<rect x="391" y="517" width="21" height="15" fill="transparent"/>
</a>
</g>
</g>
</svg>
</div>
</div>
我有几个 svg <path>
元素组合成两个单独的组 <g>
元素(ID 是 po
和 ph
)。两者都在另一个组中,所有内容都在一个 <svg>
.
HTML 看起来像这样:
<!DOCTYPE html>
<html lang="en">
<head>
<link href="stripped_back_CSS.css" rel="stylesheet">
</head>
<body>
<div id="container">
<div id="content" class="hide">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
<style type="text/css">
.st5{fill:none;stroke:red;stroke-miterlimit:10;}
</style>
<g id="page">
<g id="po">
<path class="st5" d="M382.1,465.4c1.1,2.6,1.3,7.3,1.4,9.2c0.1-3.3-4-13,2.7-13.1c5.3-0.1,4.4,8.5-2.7,6.7"/>
<path class="st5" d="M395.3,464.1c-4.9,0.9-2.3,8,2.3,5.7c4.7-2.3-0.3-7.5-3.8-3.8"/>
</g>
<g id="ph">
<path class="st5" d="M392.4,522c0,3.2,0.4,6.6,1.1,9.7c-3.7-3.9-2.3-16.4,4.9-11.3c4.1,2.8-3.3,7.3-6.6,5.5"/>
<path class="st5" d="M402.8,519.3c1,3.1,0.3,6.4,2.2,9.3"/>
<path class="st5" d="M410.5,517.6c-1.2,3.6-0.9,8.7,1.1,12.1"/>
<path class="st5" d="M403.9,525.3c1.8-0.8,4-1.9,6-2.2"/>
</g>
</g>
</svg>
</div>
</div>
</body>
</html>
和 CSS 文件如下:
#content {
text-align: center
}
#content svg {
background: #fff;
width: auto;
height: 152vh;
margin: 8vh 0 8vh 0;
box-shadow: -9px -9px 20px 0 rgba(0, 0, 0, .12);
max-width: 90%
}
这会产生以下 svg 字符:
我想将这些文本组中的每一组都变成可点击的 link。但是,如果我尝试简单地用 <a></a>
标签围绕组标签,那么文本笔划确实是可点击的,但笔划之间的 space 不是。例如,如果光标悬停在 内部 字母 'O' 上,则在该位置没有任何内容可点击。如何让每组字符的“通用区域”可以点击?
谢谢!
最简单的方法之一是在 <g>
中使用 <rect />
。
你可以把opacity
设为0,然后把你所有的<path>
都写上去。你可以给 <rect />
高度和宽度等于你想要点击的区域/任何鼠标事件。
<g id="po">
<rect x="" y="" width="" height="" style="fill-opacity:0; stroke-opacity:0" />
<path class="st5" d="M382.1,465.4c1.1,2.6,1.3,7.3,1.4,9.2c0.1-3.3-4-13,2.7-13.1c5.3-0.1,4.4,8.5-2.7,6.7"/>
<path class="st5" d="M395.3,464.1c-4.9,0.9-2.3,8,2.3,5.7c4.7-2.3-0.3-7.5-3.8-3.8"/>
</g>
只需在您希望作为单个元素处理的其他元素上方或下方添加一个透明矩形 link。您可能会发现在顶部(即在 SVG 文件的后面)导致的问题较少。见下文。
<g id="po">
<path class="st5" d="M382.1,465.4c1.1,2.6,1.3,7.3,1.4,9.2c0.1-3.3-4-13,2.7-13.1c5.3-0.1,4.4,8.5-2.7,6.7"/>
<path class="st5" d="M395.3,464.1c-4.9,0.9-2.3,8,2.3,5.7c4.7-2.3-0.3-7.5-3.8-3.8"/>
<a xlink:href="http://www.whosebug.com" target="_blank">
<rect x="382" y="461" width="18" height="14" fill="transparent"/>
</a>
</g>
<a>
元素可以围绕 <rect>
、<g>
或围绕组的所有子元素。没关系。
#content {
text-align: center
}
#content svg {
background: #fff;
width: auto;
height: 152vh;
margin: 8vh 0 8vh 0;
box-shadow: -9px -9px 20px 0 rgba(0, 0, 0, .12);
max-width: 90%
}
<div id="container">
<div id="content" class="hide">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
<style type="text/css">
.st5{fill:none;stroke:red;stroke-miterlimit:10;}
</style>
<g id="page">
<g id="po">
<path class="st5" d="M382.1,465.4c1.1,2.6,1.3,7.3,1.4,9.2c0.1-3.3-4-13,2.7-13.1c5.3-0.1,4.4,8.5-2.7,6.7"/>
<path class="st5" d="M395.3,464.1c-4.9,0.9-2.3,8,2.3,5.7c4.7-2.3-0.3-7.5-3.8-3.8"/>
<a xlink:href="http://www.whosebug.com"">
<rect x="382" y="461" width="18" height="14" fill="transparent"/>
</a>
</g>
<g id="ph">
<path class="st5" d="M392.4,522c0,3.2,0.4,6.6,1.1,9.7c-3.7-3.9-2.3-16.4,4.9-11.3c4.1,2.8-3.3,7.3-6.6,5.5"/>
<path class="st5" d="M402.8,519.3c1,3.1,0.3,6.4,2.2,9.3"/>
<path class="st5" d="M410.5,517.6c-1.2,3.6-0.9,8.7,1.1,12.1"/>
<path class="st5" d="M403.9,525.3c1.8-0.8,4-1.9,6-2.2"/>
<a xlink:href="http://www.whosebug.com">
<rect x="391" y="517" width="21" height="15" fill="transparent"/>
</a>
</g>
</g>
</svg>
</div>
</div>