javascript - 如何使用 d3.js 更改鼠标悬停时的图像

标签 javascript svg d3.js

我有一些 svg 圆圈和图像,是用 d3.js 生成的。我想更改鼠标悬停时的图像而不是工具提示。

mask.append("image")
    .attr('class', "sth")
    .attr('x',-(entry.childNodes[0].getAttribute("r"))-40)
    .attr('y',-(entry.childNodes[0].getAttribute("r"))-40)
    .attr('width', 80+entry.childNodes[0].getAttribute("r")*2)
    .attr('height', 80+entry.childNodes[0].getAttribute("r")*2)
    .attr("transform", entry.childNodes[0].getAttribute("transform"))
    .attr('clip-path', 'url(#'+('clip'+clipPathId)+')')
    .attr("xlink:href", imageUrl)
    .on("click", function(d) { 
          zoom(d);                           
          d3.event.stopPropagation(); 
    })
    .on("mouseover", function(d){ d.attr("xlink:href", "img/001.jpg"); tooltip.style("visibility", "visible"); tooltip.html("<img src='"+imageUrl+"'/>"); })
    .on("mousemove", function(){ tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px"); })
    .on("mouseout", function(){ tooltip.style("visibility", "hidden"); }); 
                    ;

最佳答案

d 是绑定(bind)到节点的数据元素。 this 是节点本身:

尝试:

.on("mouseover", function(d){ 
    d3.select(this).attr("xlink:href", "img/001.jpg");
})

关于javascript - 如何使用 d3.js 更改鼠标悬停时的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27688615/

相关文章:

html - 如何使用 CSS 填充 SVG

javascript - 需要有关更新 D3.js 词云的帮助

javascript - 无法读取未定义的属性(已定义时)

javascript - 通过单击另一个按钮撤消按钮动画,但前提是动画已播放

javascript - Windows下在package.json脚本中设置环境变量

javascript - 滚动条可见,但不会向下滚动页面

c# - this.Region 路径数据导致一个小窗口

android - 我收到消息 import SVG with ERROR@ <feGaussianBlur> is not supported

javascript - 如何为 d3 可折叠树中的节点分配唯一的 id

javascript - 如何将 d3 "translate"变形为一个函数?