Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Preloading background images that are in an inline style Re: Preloading background images that are in an inline style

#120508
Taufik Nurrohman
Participant

Maybe you need to set a fake attribute to store the background like this:

<div class="preload" id="main"<?php if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' data-background="background-image: url(%s);"', $image_src[0] );}?>>

Then, with JQuery you set a new `style` attribute with the value taken from `data-background` attribute value:

$('.preload').each(function() {
$(this).attr('style', $(this).data('background'));
});