【问题标题】:I get this error " Cannot read property 'getBBox' of null " and I couldn't understand why?我收到此错误“无法读取 null 的属性 'getBBox'”,我不明白为什么?
【发布时间】:2015-10-28 14:16:35
【问题描述】:

我正在尝试在 onmouseover 时缩放 svg 部分并在 onmouseout 上重新缩放到以前的大小

无法读取 null 的属性“getBBox”

D=document;
var lib = 'http://www.w3.org/2000/svg';
D.
function magnify(scale,x){  x.setAttribute("transform","matrix(1.5 0 0 1 0 0)");
}
function minimize(x){   x.setAttribute("transform","matrix(1 0 0 1 0 0)");
}

function getCenter(selection)
{   bbox=D.getElementById("#"+selection).getBBox();
    return[bbox.x + bbox.width/2,bbox.y + bbox.height/2];
}
function reCoor(scale, x)
{
        var coor = getCenter(x);
        var newX=x[0].width*(1-scale);
        var newY=x[1].height*(1-scale);

        var output = "matrix(" + scale + " 0 0 " + scale + " " + newX + " " + newY +")";

        return[output];
}

MyFiddle

【问题讨论】:

  • document.getElementById(element)中,元素在javascript中不需要#
  • 所以这很有帮助
  • 你在讽刺吗?
  • 我问一个问题,你投票给我减去你是吗?在某些平台(firefox,ie)中可能会使用“#”。
  • 我没有投票。不消极也不积极。不要做假设。

标签: javascript html css svg


【解决方案1】:

可以通过调用getBoundingClientRect获取任意元素的边界框

var rect = document.getElementById("myElement").getBoundingClientRect();

这将返回一个包含左、上、宽和高字段的对象。

【讨论】:

  • 感谢您的帮助,但同样的问题仍然存在。正如你告诉我的那样,我删除了“#”并使用你展示的方法。我想问题并不像看起来那样。
  • 在小提琴中没有函数调用reCoor,所以没有人调用getCenter。如果我调用 reCoor,并评论非工作功能,我的解决方案有效(请参阅控制台输出):jsfiddle.net/ucfxkuf6/2
猜你喜欢
  • 2020-06-07
  • 1970-01-01
  • 2022-06-24
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
相关资源
最近更新 更多