Code Monkey home page Code Monkey logo

lazyestload.js's Introduction

image

load images only when they are in (and remain in) the viewport.

in only about 400 bytes of javascript ๐Ÿ˜ฑ

  <img class="lazyestload" src="images/placeholder/sunset.jpg" data-src="images/sunset.jpg" >
  
  <script src="js/lazyestload.min.js"></script>
</body>

or with data-srcset

  <img class="lazyestload" src="images/placeholder/sunset.jpg" data-srcset="images/sunset-1x.jpg 1x, images/sunset-2x.jpg 2x, images/sunset-3x.jpg 3x" >
  <img class="lazyestload" src="images/placeholder/sunset.jpg" data-srcset="images/sunset-400.jpg 400w, images/sunset.jpg 1600w" >

or with picture and source

  <picture>
  	<source media="(max-width: 400px)" data-srcset="images/sunset-400.jpg" >
  	<source media="(max-width: 401px)" data-srcset="images/sunset.jpg" >
  	<img class="lazyestload" src="images/placeholder/sunset.jpg" >
  </picture>

demo lazyestload load images only when they are in the viewport (or within 100px) and when the user has stopped scrolling (meaning the user can scroll past images and they wont be loaded)

demo lazyload load images only when they are in the viewport or 100px beneath

When using placeholders the image should be less than 1kb, ~40px wide, and should have the same aspect ratio as the image that will replace it. (to avoid any layout jank) Also the "blur up" affect in the first demo works by adding the following css.

img {
    transition: filter 0.3s;
}

img.lazyestload {                
    width: 100%;
    filter: blur(8px);
}

I've added a placeholder generator. To use run npm i and then npm build you can also serve the demo locally with npm bsw (build, serve, watch)

lazyestload.js's People

Contributors

erjanmx avatar paul-browne 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lazyestload.js's Issues

Doesn't work for dynamic content

If an image is added by jquery, react or an ajax request lazyestload will not work if it already detached the scroll listener

Consider using Intersection Observer API?

background images

I tend to use background images for responsive work these days, is there any way this could support background images?

Thank you for this...

... Well done seams very good.

What is the browsers support?

Please advise how to make image placeholders what size and quality etc. Some tips or auto scripts you know php?

Thank you again for this. I will try it.

Working with "data-srcset" -> data-srcset broken?

Hi there,

phenomenal work.

I'm struggling to get responsive images with srcset to work.
The data-srcset breaks for me.

Getting the following error:
image

Anyone experiencing the same issue?

Code:

<img class="lazyestload" src="images/imagethumb.jpg" data-srcset="images/image1.jpg 1x, images/image2.jpg 2x" >

Are the loaded images cached by browser?

I ran your demo several times but it seams the images are requested every page visit.
It will be good to have the cached images displayed if available instead of showing placeholder images on view port.

Picture element

Are you maybe interested in supporting picture and source elements?

jQuery with "on" listening

If you plan to load a ajax and put new dom elements, you will need to listen a sroll after load:

$(function () { function i() { var t = document.querySelectorAll("img.lazyload"), e = t.length; for (!e && window.removeEventListener("scroll", i); e--;) { var r = window.innerHeight; if (t[e].getBoundingClientRect().top - r <= 100) { if (t[e].getAttribute("data-src") && (t[e].src = t[e].getAttribute("data-src")), t[e].getAttribute("data-srcset") && (t[e].srcset = t[e].getAttribute("data-srcset")), "PICTURE" === t[e].parentElement.tagName) for (var n = t[e].parentElement.querySelectorAll("source"), a = n.length; a--;) n[a].srcset = n[a].getAttribute("data-srcset"); t[e].addEventListener("load", function () { this.classList.remove("lazyload") }) } } } i(); $(window).on('resize scroll', function () { i(); }); });

Memory leak

Hi, i not use your library, i simple came from some news article. I look on your code and you not handle case when image not loaded and user continuously scrolling and user got this.
image

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.