Unveil.js for Rails
Lazy loaded images on rails. If you want a quick start using
unveil.js with rails there is now a gem for that.
By loading your images as they appear in the viewport rather than on load you
can improve your rails application's page speed.
Installation
Add the unveil-rails gem into your Gemfile
:
gem 'unveil-rails', '~> 0.2.1'
Install unveil.js and initializer into your rails app:
bin/rails g unveil:rails:install
Include the unveil.js initializer in app/assets/javascripts/application.js
:
Now use the helper method where ever you want lazy images:
<%= lazy_image_tag('an-image.png') %>
<%= lazy_image_tag('an-image.png', retina: 'a-retina-image.png') %>
<%= lazy_image_tag('an-image.png', placeholder: 'placeholder.png') %>
Configuration
The installation generator will have added an initializer to your application.
In this file there are a couple of settings you can change.
Threshold
Unveil::Rails.config.threshold = 200
Setting this variable to an integer will set the unveil threshold setting
as documented here.
Default placeholder
You may set an alternative default placeholder image:
Unveil::Rails.config.default_placeholder = 'default.jpg'
You can set the placeholder per lazy image by passing in the placeholder option:
<%= lazy_image_tag('an-image.png', placeholder: 'placeholder.png') %>
Credits
Developed and maintained by Made Tech. Key contributions:
And of course Luís Almeida creator of unveil.js.
License
Copyright © 2014 Made Tech Ltd. It is free software, and may be
redistributed under the terms specified in the LICENSE file.