Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pedro Menezes
committed
Aug 19, 2011
0 parents
commit d554131
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# jQuery lazyload | ||
|
||
jQuery lazyload makes your site faster. | ||
|
||
It loads your images only when the user scrolls down the page. | ||
|
||
## Example | ||
|
||
### Put the images that will lazy load in this way | ||
|
||
Noscript is necessary for the browsers with JavaScript off. | ||
|
||
<pre> | ||
<code> | ||
<img class="lazy" src="placeholder.gif" original-src="original-image.jpg" /> | ||
<noscript> | ||
<img src="original-image.jpg" /> | ||
</noscript> | ||
</code> | ||
</pre> | ||
|
||
### Load the library | ||
|
||
<pre> | ||
<code> | ||
<script src="jquery.js"></script> | ||
<script src="jquery.lazyload.js"></script> | ||
</code> | ||
</pre> | ||
|
||
### Turn on lazyload for the selected images | ||
|
||
In this case, there is a 30px threshold, so the images will load when the fold is 30px from them. The threshold is optional. | ||
|
||
<pre> | ||
<code> | ||
<script> | ||
$(function(){ | ||
$('img.lazy').lazyload({threshold: 30}); | ||
}); | ||
</script> | ||
</code> | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
(function($){ | ||
$.fn.lazyload = function(options){ | ||
var opts = $.extend(jQuery.fn.lazyload.defaults, options); | ||
var elements = this; | ||
|
||
$(window).bind('scroll', function(e){ | ||
loadAboveTheFoldImages(elements, opts); | ||
}); | ||
|
||
loadAboveTheFoldImages(elements, opts); | ||
|
||
return this; | ||
}; | ||
|
||
jQuery.fn.lazyload.defaults = {threshold: 0}; | ||
|
||
function aboveTheFold(element, options){ | ||
var fold = $(window).height() + $(window).scrollTop(); | ||
return fold >= $(element).offset().top - (options['threshold']); | ||
}; | ||
|
||
function loadOriginalImage(element){ | ||
$(element).attr('src', $(element).attr('original-src')).removeAttr('original-src'); | ||
}; | ||
|
||
function loadAboveTheFoldImages(elements, options){ | ||
elements.each(function(){ | ||
if (aboveTheFold(this, options) && ($(this).attr('original-src'))){ | ||
loadOriginalImage(this); | ||
} | ||
}); | ||
}; | ||
})(jQuery); |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<html> | ||
<head> | ||
<style type="text/css"> | ||
img{height:660px;width:990px;} | ||
</style> | ||
</head> | ||
<body> | ||
<img original-src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/berlinwall/bp1.jpg" | ||
src="placeholder.gif" /> | ||
<noscript><img src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/berlinwall/bp1.jpg"/></noscript> | ||
|
||
<img original-src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/berlinwall/bp2.jpg" | ||
src="placeholder.gif" /> | ||
<noscript><img src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/berlinwall/bp2.jpg"/></noscript> | ||
|
||
<img original-src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/berlinwall/bp4.jpg" | ||
src="placeholder.gif" /> | ||
<noscript><img src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/berlinwall/bp4.jpg"/></noscript> | ||
|
||
<img original-src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/berlinwall/bp6.jpg" | ||
src="placeholder.gif" /> | ||
<noscript><img src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/berlinwall/bp6.jpg"/></noscript> | ||
|
||
<p>Photos from: <a href="http://www.boston.com/bigpicture/2011/08/remembering_the_divide.html">Remembering the Berlin Wall</a></p> | ||
|
||
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> | ||
<script type="text/javascript" src="jquery.lazyload.js"></script> | ||
|
||
<script type="text/javascript"> | ||
$(function(){ | ||
$('img').lazyload({threshold: 100}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |