如何在SVG圆形的首行添加radio类型的选择框?
如何在SVG圆形的首行添加radio类型的选择框?
嘿,要给这个SVG圆形的首行位置加个radio选择框对吧?其实有两种实用的实现思路,我给你拆解一下:
方法1:用HTML容器包裹,独立定位radio按钮
这种方法把SVG和HTML的radio按钮放在同一个相对定位的容器里,通过CSS绝对定位把radio放到你想要的首行位置,好处是表单元素的交互逻辑和普通HTML一样,非常好处理。
示例代码如下:
<div style="position: relative; display: inline-block;"> <!-- 这里放radio按钮,定位到SVG上方的首行位置 --> <input type="radio" name="plan" id="noko4-plan" style="position: absolute; top: 10px; left: 10px; z-index: 1;"> <label for="noko4-plan" style="position: absolute; top: 8px; left: 30px; z-index: 1; color: #000;">选择此方案</label> <!-- 你的原始SVG代码 --> <svg height="334.4" width="334.4"> <circle cx="167.2" cy="167.2" r="134" stroke="#00ffff" stroke-width=4 fill="#e6ffff" /> <text text-anchor="middle" stroke="#000000" stroke-width="1px" dy=".3em"> <tspan x="50%" y="25%" dy="1.2em">1200% DAILY FOR 40 DAYS</tspan> </text> <text text-anchor="middle" stroke="#000000" stroke-width="0.5px" dy=".3em"> <tspan x="50%" y="35%" dy="1.2em">Noko 4</tspan> <tspan x="50%" y="45%" dy="1.2em">Spent Amount ($):</tspan> <tspan x="50%" y="55%" dy="1.2em">Min 60.000,00 - Max 260.000,00</tspan> <tspan x="50%" y="65%" dy="1.2em">Daily Profit: 1200%</tspan> <tspan x="50%" y="75%" dy="1.2em">No deposit</tspan> </text> </svg> </div>
你可以调整top和left的值来精准控制radio在首行的位置,z-index确保它在SVG上方显示。
方法2:在SVG内部用<foreignObject>嵌入radio按钮
如果你希望radio是SVG的一部分(比如需要把整个SVG导出为单个文件),可以用SVG的<foreignObject>元素嵌入HTML内容,这样radio就属于SVG的内部元素了。
示例代码如下:
<svg height="334.4" width="334.4"> <!-- 用foreignObject嵌入HTML的radio按钮 --> <foreignObject x="10" y="10" width="150" height="30"> <body xmlns="http://www.w3.org/1999/xhtml"> <input type="radio" name="plan" id="noko4-plan-svg"> <label for="noko4-plan-svg">选择此方案</label> </body> </foreignObject> <!-- 你的原始SVG内容 --> <circle cx="167.2" cy="167.2" r="134" stroke="#00ffff" stroke-width=4 fill="#e6ffff" /> <text text-anchor="middle" stroke="#000000" stroke-width="1px" dy=".3em"> <tspan x="50%" y="25%" dy="1.2em">1200% DAILY FOR 40 DAYS</tspan> </text> <text text-anchor="middle" stroke="#000000" stroke-width="0.5px" dy=".3em"> <tspan x="50%" y="35%" dy="1.2em">Noko 4</tspan> <tspan x="50%" y="45%" dy="1.2em">Spent Amount ($):</tspan> <tspan x="50%" y="55%" dy="1.2em">Min 60.000,00 - Max 260.000,00</tspan> <tspan x="50%" y="65%" dy="1.2em">Daily Profit: 1200%</tspan> <tspan x="50%" y="75%" dy="1.2em">No deposit</tspan> </text> </svg>
这里的x和y控制radio在SVG内的位置,width和height是foreignObject的容器大小,你可以根据需求调整。
两种方法都能实现需求,第一种更适合普通网页场景,交互逻辑更灵活;第二种适合需要把radio和SVG绑定成一个整体的场景~
内容的提问来源于stack exchange,提问作者Andrea P.
相关产品推荐
相关产品推荐

