0

Following implementation of loading all the background images works fine but it does not have lazy loading. (code belongs to template provided by: https://html5up.net/multiverse

// Main.
    var $main = $('#main');

    // Thumbs.
        $main.children('.thumb').each(function() {

            var $this = $(this),
                $image = $this.find('.image'), $image_img = $image.children('img'),
                x;

            // No image? Bail.
                if ($image.length == 0)
                    return;

            // Image.
            // This sets the background of the "image" <span> to the image pointed to by its child
            // <img> (which is then hidden). Gives us way more flexibility.

                // Set background.
                    $image.css('background-image', 'url(' + $image_img.attr('src') + ')');

                // Set background position.
                    if (x = $image_img.data('position'))
                        $image.css('background-position', x);

                // Hide original img.
                    $image_img.hide();

        });

this is what I want after lazy loading and it is the result of above code

but using lazy loading (https://github.com/verlok/lazyload) and changing to following code messes up with the CSS property of images like background-size:cover fails to work but lazy loading works fine.

// Main.
    var $main = $('#main');

    // // Thumbs.
        $main.children('.thumb').each(function() {


            var $this = $(this),
                $image = $this.find('.image'), $image_img = $image.children('img'),$thumb_img = $image.children('img'),
                x;

            $('<img>').attr('data-src',$image_img.attr('data-src')).on('load', function(){
                if($(this).attr('src') == $(this).attr('data-src')) {
                    $(this).css({'height': 'auto', 'width': '100%'});
                }
                $image.css('background-image', 'url(' + $image_img.attr('data-src') + ')');
                $image.css('background-size', 'cover');
                $image.css('background-position', 'center');
                    if (x = $image_img.data('position'))
                        $image.css('background-position', x);
                $image_img.hide();

            });




        }); 

here it fails to preserve css property as shown in first image, this is result of above code

It would be a great help if anyone can help me with this.

Utsav Rai
  • 1
  • 1
  • Do you have a sample of your html? What is `$('')` attempting to do? – tic Oct 15 '19 at 20:31
  • 1. This is my html - https://github.com/utsavrai/utsavrai.github.io/blob/master/index.html 2. I am using https://stackoverflow.com/a/13655739/6805717 to lazy load my background images which uses $('') – Utsav Rai Oct 16 '19 at 10:42

0 Answers0