You should use the MutationObserver
API, which will trigger a listener function as soon as the DOM of the specified element changes, so you're able to make sure your elements have the right CSS without triggering resize
event.
Here's some example:
$(document).ready(function () {
var observer = new MutationObserver(function ( mutations ) {
var elements = mutations.reduce(function ( elements, mutation ) {
return elements.concat( mutation.addedNodes );
}, [] );
// .filter() makes sure you only manipulate mutated elements that match your selector
resizeFonts( $( elements ).filter( ".selector" ) );
});
observer.observe( $(".parent-element")[ 0 ], {
childList: true,
subtree: true
});
$( window ).resize(function () {
resizeFonts( $( ".selector" ) );
});
});
function resizeFonts ( elements ) {
elements.css( "font-size", amazingNewFontSize );
}
Please note, however, that MutationObserver
s may not work in all browsers.
I based my example on this answer, which includes further code to make it work in older browsers.