繁体   English   中英

获取元素的offsetHeight

[英]Get an element's offsetHeight

我有一个vue.js组件,该组件具有带有图像和不同块的大型html模板。

<div>
   ...
</div>
...
<div class="image-block">
   <img src="..." alt="" width="..." height="...">
</div>
...

如何获得图像块的offsetHeight?

您是否更清楚:

mounted() { 

  let imageBlock = document.getElementsByClassName('information-block__img'); 
  // => A DOMElement set

  // If you want a jQuery set :

  let imageBlockJQ = $('.information-block__img') ;

  for (let element in imageBlock) { 

    let image = imageBlock[element]; 
    // So a DOMElement

    let imageHeight = image.offset();  // => FAIL

    // A DOMElement does'nt know the `offset` method. A jQuery set does
    // so:
    let imageHeight = $(image).offset();

    // Or

    let imageHeight = image.offsetTop ; // => RIGHT, a property

}

只需使用element.offset()调用即可。

我找到了一个对我有用的解决方案,但仅在调整页面大小时才有效:

mounted() {
      let infoBlock = document.getElementsByClassName('information-block');
      for (let element in infoBlock) {
          $(window).resize(function() {
              $('.information-block__container').height($('.information-block__img').height());
          });
      }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM