Giter Site home page Giter Site logo

1v / lazy-images-rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rhardih/lazy-images-rails

1.0 2.0 1.0 52 KB

Lazy loaded images with placeholders and noscript fallback

License: MIT License

Ruby 78.48% CSS 3.12% JavaScript 2.00% HTML 16.40%

lazy-images-rails's Introduction

lazy-images-rails

lazy-images-rails is a Rails plugin that augments the standard image_tag helper to provide instant placeholders for images while the actual image is being loaded.

Why?

Due to the asynchronous nature of images in web pages, page content is often rendered a long time ahead of images. In many cases this leads to content jumping downwards once an image above it has been loaded.

This is very annoying when you've found a thing to click/tap on, and just before you do, it moves out of the way. Especially when it causes a misclick/tap on something unentended.

This problem can be solved in many ways, but the solution provided by this plugin, is to simply use a placeholder until the real image is loaded.

How?

Instead of rendering a bare <img /> tag, the Rails image_tag helper renders an SVG placeholder behind the image while it is loading. This placeholder takes up the same space as it's constituent image.

Usage

Include the supplied css asset in the manifest, application.css:

*= require lazy_images_rails

Add an image in a view, index.html.erb:

<%= image_tag 'foo.png' %>

In case an image needs to be inserted without a placeholder, since the image_tag helper has been aliased, accessing the unmodified helper is done with the suffix:

<%= image_tag_without_lazy_images 'foo.png' %>

A note about non-square images

Since the image ratio of square images are all the same, by default the placeholder can be used without further ado to take up the same space as the square image it replaces.

If an image however, is not square, it becomes necessary to provide the dimensions explicitly.

The image_tag helper already supports this through the size attribute:

image_tag('foo.png', size: "123x456")

So setting this attribute will affect the placeholder as well and set width and height accordingly in the same manner.

Styling the placeholder

The default placeholder image is a simple graphic of a mountain and a moon. Both have been supplied with targetable classes for individual styling. Create a custom rule with these classes, e.g. (scss):

.rli-wrapper {
  .rli-placeholder {
    background: #fff;

    .moon, .mountain {
      fill: #ddd;
    }
  }
}

And the constituent paths in the the svg image will be styled accordingly.

Using custom svg as placeholder

The placeholder is stored as a string value in LazyImages::Rails.placeholder, so in order to overwrite the default, create an initializer and set it accordingly, e.g. config/initializers/lazy_images-rails.rb:

custom_placeholder = File.read("#{Rails.root}/app/assets/images/custom.svg")
LazyImages::Rails.placeholder = custom_placeholder

Here assuming you have placed your custom placeholder in app/assets/images/custom.svg.

Testing

Ruby tests

rake test

License

MIT-LICENSE | http://rhardih.mit-license.org

lazy-images-rails's People

Contributors

1v avatar rhardih avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

mmorast

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.