Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
158 lines (136 sloc) 5.97 KB
<!DOCTYPE html>
<html>
<head>
<title>jQuery Unveil - A very lightweight plugin to lazy load images</title>
</head>
<style>
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 > span.red {
color: #D14;
}
code > span.blue {
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;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="jquery.unveil.js"></script>
<script>
$(function(){
$("li img").unveil();
});
</script>
<body>
<h1>unveil.js</h1>
<p>Most of us are familiar with the <a target="_blank" href="http://www.appelsiini.net/projects/lazyload">Lazy Load</a> plugin by <a target="_blank" href="http://www.appelsiini.net/">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="https://twitter.com/share" 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.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<h2>Usage</h2>
<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>
<h2>Option</h2>
<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>
<h2>Trigger</h2>
<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>
<h2>Download</h2>
<p>Download the last version from the project page on <a href="https://github.com/luis-almeida/unveil">GitHub</a>.</p>
<h2>Demo</h2>
<p>Scroll down to see it working.</p>
<ul>
<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>
</ul>
<a href="http://github.com/luis-almeida"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://a248.e.akamai.net/camo.github.com/bec6c51521dcc8148146135149fe06a9cc737577/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub"></a>
<script>
var _gaq=[['_setAccount','UA-30936310-1'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
</script>
</body>
</html>