如何加深作为鼠标指针使用的SVG十字准线颜色?
解决SVG十字准线鼠标指针显示模糊/半透明的问题
问题说明
在ReactJS项目中使用SASS设置自定义鼠标指针,代码为:
cursor: url(crosshairs.svg), auto;
SVG是横竖线组成的十字准线,但线条呈半透明状态几乎不可见,修改SVG的颜色设置后仍未生效,原SVG代码如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- Created with Inkscape (http://www.inkscape.org/) EDIT: was height: 3301, width:2161 reduced to x128 else it wont show up--> <svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="128" height="128" viewBox="0 0 873.389 571.765" version="1.1" id="svg8" inkscape:version="0.92.3 (unknown)" sodipodi:docname="fullscreen-crosshair-1650x1080.svg"> <defs id="defs2" /> <sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="0.25" inkscape:cx="1437.71" inkscape:cy="1122.57" inkscape:document-units="mm" inkscape:current-layer="layer1" showgrid="false" units="px" inkscape:window-width="1680" inkscape:window-height="1016" inkscape:window-x="0" inkscape:window-y="0" inkscape:window-maximized="1" /> <metadata id="metadata5"> <rdf:RDF> <cc:Work rdf:about=""> <dc:format>image/svg+xml</dc:format> <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> <dc:title></dc:title> </cc:Work> </rdf:RDF> </metadata> <g inkscape:label="Layer 1" inkscape:groupmode="layer" id="layer1" transform="translate(0,274.765)"> <path style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.793751;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 436.694,-322.259 V 344.493" id="path10" inkscape:connector-curvature="0" /> <path style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.793751;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;stroke-miterlimit:4;stroke-dasharray:none" d="M -58.6065,11.1175 H 931.996" id="path12" inkscape:connector-curvature="0" /> </g> </svg>
问题根源
不是透明度问题(SVG里已设置stroke-opacity:1),而是两个关键问题:
- 线条宽度
stroke-width:0.793751过细,在128x128的尺寸下缩放后几乎不可见 viewBox(873.389x571.765)与实际宽高(128x128)比例不匹配,导致十字线被过度压缩
修复步骤
1. 重写SVG文件
简化SVG代码,调整viewBox和线条宽度,确保十字线清晰且中心对齐:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128" version="1.1"> <g id="crosshair"> <!-- 垂直中线,宽度设为2,纯黑色无透明 --> <path style="fill:none;stroke:#000000;stroke-width:2;stroke-linecap:butt;stroke-opacity:1" d="M 64,0 V 128" /> <!-- 水平中线 --> <path style="fill:none;stroke:#000000;stroke-width:2;stroke-linecap:butt;stroke-opacity:1" d="M 0,64 H 128" /> </g> </svg>
2. 优化CSS设置
添加鼠标热点坐标,确保十字中心对应点击位置(可选,但体验更好):
cursor: url(crosshairs.svg) 64 64, auto;
64 64是128x128 SVG的中心坐标,对应鼠标指针的点击热点。
3. 清除浏览器缓存
修改SVG后,浏览器可能保留旧缓存,按Ctrl+F5强制刷新页面,或手动清除缓存。
内容的提问来源于stack exchange,提问作者mdkb
相关产品推荐
相关产品推荐

