![](/img/trans.png)
[英]How to get the width and height of an img element where width and height have not been set?
[英]angularjs how to find img element height and width
嗨,我正在嘗試找到圖像元素的高度和寬度,以便調整它的大小。 但是每次我使用elem.height()或elem.width()我都會得到0?
HTML
<div>
<img data-ng-src="{{photosrc}}" lightbox-resize-image>
</div>
directive.js
.directive('lightboxResizeImage', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
console.log(elem.parent().width()); //==equals 575
console.log(elem.width()); //this always equals 0
}
});
見下面的代碼:
.directive('lightboxResizeImage', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
console.log(elem.parent().width()); // =575
console.log(elem[0].offsetHeight);
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.