Jump to content

lazy load backgroung images in span tag


ohno

Recommended Posts

Hi,

I've implemented lazy loading on my website using this guide : -

https://web.dev/codelab-use-lazysizes-to-lazyload-images/

However I can't get the last bit of code to work! Basically I have images that are in a span tag loaded as a background image, I have this is the CSS : -

.blocks1 .block a span.thumb {
width: 120px;
height: 90px;
background-repeat: no-repeat;
background-position: center;
background-size: contain;
margin: 0 auto
}

The page uses this tpl file for the images : -

{section name=c loop=$cats->mCats}
    <div class="block"><a class="clearfix" href="{$smarty.const.SITE_ROOT}/{$cats->mCats[c].menulinktext}/"><span class="thumb" style="background-image: url({$smarty.const.SITE_ROOT}{$smarty.const.IMG_DIR}/thumbnails/{$cats->mCats[c].img_category});"></span><span class="info">{$cats->mCats[c].name|escape:'htmlall'|widont}</span></a></div>
{/section}

I thought this script may have worked : -

https://appelsiini.net/2013/lazyload-190/

But I couldn't get that to work either.

Any idea's whether this can be done & if so how?!

Many thanks for any help on this one :)

Link to comment
Share on other sites

To add, I tried the latest version of the script : -

https://github.com/tuupola/lazyload#changelog

& tried modifying this code : -

let images = document.querySelectorAll(".example"); lazyload(images);

 

To the span name but still nothing, the rendered HTML of the image is like this :-

<span class="thumb" style="background-image: url(https://www.example.com/images/thumbnails/example.jpg);"></span>

I wonder if the lack of height & width maybe causing it? (this info is in the CSS file but I'll try adding it inline now)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.