如何将svg形状与svg文本合并(How to merge svg shape with a svg text)
我在尝试将svg文本与svg形状合并时遇到问题。 这是我当前的代码,白色条只在我的指针在框中时变为灰色,但是当它在字母上时,框变回白色。 如何使它保持灰色,即使我的鼠标在文本上,直到我将指针移离文本和框。 我已经尝试将函数放在
<g>
,<a>
,<svg>
标签中,但它根本不起作用。<script> function ontop(x){ x.style.fill = "#c8cace" } function notOnTop(x){ x.style.fill = "white" } </script> <svg> <g> <a href="https://google.com" target="_blank" > <rect height="50px" width="350px" x="70%" y="14%" fill="white" rx="5px" onmouseenter="ontop(this)" onmouseleave="notOnTop(this)" /> <text x="72%" y="22%" font-size="20" fill="black" >Google</text> </a> </g> </svg>
Im having problems trying to merge a svg text with an svg shape. This is my current code, the white bar only turn grey when my pointer is in the box, but when it is on the letter, the box turned back white. How do i make it so that it will remain grey even though my mouse is on the text until I move my pointer away from the text and the box. I've tried putting the functions in the
<g>
,<a>
,<svg>
tags but it simply won't work.<script> function ontop(x){ x.style.fill = "#c8cace" } function notOnTop(x){ x.style.fill = "white" } </script> <svg> <g> <a href="https://google.com" target="_blank" > <rect height="50px" width="350px" x="70%" y="14%" fill="white" rx="5px" onmouseenter="ontop(this)" onmouseleave="notOnTop(this)" /> <text x="72%" y="22%" font-size="20" fill="black" >Google</text> </a> </g> </svg>
满意答案
给文本指定属性pointer-events:none,以便鼠标忽略它。
请注意,您不需要使用javascript来悬停CSS:hover会这样做。
rect { fill: white; } rect:hover { fill: #c8cace; }
<svg> <g> <a href="https://google.com" target="_blank" > <rect height="50px" width="350px" x="70%" y="14%" rx="5px" /> <text x="72%" y="22%" font-size="20" fill="black" pointer-events="none">Google</text> </a> </g> </svg>
Give the text the property pointer-events: none so that it is ignored by the mouse.
Note that you don't need javascript to do hovering CSS :hover will do that.
rect { fill: white; } rect:hover { fill: #c8cace; }
<svg> <g> <a href="https://google.com" target="_blank" > <rect height="50px" width="350px" x="70%" y="14%" rx="5px" /> <text x="72%" y="22%" font-size="20" fill="black" pointer-events="none">Google</text> </a> </g> </svg>
相关问答
更多如何将文本转换为SVG路径?(How to convert text to SVG paths?)
如何获得SVG文本的“crispEdges”?(How to get “crispEdges” for SVG text?)
SVG:形状上的文本禁用背景形状上的动画(SVG: text over a shape disables animation on background shape)
如何将svg形状拖到另一个svg中以便保存(how to drag an svg shape into another svg so that it can be saved)
SVG:点击底层svg(SVG: Click underlying svg)
用另一个SVG形状填充SVG形状(Fill SVG shape with another SVG shape)
使用SVG和JS创建曲线文本(Creating Curved Text with SVG and JS)
如何将svg形状与svg文本合并(How to merge svg shape with a svg text)
如何从SVG中的形状中剪切文本?(How can I cut text from a shape in SVG?)
d3js svg同一行上的文本和形状(d3js svg text and shapes on same line)
相关文章
更多HTML5 SVG【HTML5教程 - 第八篇】
HTML5 Canvas vs. SVG【HTML5教程 - 第九篇】
最新问答
更多Python / IPython奇怪的不可重现列表索引超出范围错误(Python/IPython strange non reproducible list index out of range error)
故事板从表格单元中延伸出来并显示其详细信息披露按钮(storyboard segue from a tablecell an its Detail Disclosure Button)
我们可以将Gherkin功能文件与testcomplete集成(Can we integrate Gherkin feature files with testcomplete)
TrustAllCertificatesCallback被忽略(TrustAllCertificatesCallback is ignored)
返回嵌套元素的索引(Return index of nested element)
在数组中重新编号元素的有效方法(Efficient way of re-numbering elements in an array)
Express app定义的`static`文件夹无法正常工作(Express app defined `static` folder not working properly)
Javascript错误:未捕获TypeError:无法读取null的属性'value'[duplicate](Javascript error: Uncaught TypeError: Cannot read property 'value' of null [duplicate])
使用node.js和socket.io每秒广播一次(Using node.js and socket.io to broadcast every second)
如何让XMLSerializer将命名空间添加到嵌套对象中的属性?(How do I get the XMLSerializer to add namespaces to attributes in nested objects?)
Copyright ©2023 656463.com All Rights Reserved.滇ICP备2022006988号-50
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们,本站将尽快处理。谢谢合作!