Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch height of an element in VueJS

Is there any way to set a watcher on the height of an element in VueJS?

I am trying to fix the filters part on a search result page. but if the results change there is no way to find about it and unfix the filters.

like image 425
Saeed Avatar asked Aug 08 '18 09:08

Saeed


People also ask

How do I get element height in Vue?

To get an element's height with Vue. js, we can assign a ref to the element we want to get the height for. Then we can get the height with the clientHeight property of the element that's been assigned the ref. We assigned the ref with the ref prop set to a name.

How do you find the height of an element?

In JavaScript, you can use the clientHeight property, which returns an element's height, including its vertical padding. Basically, it returns the actual space used by the displayed content. For example, the following code returns 120, which is equal to the original height plus the vertical padding.

For what watch is used in Vuejs?

The Vue. js Watcher or watch property allows the developers to listen to the component data and run whenever they change a particular property. The watcher or watch property is a unique Vue. js feature that lets you keep an eye on one property of the component state and run a function when that property value changes.

How do you find the height of an element in HTML?

The HTMLElement. offsetHeight read-only property returns the height of an element, including vertical padding and borders, as an integer. Typically, offsetHeight is a measurement in pixels of the element's CSS height, including any borders, padding, and horizontal scrollbars (if rendered).


1 Answers

You could use the ResizeObserver and integrate it into your Vue-Component

function observeHeight() {
  const resizeObserver = new ResizeObserver(function() {
     console.log("Size changed");
  });

 resizeObserver.observe(document.getElementById('yourId'));
}

function changeHeight() {
  var elem = document.getElementById('yourId');
  
  console.log('...loading data...')
  setTimeout(function(){ 
    elem.style = "height: 200px";
  }, 3000);
  
}

observeHeight();
changeHeight();
#yourId {
  background-color: beige;
  height: 100px;
}
<div id="yourId">
  
</div>
like image 181
Paul Avatar answered Nov 03 '22 06:11

Paul