Giter VIP home page Giter VIP logo

responsively-lazy's Introduction

Responsively Lazy

Probably the best lazy loading implementation available.

  • Perfect for responsive images
  • Truly lazy (absolutely no unnecessary requests)
  • SEO friendly (valid HTML)
  • Supports WebP and AVIF
  • Customizable load threshold
  • Lazy load background images
  • Lazy load HTML

You can find a demo at ivopetkov.github.io/responsively-lazy-v3/ and learn how the magic works at ivopetkov.com/responsively-lazy/

Download and install

Download the minified JS file or install via npm.

npm install responsively-lazy

The library does not have any dependencies, and it's just 2.5kb when gzipped and minified.

Usage

  • Include the JS file in the head of the document
<script async src="responsively-lazy.min.js"></script>
  • Add the following code for each image
<img
    src="images/2500.jpg"
    style="width:100%;aspect-ratio:400/274;"
    srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
    data-responsively-lazy="images/400.jpg 400w, images/600.jpg 600w, images/800.jpg 800w, images/1000.jpg 1000w"
/>

Values to customize:

aspect-ratio

The aspect ratio of the image (width/height) to reserve space. Skip if you don't know it.

src

The default (largest) image size. Will be used by very old browsers, search engines, social networks, etc.

data-responsively-lazy (the star of the show)

Must contain a list of image versions separated by commas. Each version must contain a path to the image and its size. Optionaly you can specify the file type (webp or avif). The first supported format for the selected width is used.

data-responsively-lazy="images/400.avif 400w avif, images/400.webp 400w webp, images/400.jpg 400w, ..."

The paths may be encoded if there are special characters like commas, spaces, etc. Example: https%3A%2F%2Fexample.com%2Fimage.jpg

data-responsively-lazy-threshold

Specify how close to the viewport an image should be to start loading.

data-responsively-lazy-threshold="500px" // can use percents too

data-responsively-lazy-type=html

Lazy load HTML. Can contain JavaScript.

<div
    data-responsively-lazy-type="html"
    data-responsively-lazy="hello world"
></div>

data-responsively-lazy-type=background

Lazy load image as a background image.

<div
    data-responsively-lazy-type="background"
    data-responsively-lazy="images/400.jpg 400w, images/600.jpg 600w, images/800.jpg 800w, images/1000.jpg 1000w"
></div>

data-on-responsively-lazy-load

Run JavaScript code when the image is loaded.

data-on-responsively-lazy-load="..."

A new concept

Responsively Lazy is very different from the other lazy loading libraries. Others make you break your HTML by removing the src attribute, or make you put some tiny version there, or make you use <noscript> to make your images appear in search engines (like Google Images). The following code has worked for ages:

<img src="image.jpg" />

Let's not break it when we can enhance it.

<img src="image.jpg" data-responsively-lazy="image-200.jpg 200w, image-400.jpg 400w" srcset="..." />

Browser support

The lazy loading works in browsers supporting the srcset attribute and JavaScript. As of December 2023 that's 97.6%. Unsupported browsers, search engines and social networks will load the image in the src attribute. Make sure it's high resolution.

DOM changes

The library will listen for DOM changes and you can also request loading by calling responsivelyLazy.run().

License

Free to use under the MIT license.

Got questions?

You can find me at @IvoPetkovCom and ivopetkov.com

responsively-lazy's People

Contributors

gianpaj avatar ivopetkov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

responsively-lazy's Issues

Horizontal scroll and unknown image aspect ratio

Hi, does it work in horizontally scrollable element when I do not know image aspect ratio?
I have 50 small images, different image aspect ratio on page with horizontal scrolling.
In total they have 250kB and they are 50 requests.
Thanks!

Fallback when image error

Hello, I tried your lazyload script. I used vanilla lazyload before, and in vanilla lazyload, there is an option callback_error that can be used for a fallback function when an image fails to load. Is it possible to do the same with this script? Thank you

Background Images

I know that this is designed to solve issues using srcset, which isn't supported for background images, but it is hard to justify pulling in two dependencies, one for responsive img tags, and another for background images. Is there some kind of way to adapt this for background images, or provide support for that using another technique?

ie 11 and responsively-lazy height

Well today I checked my website on ie 11 and it was not working I mean I couldn't see images.
Turn out to be img.responsively-lazy height was 0 and ie was unable to run your JS I guess where it overwrites height.

so I removed height:0 and now its working fine.

Any specific reason for height 0 ??

no support for pixel density descriptor (e.g. 2x)?

When a srcset image is specified with a pixel density descriptor instead of a width descriptor as explained here:
https://webkit.org/blog/2910/improved-support-for-high-resolution-displays-with-the-srcset-image-attribute/
or more detailed in the official spec:
https://html.spec.whatwg.org/multipage/embedded-content.html#width-descriptor
the higher resolution image will be removed from srcset.

For example something like this

<img class="responsively-lazy" [...] data-srcset="image/3945799cca93f17f_280x280.jpg, image/[email protected] 2x">

will be transformed into this

<img class="responsively-lazy" [...] srcset="image/3945799cca93f17f_280x280.jpg">

responsively-lazy and Bootstrap сarousel

Is there a way to load images in Bootstrap carousel when it start rotating?
For now i use:
$('.carousel').each(function () { $(this).on('slid.bs.carousel', function () { responsivelyLazy.run(); }); });
But it start loading only when rotation stops.
slide.bs.carousel not an option - image is still hidden.

no-script not working

Howdy,

Did some testing and found with JS disabled within the Browser, no images will show.

Windows 7 64bit
Firefox 53.0.2
Opera 45

I can get image to show if "srcset" attribute is removed.

Other than that it looks like a nice script you've put together. Kudos for providing it open source with MIT Licensing.

Re-running after DOM changes

For this project we've got to cycle through a series of images (a series of <img> tags in a container, cycling through them toggling a hidden class)

What I can't figure out is how to re-run responsively-lazy against the new elements (which were hidden when it ran). I've tried playing around with a browserified version and trying to make it re-initializable but haven't had much luck.

Chrome and missing images on scrolling

I've noticed an issue that seems limited to Chrome. When scrolling a page of responsively-lazy images, sometimes the transparent placeholder (srcset) doesn't get swapped out for the image. It seems like the javascript occasionally doesn't fire. I've seen this on a few recent versions of Chrome 5x, but haven't tried rolling back to 49.xx or earlier.

I've whipped up a simple test page. Just load up Chrome and scroll down as normal and wait for a row of blank images. If you scroll them out of view and back into view, they then load.
https://www.harpgallery.com/test.html

Maybe it's a Chrome bug?

The images do not scale down

I don't know if this is intentional or not but when the screen goes from big to small and the "big" image was already loaded the "small" image is not loaded. I'm using quotes because I have the following scenario:

  1. I use Bootstrap's grid feature on a website and I have a sidebar displayed for medium and large screens.
  2. On small screens the sidebar is not displayed on the side so the main content area is bigger than on the medium screen. In this case I use bigger images but they are not loaded.
    I guess your library assumes that bigger images are for big screens and once you loaded the biggest image you don't need to load the smaller image.

Retina Support

First thanks for this amazing Script.

There´s only one thing i'm missing. Is it possible to target Retina devices?

Cheers,
Roman

Src is always downloaded

This solution has a one unresolved behavior.
Each browser downloads image in src before when plugin switch image in data-src.

In your example each browser download 2000px image and after then decides what to replace and show.
i think that is not very useful to insert a large picture like a fallback.

Try to mention this behavior in readme.md

Google Bot prefers scrset over src

Hi Ivo.
It is not an issues but I'd like to mention that Google bot uses scrset over src and executes JS of course.
So Google bot will not use the default large image in src but will use the one inserted into srcset.

Lazy load Instagram Images - Instagram Api display

Hello,

I am looking a way to lazy load the images from instagram basic display api.

I use the code:
<img class="instagram-feed__item__image" data-src="<?php echo $image_url; ?>" alt="<?php echo $item['caption']; ?>" class="responsively-lazy">

But the Image url is like: https://scontent.cdninstagram.com/v/t51.29350-15/133049651_2767029926897156_3167520314147899187_n.jpg?_nc_cat=105&ccb=2&_nc_sid=8ae9d6&_nc_ohc=LbOZsO0i_PsAX_swsGB&_nc_ht=scontent.cdninstagram.com&oh=d7ca6bd41c09130304ad5e6b760a9889&oe=60137C1B

Any idea how to apply lazy load?

Regards,
Dimitris

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.