css - 如何更改 SVGCircleElement 对象的样式?

标签 css d3.js svg

在我的散点图中,我想更改鼠标悬停时圆圈的样式(不透明度/颜色)以及所有其他共享相同类名的圆圈。

但是 groupList[i].style("opacity", .6); 似乎不是正确的方法。

var mouseOver = function() { 
    var circle = d3.select(this);

    var highlitedGroup = this.className.baseVal;

    var groupList = document.getElementsByClassName(highlitedGroup);

    // The styling for the circle which mouse is over it.     
    circle.transition()
        .duration(800).style("opacity", 1)
        .attr("r", 16).ease("elastic");

    // For the all other circles which have the same className do this styling  
    for (var i=0; i<groupList.length; i++) {
               // List of SVGCircleElement objects  
                groupList[i].style("opacity", .6); //??
        }     
}

最佳答案

因为您已经在使用 D3.js,所以我建议您尽可能在整个代码中坚持使用它。在这种情况下,您的函数基本上可以归结为两个语句,其中第一个语句操纵主圆,而第二个语句将处理具有相同类的所有圆。

var mouseOver = function() {

  // The styling for the circle which mouse is over it.     
  d3.select(this).transition()
    .duration(800).style("opacity", 1)
    .attr("r", 16).ease("elastic");

  // For the all other circles which have the same className do this styling  
  d3.selectAll("." + this.className)
    .style("opacity", .6);

}

请注意,这仅在只有一个类(class)分配给主圈子时才有效。如果为该元素分配了多个类,this.className 将包含一个以空格分隔的类名称列表,以打破选择。

关于css - 如何更改 SVGCircleElement 对象的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33417101/

相关文章:

Javascript D3-fetch 写入 csv

javascript - 火狐 svg.getElementById ('id' )

javascript - 生成渐变 css/svg 背景

javascript - 使用包装器按比例调整 div 的大小

html - 如何获取文本溢出 :ellipsis to work in a table?

html - 摆脱这个边距和垂直高度和对齐方式

javascript - 我可以通过使用媒体查询设置内容 css 属性来测试 Retina 设备吗?

javascript - 为什么 SVG 阴影在底部被切割

javascript - Angular - D3.js,Jasmine 测试生成的 HTML

javascript - d3 等值线队列和填充问​​题