我在使用d3創建的可視化中添加橫幅時遇到了問題。我想添加橫幅,然後在橫幅上添加文本。這適用於Chrome(文本呈現在圖片的「頂部」),但在Firefox上,它看起來像是在文本頂部呈現圖片。有人知道爲什麼這可以在Chrome瀏覽器中運行,但不是Firefox,並且有什麼方法可以在Firefox中獲得相同的結果?foreignObject內的背景圖像隱藏了Firefox上的其他svg元素
var margin = {
top: 155,
right: 3,
bottom: 3,
left: 3
},
width = $(window).width() - margin.left - margin.right,
height = $(window).height() - margin.top - margin.bottom,
formatNumber = d3.format(",d"),
transitioning;
var x = d3.scale.linear()
.domain([0, width])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, height])
.range([0, height]);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.style("shape-rendering", "crispEdges");
var grandparent = svg.append("g").attr("class", "grandparent");
grandparent.append("rect")
.attr("y", -margin.top)
.attr("width", width)
.attr("height", margin.top);
grandparent.append("foreignObject")
.attr("y", -margin.top)
.attr("width", width)
.attr("height", margin.top)
.append("xhtml:div")
.attr("class", "bannerDiv");
grandparent.append("text")
.attr("x", 6)
.attr("y", (-1/8) * margin.top)
.attr("dy", ".75em")
.style("fill", "#A5D867")
.text("The text here");
function text(text) {
text.attr("x", function(d) {
return x(d.x) + 6;
})
.attr("y", function(d) {
return y(d.y) + 6;
});
}
請參見本的jsfiddle爲例: https://jsfiddle.net/rmw6snj6/
使用帶圖案填充的矩形顯示圖像。 –
@RobertLongson出於好奇,Chrome和Firefox之間的這種行爲差異的任何(已知)原因? –
是的,聽起來像一個bug,是否知道?這意味着'foreignObject'內容總是處於頂部?啊也許https://bugzilla.mozilla.org/show_bug.cgi?id=984312 – Kaiido