javascript - 更改 svg 图像的颜色

标签 javascript css d3.js svg

我有一个 SVG 图像,我在其中放置了更多图像(它们也是 svg 图形)。我需要在鼠标悬停时更改它们的颜色。

现在我正在尝试使用 d3js 进行以下操作,但它不起作用。

 graph
        .selectAll('g')
        .data(Operators)
        .enter()
        .append('g')
        .selectAll('image')
        .data(function (d) {
            //console.log('image d', d);
            return d;
        })
        .enter()
        .append('image')
        .attr('id', function (d) {
            return d.name;
        })
        .attr('x', function (d) {
            return d.x;
        })
        .attr('y', function (d) {
            return d.y ;
        })
        .attr('width', function (d) {
            return d.width;
        })
        .attr('height', function (d) {
            return d.height;
        })
        .attr("xlink:href", function (d) {
            return base_url + "/assets/svg/" + d.src;
        })
        .style('fill', function (d) {
            //return d.hoverColor;
        })
        .on('mouseover', function (d) {
            var img = d3.select(this);
            console.log('hoverColor: ', d.hoverColor);
            img.style('fill', d.hoverColor);
        })

这是一个示例 SVG 图像

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.2.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
<svg version="1.1" baseProfile="tiny" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
     x="0px" y="0px" width="32px" height="32px" viewBox="0 0 32 32" xml:space="preserve">
<g>
    <g>
        <path fill="#FFFFFF" d="M4.628,26.186L2.305,24l1.037-0.977C0.451,21.527,0,19.656,0,19.656v4.84c0,0,1.18,3.527,8.637,4.558
            C6.25,27.691,4.749,26.299,4.628,26.186z"/>
    </g>
    <g>
        <path fill="#FFFFFF" d="M17.957,16.254c1.014,0,1.993,0.109,2.938,0.286C25.23,15.116,26,12.761,26,12.761V7.923
            c0,0-1.286,5.202-13.021,5.202C1.246,13.125,0,7.923,0,7.923v4.838c0,0,1.47,4.397,11.502,4.806
            C13.405,16.809,15.597,16.254,17.957,16.254z"/>
    </g>
    <g>
        <path fill="#FFFFFF" d="M26,18.277V13.73c0,0-0.447,1.796-3.195,3.278C23.961,17.357,25.031,17.799,26,18.277z"/>
        <path fill="#FFFFFF" d="M4.628,21.814c0.128-0.119,1.812-1.67,4.446-3.104C0.956,17.719,0,13.729,0,13.729v4.839
            c0,0,0.689,2.067,4.357,3.5L4.628,21.814z"/>
    </g>
    <circle fill="#FFFFFF" cx="17.957" cy="24" r="4.747"/>
    <path fill="#FFFFFF" d="M17.957,30.248c-6.744,0-12.078-4.944-12.301-5.154L4.494,24l1.162-1.092
        c0.224-0.211,5.558-5.154,12.301-5.154c6.74,0,12.075,4.943,12.299,5.154L31.418,24l-1.162,1.094
        C30.032,25.304,24.697,30.248,17.957,30.248z M9.083,23.998c1.788,1.285,5.132,3.25,8.875,3.25c3.735,0,7.079-1.961,8.869-3.248
        c-1.79-1.285-5.132-3.246-8.869-3.246C14.197,20.754,10.865,22.713,9.083,23.998z"/>
    <g>
        <g>
            <path fill="#FFFFFF" d="M12.89,2.497c-4.141,0-7.638,1.487-7.638,3.248c0,1.761,3.498,3.249,7.638,3.249
                c4.139,0,7.639-1.488,7.639-3.249C20.527,3.985,17.029,2.497,12.89,2.497z M12.89,7.579c-2.211,0-3.943-0.805-3.943-1.833
                c0-1.027,1.732-1.833,3.943-1.833s3.943,0.805,3.943,1.833C16.833,6.774,15.101,7.579,12.89,7.579z"/>
            <path fill="#FFFFFF" d="M12.89,4.185c-1.99,0-3.671,0.714-3.671,1.561c0,0.847,1.681,1.562,3.671,1.562
                c1.99,0,3.67-0.715,3.67-1.562C16.561,4.899,14.88,4.185,12.89,4.185z"/>
            <path fill="#FFFFFF" d="M12.89,0C5.771,0,0,2.572,0,5.745c0,3.173,5.771,5.747,12.89,5.747c7.119,0,12.889-2.573,12.889-5.747
                C25.779,2.571,20.009,0,12.89,0z M12.89,10.537c-5.779,0-10.307-2.105-10.307-4.792S7.11,0.954,12.89,0.954
                c5.78,0,10.307,2.104,10.307,4.791S18.669,10.537,12.89,10.537z"/>
        </g>
    </g>
</g>
</svg>

谁能帮帮我?

最佳答案

使用简单的 CSS:

向您的路径元素添加一个类名,如 class="svgimg"

然后在 css

svg:hover .svgimg { fill: darkslateblue; }

找到这个演示: DEMO:

关于javascript - 更改 svg 图像的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33806211/

相关文章:

javascript - 如何将 Cordova 应用程序中的本地存储项目持久保存到手机上?

javascript - 为什么IE8的一些内置函数不是Function的实例?

Javascript 文本更改不起作用

javascript - Html 5 文件系统 API,我收到 DOMError "NotSupporteError"

python - 从 css 选择器中排除 div

javascript - D3.js 中的点图

mysql - 格式化数据以用于 JSON 和 D3.js

javascript - 如何画 donut ?

css - Shiny R 不读取自定义 CSS 文件

android - 使用 Cordova 的 Android 自定义字体的问题