Giter Site home page Giter Site logo

hugo-mods / lazyimg Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 4.0 258 KB

Lazy image loading with superpowers

Home Page: https://hugo-mods.github.io/#lazyimg

CSS 3.27% HTML 96.73%
hugo hugo-module lazysizes lazyload-images lqip responsive-images hugo-assets hugo-webp webp

lazyimg's Introduction

Logo

lazyimg

Lazy image loading with superpowers.

  • Lazy loading for images via lazySizes
  • No-Script/No-JS safe: Fallback to browser's native method
  • Image previews: Blur-up Low Quality Image Placeholder (LQIP) technique
  • Automatic responsive image resizing
  • Multiple supported renderers (e.g. LQIP with WebP support)

Used by the Osprey Delight theme, which directly benefits from this module!

Quickstart

Go get it

Initialize Hugo's mod system on your site (replace username and repo):

hugo mod init github.com/{username}/{repo}

Add module to site's config (e.g. config.yaml):

module:
  imports:
  - path: github.com/hugo-mods/lazyimg

Get the module (also upgrades existing one):

hugo mod get -u

Usage

  1. Put the images which you want to use in the assets directory of your project. They should be in a high resolution and will be resized automatically.
  2. Add the following boilerplate setup code to your site's <head>:
{{ partial "lazyimg-setup" }}
  1. Load the image:
    • In a template file: Call the lazyimg partial where you would usually use an img tag:
    {{ partial "lazyimg" "awesome-image.jpeg" }}
    
    • In a content file: Call the lazyimg shortcode:
    {{< lazyimg img="awesome-image.jpeg" >}}
    

For more advanced usage, please refer to the exampleSite for a practical approach or continue reading the theory as described in the next section.

Configuration

Please check out the example site's config for all options.

Resizers

Allowed values for resizer:

Name Description
simple Produces default image with maxSize, LQIP with lqipSize.
responsive Produces default image with maxSize, LQIP with lqipSize and responsive images based on responsiveSizes.
auto Produces default image with maxSize, LQIP with lqipSize and partitions (max. of 5) responsive sizes based on an algorithm.

Renderers

Allowed values for renderer:

Name Description
simple Responsive image lazy-loading.
lqip Responsive image lazy-loading with LQIP blur-up preview. Use with lazyimg.css.
lqip-webp Responsive image lazy-loading lqip with additional WebP support.
native Browser-native loading via loading="lazy". Does not require JS, used as fallback in other renderers when noscript is true.

CSS

The lazyimg.css is the recommended boilderplate CSS. It contains rules for blur-up animation, hiding "broken image icon" on lazy-loading and no-js selector.

Disabled JS

Support for disabled JS can be accomplished by adding a "no-js" class to your html root tag, e.g.:

<html lang="en" class="no-js"> <!-- ... --> </html>

Then, to remove the class when the client indeed supports JS, add the following script:

<script>document.documentElement.className = "js"</script>

Alternatively, use lazyimg-setup-nojs instead of lazyimg-setup which will do the replacement for you. In this case, you only need to add the no-js class to your html root tag.

lazyimg's People

Contributors

kdevo avatar

Stargazers

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

Watchers

 avatar  avatar

lazyimg's Issues

Allow defining an error-handler when asset could not be found

Additionally, we should allow defining a default behavior (error-handler config key) when the specified asset could not be found. Some ideas:

  • error-handler/warning.html: partial that just prints a warning when an image could not be found
  • error-handler/error.html: partial that prevents site from being rendered when image could not be found (current behavior)
  • error-handler/static.html: partial that prints a warning, but tries to use the image statically (from static folder)

Resizer `responsive` exceeds `maxSize` boundary

When using the resizer responsive with responsiveSizes exceeding maxSize, one image after maxSize is included in the resulting image set.

Reproduction

Example config:

maxSize: 1000x
responsiveSizes: ["512x", "1024x", "1280x"]
  • Want images: ["512x"] (maybe it also makes sense to include the "1000x" here?)
  • Got images: ["512x", "1024x"]

Resolution

  • Ensure that no image exceeds user-defined maxSize for responsive resizer
  • Ensure consistency for other resizers, too

Figure & Figcaption

Hello. As a result of this mod, the pictures are wrapped in the <picture> tag, it would be nice if the pictures were wrapped in the <figure> tag and at the same time so that you can specify the figcaption="text" setting in the shortcode

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.