Find file
Fetching contributors…
Cannot retrieve contributors at this time
158 lines (136 sloc) 5.97 KB
<!DOCTYPE html>
<title>jQuery Unveil - A very lightweight plugin to lazy load images</title>
body {
text-align: center;
background: #121416;
font-family: sans-serif;
color: white;
padding: 50px 0;
h1 {
margin-bottom: 60px;
font: italic bold 3em serfif;
text-shadow: 0 0 1px #17EB00;
color: #17EB00;
h2 {
margin-top: 60px;
margin-bottom: 20px;
font-size: 1.1em;
color: #6C816A;
p {
margin: 5px auto;
max-width: 600px;
color: #F5F5F5;
font-size: .9em;
line-height: 1.2;
a {
color: #3595b5;
strong {
font-size: 1.5em;
color: white;
code {
background: black;
margin: 20px 0 0 0;
padding: 5px 10px;
display: inline-block;
border-radius: 5px;
code > {
color: #D14;
code > {
color: #099;
ul {
list-style: none;
margin: 50px 0;
padding: 0;
ul > li {
display: block;
margin: 30px auto;
position: relative;
ul > li > img {
width: 600px;
height: 400px;
background: #FFF;
border: 10px solid #FFF;
ul > li:after {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
background: #000;
z-index: 1;
iframe {
margin: 30px 0 40px 0;
<script src="//"></script>
<script src="jquery.unveil.js"></script>
$("li img").unveil();
<p>Most of us are familiar with the <a target="_blank" href="">Lazy Load</a> plugin by <a target="_blank" href="">Mika Tuupola</a>.
<p>This plugin is very useful and it boosts performance delaying loading of images in long web pages because images outside of viewport (visible part of web page) won't be loaded until the user scrolls to them.</p>
<p>Lazy Load has some cool options such as custom effects, container, events or data attribute. If you're not gonna use any of them you can reduce the file size by leaving just the essential code to show the images.</p>
<p>That's what I did and this is my lightweight version of Lazy Load - <strong>less than 0.5kb</strong>.</p>
<a href="" class="twitter-share-button" data-text="unveil.js - A very lightweight plugin to lazy load images" data-via="lmgalmeida" data-related="lmgalmeida" data-count="none">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);;js.src="//";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<p>Include the actual image source in a "data-src" attribute.</p>
<p>Use a placeholder image in the src attribute, something to be displayed while the original image loads.</p>
<code>&lt;img <span class="blue">src=</span><span class="red">"bg.png"</span> <span class="blue">data-src=</span><span class="red">"img.jpg"</span> /></code><br>
<code>$(<span class="red">"img"</span>).unveil();</code>
<p>By default, images are only loaded and "unveiled" when user scrolls to them and they became visible on the screen.</p>
<p>If you want your images to load earlier than that, lets say 200px before they get visible, you just have to:</p>
<code>$(<span class="red">"img"</span>).unveil( <span class="blue">200</span> );</code>
<p>You can still trigger image loading whenever you need.</p>
<p>All you have to do is select the images you want to "unveil" and trigger the event:</p>
<code>$(<span class="red">"img"</span>).trigger( <span class="blue">"unveil"</span> );</code>
<p>Download the last version from the project page on <a href="">GitHub</a>.</p>
<p>Scroll down to see it working.</p>
<li><img src="img/bg.png" data-src="img/img1.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img2.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img3.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img4.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img5.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img6.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img7.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img8.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img9.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img10.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img11.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img12.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img13.jpg" /></li>
<li><img src="img/bg.png" data-src="img/img14.jpg" /></li>
<a href=""><img style="position: absolute; top: 0; left: 0; border: 0;" src="" alt="Fork me on GitHub"></a>
var _gaq=[['_setAccount','UA-30936310-1'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];