How to detect resize of any element in HTML5

后端 未结 4 783
时光取名叫无心
时光取名叫无心 2021-02-13 12:05

What should the best practices to listen on element resize event?

I want to re-position an element (jQuery dialog in my case), once it\'s size changed. But I am now more

4条回答
  •  忘了有多久
    2021-02-13 12:49

    Given yourelement, when the size changes (ex. a text translation took place) you can doyourstuff(), including
    ro.unobserve(yourelement);

      var inilen = yourelement.offsetWidth;
      var ro = new ResizeObserver( entries => {
        for (let entry of entries) {
          const cr = entry.contentRect;
          if (inilen !== cr.width) {
            doyourstuff();        
          }
        }
      });
    
      ro.observe();
    

提交回复
热议问题