简体   繁体   English

CSS精灵+ D3.js

[英]CSS-Sprites + D3.js

I'm trying to append HTML elements within a <circle> and then use CSS-Sprite to fill them, but so far I can't see them! 我试图在<circle>内附加HTML元素,然后使用CSS-Sprite填充它们,但是到目前为止我看不到它们!

Here's what im doing: 我在做什么:

 //Create the node in order to use the Force Layout
 var node = svg.selectAll(".container")
    .data(data)
    .append("circle")
    .attr("class", "dot")
    .attr("r", radius)
    .attr("cx", function(d) { return x(d[xVar]); }) //xAxis position
    .attr("cy", function(d) { return y(d[yVar]); }) //yAxis position
    .append("xhtml:i")
    .attr('class',  function(d) {
      return 'sprite '+ d['css-code'];
    });

And my css: 而我的CSS:

.sprite{
    position: absolute; 
    background:url(sprite.png) no-repeat;
}

.ad{background-position:0 -704px;}
.ae{background-position:0 -736px;}
.etc

They are being generated and I can see them on my browser inspector with the correct css atributes but they just won't appear. 它们正在生成,我可以在浏览器检查器中看到它们并带有正确的css属性,但是它们不会出现。

How do I use CSS-Sprites with D3's Force Layout? 如何将CSS精灵与D3的“力布局”一起使用?

You're inserting the <i> elements directly within the SVG <circle> elements, which is not legal SVG markup. 您将直接在SVG <circle>元素内插入<i>元素,这不是合法的SVG标记。 There are at least a couple of alternatives that you could use. 至少可以使用两种选择。

  • Make the nodes <g> elements which would contain both the <circle> elements and a different element for the sprite. 使节点<g>元素同时包含<circle>元素和sprite的其他元素。

  • Insert the HTML sprites directly in the HTML container for the graph and absolutely position them to correspond with the node positions. 将HTML Sprite直接插入到图形的HTML容器中,并对其进行绝对定位以与节点位置相对应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM