I want to center and crop images even for images that are dynamically loaded later.
HTML
<body>
<div class="iThumbnail">
<img src="images/art-vertical.jpeg" alt=""/>
</div>
</body>
The body
content above is static the first time.
Later, body
contents are loaded/replaced dynamically with other image sources.
So, for the static content, I'm able to center and crop the images with the below code (thanks to jonathanNicol and nickCraver).
$(document).ready(function() {
function thumbnailImageCropCenter() {
$('.iThumbnail').find('img').one('load', function() {
if (($(this).height() > $(this).width()) && ($(this).height() != 0) ) {
$(this).addClass('portraitView');
}
}).each(function() {
if(this.complete) $(this).load();
});
}
thumbnailImageCropCenter();
});
I want to avoid calling thumbnailImageCropCenter()
every time I load a page content dynamically.
Question: Is there a way to automatically bind thumbnailImageCropCenter()
to body
such that it applies to all the images within body
, no matter if images were loaded dynamically later.
PS: replacing .one()
with .bind()
didn't help.