编程语言
首页 > 编程语言> > javascript – d3生成的SVG没有响应

javascript – d3生成的SVG没有响应

作者:互联网

我正在尝试创建一个响应式SVG.我已经成功创建了一个示例SVG:

<html xmlns:xlink="http://www.w3.org/1999/xlink"><head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  </head>
  <body>
    <div id="container">
        <svg viewBox="0 0 600 200" preserveAspectRatio="xMidYMid" id="chart">
            <circle fill="red" cy="100" cx="100" r="100"></circle>
            <circle fill="blue" cy="100" cx="300" r="100"></circle>
            <circle fill="green" cy="100" cx="500" r="100"></circle>
        </svg>
    </div>
</body></html>

https://jsfiddle.net/andrewsu/kombdqL2/4/

https://gist.github.com/andrewsu/d3ed340495a2f21a25f8f69dedb2096a

如果您在jsfiddle版本中调整面板边界,则可以看到圆圈的大小适当缩放.

我想用D3创建完全相同的SVG.

<html xmlns:xlink="http://www.w3.org/1999/xlink"><head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
  </head>
  <body>
    <div id="container">
    </div>

    <script type="text/javascript">
        var width=600, height=200;
        var svg = d3.select("#container").append("svg")
        .attr("id","chart")
        .attr("preserveAspectRatio", "xMidYMid")
        .attr("viewbox", "0 0 "+ width + " " + height);
        svg.append("circle")
            .attr("r","100")
            .attr("cx","100")
            .attr("cy","100")
            .attr("fill","red");
        svg.append("circle")
            .attr("r","100")
            .attr("cx","300")
            .attr("cy","100")
            .attr("fill","blue");
        svg.append("circle")
            .attr("r","100")
            .attr("cx","500")
            .attr("cy","100")
            .attr("fill","green");
    </script>

</body></html>

https://jsfiddle.net/andrewsu/g1x3s2ny/5/

https://gist.github.com/andrewsu/bf0e7549934f93ac40a416dc17bb7b1e

尽管我可以说,渲染的HTML完全相同,但第二个示例无法正常工作(请参阅jsfiddle).

奇怪的是,如果我使用浏览器的检查器来更改viewBox属性中的任何四个数字,行为会立即按预期工作.

思考?

解决方法:

viewBox有一个大写的B:

.attr("viewBox", "0 0 "+ width + " " + height);

现在检查你的小提琴:https://jsfiddle.net/on17ga9u/

这是文档:https://www.w3.org/TR/SVG/coords.html#ViewBoxAttribute

标签:javascript,responsive-design,svg,d3-js
来源: https://codeday.me/bug/20190627/1310161.html