简体   繁体   English

将 SVG 定位在浏览器窗口的中间

[英]Positioning SVG at middle of browser window

I have the following SVG definition and I can't seem to position the SVG window at the very center of the screen even though I specify the viewBox co-ordinates.我有以下 SVG 定义,即使我指定了 viewBox 坐标,我似乎也无法将 SVG 窗口定位在屏幕的正中央。

var width = 1000, height = 500;

var svg = d3.select("body").append("svg")
            .attr("width", width)
            .attr("height", height);

svg.append("defs").selectAll("marker")
            .data(["suit", "licensing", "resolved"])
            .enter().append("marker")
            .attr("id", function (d) {
                return d;
            })
            .attr("viewBox", "100 -100 1000 500")
            .attr("preserveAspectRatio", "xMidYMid meet")
            .attr("display", "block")            
            .attr("refX", 25)
            .attr("refY", 0)
            .attr("markerWidth", 6)
            .attr("markerHeight", 6)
            .attr("orient", "auto")
            .append("path")
            .attr("d", "M0,-5L10,0L0,5 L10,0 L0, -5")
            .style("stroke", "#4679BD")
            .style("opacity", "0.6");

How do I position the SVG at the very center of my browser window?如何将 SVG 定位在浏览器窗口的正中央?

If you try to create an SVG element and have it centered on the page, then I guess it would be a simple problem of centering a DOM node.如果您尝试创建一个 SVG 元素并使其居中在页面上,那么我想这将是一个将 DOM 节点居中的简单问题。 But first you have to retrieve the SVG DOM node, which is different from your svg variable.但首先您必须检索 SVG DOM 节点,它与您的svg变量不同。

Using the technique described here: https://davidwalsh.name/css-vertical-center使用此处描述的技术: https : //davidwalsh.name/css-vertical-center

svgElement = document.getElementsByTagName("svg")[0];

svgElement.style.position = "relative";
svgElement.style.top = "50%";
svgElement.style.left = "50%";
svgElement.style.transform = "translate(-50%, -50%)";

Demo: http://jsfiddle.net/cgupL5mk/演示: http : //jsfiddle.net/cgupL5mk/

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

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