Download
User Rating: 0/5 ( 0 votes)
lazyScript is a simple jquery plugin to execute any javascript code in lazy mode. You only have to provide a callback to execute for each element appearing in the viewport.
Source: github.com
1. INCLUDE JS FILES
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../jquery.lazyscript.js"></script>
2. HTML
<div class="lazy" data-src="1.jpg"></div>
<div class="lazy" data-src="2.jpg"></div>
<div class="lazy" data-src="3.jpg"></div>
<div class="lazy" data-src="4.jpg"></div>
<div class="lazy" data-src="5.jpg"></div>
<div class="lazy" data-src="6.jpg"></div>
<div class="lazy" data-src="7.jpg"></div>
<div class="lazy" data-src="8.jpg"></div>
3. JAVASCRIPT
$(document).ready(function () {
$(window).lazyScript({
callback: function ($element) {
var src = $element.data('src');
$element.html('<img src="' + src + '">');
}
});
});
4. OPTIONS
-
selectorClass (string) Name of the class used to select the lazy elements. Note that the class is removed after the callback to avoid being executed twice. By default is lazy
-
callback (function) The callback executed for each lazy element. The argument provided is the jquery object with the element.
-
threshold (integer) By default is 0
-
scrollInterval (integer) Defines the duration of the timeout used while scrolling to prevent trigger the scroll callback too many times and improve the performance. By default is 500.