D3-使用一个SVG点击事件来影响另一个

D3 - Using one SVG click event to affect another

本文关键字:事件 另一个 影响 SVG 一个 D3-      更新时间:2024-03-20

这是我几天前问的一个问题的后续。

在我的图形中,我希望原始图标在单击时具有动画效果,并且我还希望解释性文本淡出视图。我已经使用InkScape制作了文本,并将其xlink到代码中(就像其他图标一样)。

var coal = svg.append("svg:image")
.attr("xlink:href", "nouns/coal.svg")
.attr("width", 35)
.attr("height", 35)
.attr("x", 10)
.attr("y", 30)
.on("click", function() {
    d3.select(this).transition()
        .attr("x", function() {return d3.select(this).attr("x") == 10 ? 80 : 10; })
        .attr("y", function() {return d3.select(this).attr("y") == 30 ? 150 : 30; })
        .attr("width", function() {return d3.select(this).attr("width") == 35 ? 100 : 35; })
        .attr("height", function() {return d3.select(this).attr("height") == 35 ? 100 : 35; })
        .duration(750);
    d3.select(".coaltext").transition()
        .attr("opacity", function() {return d3.select(".coaltext").attr("opacity") == 0 ? 100 : 0 })
});
    svg.append("g")
        .append("svg:image")
        .attr("xlink:href", "testtext.svg")
        .attr("id", "coaltext")
        .attr("height", 200)
        .attr("width", 200)
        .attr("y", 170)
        .attr("x", 400)
        .attr("opacity", 0);

唯一奇怪的是我收到了这个错误消息:event.returnValue已弃用。请改用标准事件.prventDefault()--jquery.min.js:2

这很奇怪,因为我没有在脚本中加载任何jquery库。小时。

在选择元素时,似乎要使用#coaltext而不是.coaltext——这是一个ID,而不是一个类。