Last active
December 11, 2015 01:49
-
-
Save joaojeronimo/4526403 to your computer and use it in GitHub Desktop.
A jQuery plugin to lazyLoad images
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
<html> | |
<head> | |
<title>lazyLoad Example</title> | |
</head> | |
<body> | |
<img class="lazy" data-src="myImage.png" alt="My image" /> | |
<script src="path/to/jQuery.js"></script> | |
<script src="path/to/lazyLoad.js" ></script> | |
<script> | |
$('img.lazy').lazyLoad(); | |
</script> | |
</body> | |
</html> |
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
/* | |
Instructions: | |
1) apply the class "lazy" to your images; | |
2) Instead of the "src" attribute, define the image source with "data-src"; | |
3) When you want to load your images, most likely after the document is ready, execute $('img.lazy').lazyLoad() | |
*/ | |
(function($){ | |
$.fn.lazyLoad = function() { | |
this.each(function (index) { | |
var src = $(this).attr('data-src'); | |
if (src !== undefined) { | |
$(this).attr('src', src); | |
} | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment