javascript - IE 无法使用 getBoundingClientRect() 正确计算

标签 javascript d3.js

我以为我的 Angular 应用程序的 D3 轴可以在 Internet Explorer 中正常工作,但在 Chrome 中开发了几周后,我今天意识到它现在在 IE 中无法正常工作。

我的 X 轴有 3 个零件。第一个是日期标签,接下来是小时标签,然后是代表每个小时的“条”。

在 Chrome 中看起来像这样: enter image description here

在 IE 11 中,宽度计算失败。这是交替条的代码:

hoursBarUpperG
  .selectAll('.tick')
  .sort(function(a: any, b: any) {
    return ascending(a, b);
  })
  .each(function(this: any, _d: any, i: any) {
    if (this.nextSibling) {
      select(this)
        .append('rect')
        .attr(
          'width',
          this.nextSibling.firstChild.getBoundingClientRect().x - this.firstChild.getBoundingClientRect().x
        )
        .attr('height', 6)
        .attr('class', i % 2 ? 'lightGrayBar' : 'darkGrayBar');
    }
  });

问题是矩形附加了属性“width”,显示为“Nan”。 在 Chrome 中,getBoundingClientRect() 返回一个 DOMRect。在 IE 中,我得到一个没有“x”字段的 ClientRect。

是否有比使用 getBoundingClientRect() 更好的替代方法,以便我可以正确设置这些条形的宽度?

最佳答案

更新:我发现将代码更改为使用“left”而不是“x”似乎可以解决 IE 中的问题。

this.nextSibling.firstChild.getBoundingClientRect().left - this.firstChild.getBoundingClientRect().left

我仍然很想听听是否有人有更好的想法来进行此计算!

关于javascript - IE 无法使用 getBoundingClientRect() 正确计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49267129/

相关文章:

javascript - 突出显示 <div> 中字符串的每个实例 - Javascript

javascript - 错误 - '' 未定义的类型错误 : Cannot read property 'length"?

Javascript 重复字母 X 次

svg - D3 和​​传单在 Chrome/Safari 和 Firefox 中返回不同的 SVG

d3.js - d3.js 中的重叠圆圈不显示工具提示

javascript - 文本区域不显示在嵌套的 svg 中

javascript - 确定 Mongoose 中空数组和未定义数组之间的区别

javascript - 如何以 Angular 延迟加载 Controller ?

javascript - 在 d3js 中绘制线条时遇到问题

javascript - querySelectorAll 是在多个图表中选择元素的好方法吗?