Git Product home page Git Product logo

lazim's Introduction

lazim

Super tiny and simple image lazy load library. 350 bytes gzipped.

Install

npm i lazim --save

Usage

lazim doesn't really care about your markup. This works:

<img data-src='/image.jpg' />

But so does this:

<div data-src='/image.jpg'>
  <img />
</div>

And so does this:

<div data-src='/image.jpg'>
  <div
    <img />
  </div>
  <h2>silly image</h2>
</div>

Instantiating

To run lazim, import bind and call it:

import { bind } from 'lazim'

bind()

You can also pass a different attribute, if you don't like data-src:

bind('data-url')

lazim checks if there are images in the viewport on initial load, but in the event you need to run this again when adding/animating elements:

import { update } from 'lazim'

update()

Once bound, lazim removes the data-src attribute to prevent duplicate event listeners.

Animation

lazim adds a couple helper classes to the element you defined data-src on:

When visible in the viewport, it will receive an is-visible class. When the image loads, it receives an is-loaded class.

<div class='is-visible is-loaded'>
  <img src='/image.jpg' />
</div>

Re-binding

If you're using a PJAX library like operator, you'll need to re-bind new images that are added to the DOM on each page load. In that case, just call bind again whenever the page updates:

router.on('after', () => bind())

License

MIT License © Eric Bailey

lazim's People

Contributors

estrattonbailey avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

lazim's Issues

On page load/reload images aren't loading in the viewport until the first scroll event

This is probably something to do with the way I've setup my slater theme, but when the user makes a reload, say halfway down the page... lazim isn't loading any images until the first scroll event on the window. Same is true for the top of page first load... Is there a way to load any images that are already in the viewport before scrolling? Or an option that sets it just below the viewport?

Thanks again for sharing these contributions, learning quite a bit from your setups, and it's much appreciated!

Lazy image with low quality placeholder

Would it be possible (somehow) to have an image placeholder just before the real image loads?
It's just so the content area doesn't jump when images are loading.

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.