admin管理员组文章数量:1427287
I'm using Angular Material
for my grid system. I'm trying to generate an SVG and it is supposed to show me the width of the element, however it is getBoundingClientRect()
is returning 300px
despite the width being 618px
. I tried it again making my window smaller but it still showed up as 300px
even though this time it was actually 100px
..
This is my HTML:
<div layout="row" layout-wrap layout-padding>
<div flex="33" ng-repeat="result in ctrl.results">
<svg height="100%" width="100%" position>
<rect x="0" y="0" width="100%" height="100%" fill="#da552f"></rect>
<text fill="#ffffff" x="50%" y="50%" alignment-baseline="middle" text-anchor="middle" font-size="48" font-family="Verdana">Hello World</text>
</svg>
</div>
</div>
and this is my angularjs directive for the position
attribute:
var app = angular.module('MainDirective', []);
app.directive('position', function () {
return {
restrict: 'A',
link: function ($scope, element, attrs) {
var rect = element[0].getBoundingClientRect();
var text = element.children()[1].getBBox();
console.log(rect)
}
};
});
There is no custom CSS in my project.
Any thoughts why this might be happening? I've tried so many different variations of getBoundingClientRect()
but they all returned 300
...
Edit: just as proof:
I'm using Angular Material
for my grid system. I'm trying to generate an SVG and it is supposed to show me the width of the element, however it is getBoundingClientRect()
is returning 300px
despite the width being 618px
. I tried it again making my window smaller but it still showed up as 300px
even though this time it was actually 100px
..
This is my HTML:
<div layout="row" layout-wrap layout-padding>
<div flex="33" ng-repeat="result in ctrl.results">
<svg height="100%" width="100%" position>
<rect x="0" y="0" width="100%" height="100%" fill="#da552f"></rect>
<text fill="#ffffff" x="50%" y="50%" alignment-baseline="middle" text-anchor="middle" font-size="48" font-family="Verdana">Hello World</text>
</svg>
</div>
</div>
and this is my angularjs directive for the position
attribute:
var app = angular.module('MainDirective', []);
app.directive('position', function () {
return {
restrict: 'A',
link: function ($scope, element, attrs) {
var rect = element[0].getBoundingClientRect();
var text = element.children()[1].getBBox();
console.log(rect)
}
};
});
There is no custom CSS in my project.
Any thoughts why this might be happening? I've tried so many different variations of getBoundingClientRect()
but they all returned 300
...
Edit: just as proof:
Share Improve this question edited Dec 15, 2016 at 3:30 Katie asked Dec 15, 2016 at 3:12 KatieKatie 7511 gold badge11 silver badges30 bronze badges 8-
1
Where do you check
.width
of.getBoundingClientRect()
object? – guest271314 Commented Dec 15, 2016 at 3:25 -
@guest271314 within the
console.log(rect)
... you could technically doconsole.log(rect.width)
I suppose. – Katie Commented Dec 15, 2016 at 3:30 -
Is
element[0]
div
element? What doesconsole.log(window.innerWidth)
log atconsole
? "is returning300px
despite the width being618px
" Which elementwidth
is set to618px
? "There is no custom CSS in my project" How is elementwidth
set to618px
? – guest271314 Commented Dec 15, 2016 at 3:35 -
element[0] is the SVG and
console.log(window.innerWidth)` returns1920
. When I check the Chrome dev tools and hover over the SVG it shows as618px
.. as does the innerrect
– Katie Commented Dec 15, 2016 at 3:41 -
The
width
ofsvg
element is set to100%
at the element<svg height="100%" width="100%" position>
, not618px
.console.log(document.querySelector("svg").width.animVal.valueAsString, window.getComputedStyle(document.body).width)
. – guest271314 Commented Dec 15, 2016 at 3:46
1 Answer
Reset to default 3I figured out what my issue was.. I should've used angulars $scope.$watch
as I was generating the svg
on an event click.
var app = angular.module('MainDirective', []);
app.directive('position', function () {
return {
restrict: 'A',
link: function ($scope, element, attrs) {
var rect, image, text;
$scope.$watch(element, function () {
rect = element[0].clientWidth;
image = element.children()[1].getBBox();
text = element.children()[2].getBBox();
console.log("Rect: "+rect+" image: "+image.width+" text: "+text.width)
});
}
};
});
本文标签: javascriptgetBoundingClientRect() returning wrong widthStack Overflow
版权声明:本文标题:javascript - getBoundingClientRect() returning wrong width - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745494173a2660735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论