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.
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.
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.
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.
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).
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With