最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect When User Resizes Div With CSS resize: both - Stack Overflow

programmeradmin9浏览0评论

I know I can use ResizeObserver to detect when a div's size changes, but I would like to only know when the user resizes the div with the CSS property resize: both, and not when the page gets resized.

// Detect when user resizes element with the property resize: both;
div {
  resize: both;
  overflow: auto;
  border: 1px solid;
}
<div>
  Hi!
</div>

I know I can use ResizeObserver to detect when a div's size changes, but I would like to only know when the user resizes the div with the CSS property resize: both, and not when the page gets resized.

// Detect when user resizes element with the property resize: both;
div {
  resize: both;
  overflow: auto;
  border: 1px solid;
}
<div>
  Hi!
</div>

Share Improve this question edited Mar 17, 2021 at 20:51 KetZoomer asked Mar 5, 2021 at 6:15 KetZoomerKetZoomer 2,9454 gold badges20 silver badges50 bronze badges 1
  • 2 Will this question help you? - stackoverflow./questions/8082729/… – s.kuznetsov Commented Mar 5, 2021 at 6:36
Add a ment  | 

2 Answers 2

Reset to default 6

You can use ResizeObserver:

const observer = new ResizeObserver(mutations => {
  console.clear()
  console.log(mutations[0].contentRect.width, mutations[0].contentRect.height)
});

observer.observe(test);
#test {
  resize: both;
  overflow: auto;
  border: 1px solid;
  width: 100px;
  height: 100px;
}
<div id='test'>Hi!</div>

You can use MutationObserver for this. Source here.

let observer = new MutationObserver(function(mutations) {
  console.log('Resized');
});

let child = document.querySelector('div');
observer.observe(child, { attributes: true });
div {
  resize: both;
  overflow: auto;
  border: 1px solid;
    width: 200px;
    height: 200px;
    
}
<div>
  Hi!
</div>

发布评论

评论列表(0)

  1. 暂无评论