javascript - d3js 在使用 d3.zoom() 时设置背景颜色

标签 javascript css d3.js background-color

我正在尝试用一些颜色在背景上绘制一些图像,然而 this当我缩小时,方法无法按预期工作(边框上的某些屏幕部分变白)。 那么设置背景颜色的正确方法是什么?

代码如下:

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
  </style>
</head>

<body>
  <script>

    var svg = d3.select("body")
      .append("svg")
      .attr("width", "100%")
      .attr("height", "100%")
      .call(d3.zoom().on("zoom", function () {
              svg.attr("transform", d3.event.transform)
      }))
      .append("g")

    svg.append("rect")
        .attr("width", "100%")
        .attr("height", "100%")
        .attr("fill", "pink");

    svg.append("image")
        .attr("xlink:href", "./image/" + "item_1" + ".png")
        .attr("x", document.body.clientWidth / 2)
        .attr("y", document.body.clientHeight / 2)
        .attr("width", 32)
        .attr("height", 32)

    svg.append("image")
        .attr("xlink:href", "./image/" + "item_2" + ".png")
        .attr("x", 3 * document.body.clientWidth / 4)
        .attr("y", 3 * document.body.clientHeight / 4)
        .attr("width", 32)
        .attr("height", 32)

  </script>
</body>

更新: 看起来添加 background-color 解决了这个问题:

...
  </style>
</head>

<style>
   body{
    background-color: #E59400;
   } 
</style>

<body>
  <script>
...

最佳答案

<!DOCTYPE html>
   <head>
  <meta charset="utf-8">
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
  </style>
</head>

<body>
  <script>

var svg = d3.select("body")
  .append("svg")
  .attr("width", "100%")
  .attr("height", "100%").style("background-color", "pink")
  .call(d3.zoom().on("zoom", function () {
          svg.attr("transform", d3.event.transform)
  }))
  .append("g")

svg.append("rect")
    .attr("width", "100%")
    .attr("height", "100%")
    .attr("fill", "pink")
    ;

svg.append("image")
    .attr("xlink:href", "./image/" + "item_1" + ".png")
    .attr("x", document.body.clientWidth / 2)
    .attr("y", document.body.clientHeight / 2)
    .attr("width", 32)
    .attr("height", 32)

svg.append("image")
    .attr("xlink:href", "./image/" + "item_2" + ".png")
    .attr("x", 3 * document.body.clientWidth / 4)
    .attr("y", 3 * document.body.clientHeight / 4)
    .attr("width", 32)
    .attr("height", 32)

  </script>
</body>

关于javascript - d3js 在使用 d3.zoom() 时设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48260408/

相关文章:

Css样式布局问题

javascript - 删除点击事件应用的 CSS

javascript - 使所有对象的数组长度相同

javascript - 在dimple.js 中处理大型数据集以呈现图表

javascript - D3 - 图例无法可视化

c# - &lt;script&gt;...&lt;/script&gt; 在转发器控制代码中未显示在页面呈现的源代码中

javascript - 如何将日期选择器值传递给 Controller ​​?

javascript - Bootstrap 表在 Chrome 上无法正常工作,但在 Firefox 上可以正常工作

javascript - 如何禁用react js的react-bootstrap-date-picker节点模块上的过去和今天的日期?

html - django + html 渲染