question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add lazy load to background images

See original GitHub issue

Hi all, Love Slick, thanks for creating this! Was wondering if the lazy load logic could also be added to background images that may be attached to each slide. Right now I’m using background-size:cover on desktop (and manually setting the height) compared to swapping the background out for a static image on smaller devices. I could obviously add the lazy load handler for the inline images, but they’d be loaded regardless because of the background image reference. Thanks!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:7
  • Comments:28 (2 by maintainers)

github_iconTop GitHub Comments

23reactions
lnilyacommented, Jun 16, 2017

I use the lazyLoaded Event and replace the src attribute with the background attribute. Looks something like this:

$('.ref-slider').on('lazyLoaded', function (e, slick, image, imageSource) {
        
       image.attr('src',''); //remove source
        image.css('background-image','url("'+imageSource+'")'); //replace with background instead
        image.css('background-size','cover');
        image.css('background-position','center');
        image.css('background-repeat','no-repeat');

    });

Maybe this helps! This way you build your slides normally with tags and data-lazy attributes, but have the benefits of the fixed height/cover etc. props. Also fading on of the slick slider works properly.

22reactions
andrewtibbettscommented, Jan 14, 2015

I often have slides generated from a database (images + etc are set in CMS) and are therefore not available to CSS. And having inline images act like background-size:cover takes some js trickery and is a real pain. This would be of great help to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy loading images - web.dev
Chrome and Firefox both support lazy loading with the loading attribute. This attribute can be added to <img> elements, and also to <iframe> ......
Read more >
Lazy Loading Images – The Complete Guide - ImageKit.io
Images on a webpage can be loaded in two ways - using the <img> tag, or using the CSS `background` property. Let's first...
Read more >
How to Lazy Load div background images - Stack Overflow
First you need to think off when you want to swap. For example you could switch everytime when its a div tag thats...
Read more >
Lazy loading CSS background images for better website ...
Find out how to lazy load CSS background images to improve your website loading performance.
Read more >
How to Lazyload Background Images in WordPress - MainWP
Another plugin option for lazyloading of background images is to use the Lazy Loader plugin. ... If you are using the WP Rocket...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found