Skip to content

Instantly share code, notes, and snippets.

@fidlerryan
Created February 14, 2021 21:54
Show Gist options
  • Save fidlerryan/b89439fa57c5cd47b617a961d383d764 to your computer and use it in GitHub Desktop.
Save fidlerryan/b89439fa57c5cd47b617a961d383d764 to your computer and use it in GitHub Desktop.
ResizeObserver
// check if ResizeObserver is supported
if( 'ResizeObserver' in window ){
var figcaption = document.querySelector( 'figcaption > h1.entry-title' );
var figure = document.querySelector( 'figure > div.post-thumbnail' );
// check if the elements exist
if( ( typeof( figcaption ) !== 'undefined' && figcaption !== null ) &&
( typeof( figure ) !== 'undefined' && figure !== null ) ){
var ro = new ResizeObserver( entries => {
for( let entry of entries ){
// get the height, including padding, of .entry-title
if( entry.target.tagName === 'H1' ){
var h1 = entry.target;
var h1cr = entry.contentRect;
var h1height = h1cr.height + h1cr.top;
}
// get the height, including padding, of .post-thumbnail
if( entry.target.tagName === 'DIV' ){
var div = entry.target;
var divcr = entry.contentRect;
var divheight = divcr.height + divcr.top;
}
// if .entry-title is taller than .post-thumbnail
if( h1height > divheight ){
// set the height of the image to the height of .entry-title
div.querySelector( 'img' ).style.height = h1height + 'px';
}
}
});
ro.observe( figcaption );
ro.observe( figure );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment