mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
Lazyload: add lazyload widget
This commit is contained in:
parent
5d4529c37d
commit
a1c9e95639
16
dist/js/widgets/widget-lazyload.min.js
vendored
Normal file
16
dist/js/widgets/widget-lazyload.min.js
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
/*! Widget: lazyload - 10/25/2015 (v2.23.6) */
|
||||
!function(a,b){"use strict";var c=a.tablesorter;c.lazyload={init:function(b,d){"scrollstop"!==d.lazyload_event||c.addScrollStopDone||(c.addScrollStop(),c.addScrollStopDone=!0,a.event.special.scrollstop.latency=d.lazyload_latency||250),c.lazyload.update(b,d);var e=[d.lazyload_update,"pagerUpdate",d.columnSelector_updated||"columnUpdate",""].join(b.namespace+"lazyload ");b.$table.on(e,function(){c.lazyload.update(b,b.widgetOptions)})},update:function(a,b){var c=(/(\.|#)/.test(b.lazyload_imageClass)?"":".")+b.lazyload_imageClass;a.$table.find(c).lazyload({threshold:b.lazyload_threshold,failure_limit:b.lazyload_failure_limit,event:b.lazyload_event,effect:b.lazyload_effect,container:b.lazyload_container,data_attribute:b.lazyload_data_attribute,skip_invisible:b.lazyload_skip_invisible,appear:b.lazyload_appear,load:b.lazyload_load,placeholder:b.lazyload_placeholder})},remove:function(a,b){a.$table.off(a.namespace+"lazyload")}},c.addWidget({id:"lazyload",options:{lazyload_imageClass:"lazy",lazyload_update:"lazyloadUpdate",lazyload_latency:250,lazyload_threshold:0,lazyload_failure_limit:0,lazyload_event:"scrollstop",lazyload_effect:"show",lazyload_container:b,lazyload_data_attribute:"original",lazyload_skip_invisible:!1,lazyload_appear:null,lazyload_load:null,lazyload_placeholder:"data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="},init:function(a,b,d,e){c.lazyload.init(d,e)},remove:function(a,b,d){c.lazyload.remove(b,d)}}),c.addScrollStop=function(){var b=a.event.dispatch||a.event.handle,c=a.event.special,d="D"+ +new Date,e="D"+(+new Date+1);c.scrollstart={setup:function(e){var f,g=a.extend({latency:c.scrollstop.latency},e),h=function(a){var c=this,d=arguments;f?clearTimeout(f):(a.type="scrollstart",b.apply(c,d)),f=setTimeout(function(){f=null},g.latency)};a(this).bind("scroll",h).data(d,h)},teardown:function(){a(this).unbind("scroll",a(this).data(d))}},c.scrollstop={latency:250,setup:function(d){var f,g=a.extend({latency:c.scrollstop.latency},d),h=function(a){var c=this,d=arguments;f&&clearTimeout(f),f=setTimeout(function(){f=null,a.type="scrollstop",b.apply(c,d)},g.latency)};a(this).bind("scroll",h).data(e,h)},teardown:function(){a(this).unbind("scroll",a(this).data(e))}}}}(jQuery,window),/*!
|
||||
* Lazy Load - jQuery plugin for lazy loading images
|
||||
*
|
||||
* Copyright (c) 2007-2015 Mika Tuupola
|
||||
*
|
||||
* Licensed under the MIT license:
|
||||
* http://www.opensource.org/licenses/mit-license.php
|
||||
*
|
||||
* Project home:
|
||||
* http://www.appelsiini.net/projects/lazyload
|
||||
*
|
||||
* Version: 1.9.7
|
||||
*
|
||||
*/
|
||||
function(a,b,c,d){var e=a(b);a.fn.lazyload=function(f){function g(){var b=0;i.each(function(){var c=a(this);if(!j.skip_invisible||c.is(":visible"))if(a.abovethetop(this,j)||a.leftofbegin(this,j));else if(a.belowthefold(this,j)||a.rightoffold(this,j)){if(++b>j.failure_limit)return!1}else c.trigger("appear"),b=0})}var h,i=this,j={threshold:0,failure_limit:0,event:"scroll",effect:"show",container:b,data_attribute:"original",skip_invisible:!1,appear:null,load:null,placeholder:"data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="};return f&&(d!==f.failurelimit&&(f.failure_limit=f.failurelimit,delete f.failurelimit),d!==f.effectspeed&&(f.effect_speed=f.effectspeed,delete f.effectspeed),a.extend(j,f)),h=j.container===d||j.container===b?e:a(j.container),0===j.event.indexOf("scroll")&&h.bind(j.event,function(){return g()}),this.each(function(){var b=this,c=a(b);b.loaded=!1,(c.attr("src")===d||c.attr("src")===!1)&&c.is("img")&&c.attr("src",j.placeholder),c.one("appear",function(){if(!this.loaded){if(j.appear){var d=i.length;j.appear.call(b,d,j)}a("<img />").bind("load",function(){var d=c.attr("data-"+j.data_attribute);c.hide(),c.is("img")?c.attr("src",d):c.css("background-image","url('"+d+"')"),c[j.effect](j.effect_speed),b.loaded=!0;var e=a.grep(i,function(a){return!a.loaded});if(i=a(e),j.load){var f=i.length;j.load.call(b,f,j)}}).attr("src",c.attr("data-"+j.data_attribute))}}),0!==j.event.indexOf("scroll")&&c.bind(j.event,function(){b.loaded||c.trigger("appear")})}),e.bind("resize",function(){g()}),/(?:iphone|ipod|ipad).*os 5/gi.test(navigator.appVersion)&&e.bind("pageshow",function(b){b.originalEvent&&b.originalEvent.persisted&&i.each(function(){a(this).trigger("appear")})}),a(c).ready(function(){g()}),this},a.belowthefold=function(c,f){var g;return g=f.container===d||f.container===b?(b.innerHeight?b.innerHeight:e.height())+e.scrollTop():a(f.container).offset().top+a(f.container).height(),g<=a(c).offset().top-f.threshold},a.rightoffold=function(c,f){var g;return g=f.container===d||f.container===b?e.width()+e.scrollLeft():a(f.container).offset().left+a(f.container).width(),g<=a(c).offset().left-f.threshold},a.abovethetop=function(c,f){var g;return g=f.container===d||f.container===b?e.scrollTop():a(f.container).offset().top,g>=a(c).offset().top+f.threshold+a(c).height()},a.leftofbegin=function(c,f){var g;return g=f.container===d||f.container===b?e.scrollLeft():a(f.container).offset().left,g>=a(c).offset().left+f.threshold+a(c).width()},a.inviewport=function(b,c){return!(a.rightoffold(b,c)||a.leftofbegin(b,c)||a.belowthefold(b,c)||a.abovethetop(b,c))},a.extend(a.expr[":"],{"below-the-fold":function(b){return a.belowthefold(b,{threshold:0})},"above-the-top":function(b){return!a.belowthefold(b,{threshold:0})},"right-of-screen":function(b){return a.rightoffold(b,{threshold:0})},"left-of-screen":function(b){return!a.rightoffold(b,{threshold:0})},"in-viewport":function(b){return a.inviewport(b,{threshold:0})},"above-the-fold":function(b){return!a.belowthefold(b,{threshold:0})},"right-of-fold":function(b){return a.rightoffold(b,{threshold:0})},"left-of-fold":function(b){return!a.rightoffold(b,{threshold:0})}})}(jQuery,window,document);
|
359
docs/example-widget-lazyload.html
Normal file
359
docs/example-widget-lazyload.html
Normal file
@ -0,0 +1,359 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>jQuery plugin: Tablesorter 2.0 - Lazyload widget</title>
|
||||
|
||||
<!-- Demo stuff -->
|
||||
<link rel="stylesheet" href="css/jq.css">
|
||||
<link rel="stylesheet" href="css/jquery-ui.min.css">
|
||||
<link href="css/prettify.css" rel="stylesheet">
|
||||
<script src="js/prettify.js"></script>
|
||||
<!-- jQuery -->
|
||||
<script src="js/jquery-latest.min.js"></script>
|
||||
<script src="js/jquery-ui.min.js"></script>
|
||||
<script src="js/docs.js"></script>
|
||||
|
||||
<!-- Tablesorter: required -->
|
||||
<link rel="stylesheet" href="../css/theme.blue.css">
|
||||
<script src="../js/jquery.tablesorter.js"></script>
|
||||
<script src="../js/widgets/widget-lazyload.js"></script>
|
||||
|
||||
<script id="js">$(function() {
|
||||
$("table").tablesorter({
|
||||
theme : 'blue',
|
||||
widgets : [ 'lazyload', 'zebra' ],
|
||||
widgetOptions : {
|
||||
// widget options
|
||||
lazyload_imageClass : 'lazy',
|
||||
// scrollStop option (https://github.com/ssorallen/jquery-scrollstop)
|
||||
lazyload_latency : 250,
|
||||
// lazyload options (see http://www.appelsiini.net/projects/lazyload)
|
||||
lazyload_threshold : 0,
|
||||
lazyload_failure_limit : 0,
|
||||
lazyload_event : 'scrollstop',
|
||||
lazyload_effect : 'show',
|
||||
lazyload_container : window,
|
||||
lazyload_data_attribute : 'original',
|
||||
lazyload_skip_invisible : false,
|
||||
lazyload_appear : function( elements_left, settings ) {
|
||||
// callback fired when image is in view, but before it is loaded
|
||||
$(this).addClass( 'tablesorter-processing' );
|
||||
},
|
||||
lazyload_load : function( elements_left, settings ) {
|
||||
// callback fired after image has loaded
|
||||
$(this).removeClass( 'tablesorter-processing' );
|
||||
},
|
||||
lazyload_placeholder : 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
|
||||
}
|
||||
});
|
||||
});</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="banner">
|
||||
<h1>table<em>sorter</em></h1>
|
||||
<h2>Lazyload widget</h2>
|
||||
<h3>Flexible client-side table sorting</h3>
|
||||
<a href="index.html">Back to documentation</a>
|
||||
</div>
|
||||
<div id="main">
|
||||
|
||||
<p></p>
|
||||
<br>
|
||||
|
||||
<div id="root" class="accordion">
|
||||
|
||||
<h3><a href="#">Notes</a></h3>
|
||||
<div>
|
||||
<ul>
|
||||
<li>This widget, added in <span class="version">v2.23.6</span>, integrates the popular <a class="external" href="https://github.com/tuupola/jquery_lazyload">jQuery lazyload plugin</a> & <a class="external" href="https://github.com/ssorallen/jquery-scrollstop">jQuery scrollstop</a>.</li>
|
||||
<li>This widget will only work if:
|
||||
<ul>
|
||||
<li>The image class, set by the <code>lazyload_imageClass</code> option is included.</li>
|
||||
<li>The image <code>width</code> & <code>height</code> attributes are set.</li>
|
||||
<li>The url to the image is added to the data-attribute, set by the <code>lazyload_data_attribute</code> option, and not the image <code>src</code> attribute.</li>
|
||||
</ul>
|
||||
<pre class="prettyprint lang-html"><img class="lazy" data-original="images/logo.png" width="300" height="300"></pre>
|
||||
</li>
|
||||
<li>The images will load only if:
|
||||
<ul>
|
||||
<li>The image is inside the viewport.</li>
|
||||
<li>When the user has stoped scrolling; this is because the <code>lazyload_event</code> option is set by default to use the "scrollstop" event, which is added by the jQuery scrollstop plugin (see <code>lazyload_event</code> option for more details).</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>In this demo, scroll down the page to see the lazyload plugin in action.</li>
|
||||
<li>This widget will not work with the original tablesorter plugin (v2.0.5).</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<h3><a href="#">Options</a></h3>
|
||||
<div>
|
||||
<h4>Lazyload widget default options (added inside of tablesorter <code>widgetOptions</code>)</h4>
|
||||
<div class="tip">
|
||||
<span class="label label-info">TIP!</span> Click on the link in the option column to reveal full details (or <a href="#" class="toggleAll">toggle</a>|<a href="#" class="showAll">show</a>|<a href="#" class="hideAll">hide</a> all) or double click to update the browser location.
|
||||
</div>
|
||||
<table class="tablesorter-blue options">
|
||||
<thead>
|
||||
<tr><th>Option</th><th>Default</th><th class="sorter-false">Description</th></tr>
|
||||
</thead>
|
||||
|
||||
<tbody class="tablesorter-infoOnly">
|
||||
<tr><th colspan="3">Lazyload widget options</th></tr>
|
||||
</tbody>
|
||||
|
||||
<tbody>
|
||||
<tr id="lazyload_imageclass">
|
||||
<td><a href="#" class="permalink">lazyload_imageClass</a></td>
|
||||
<td><code>'lazy'</code></td>
|
||||
<td>
|
||||
This option contains the image class name targeted by the lazyload widget.
|
||||
<div class="collapsible">
|
||||
<br>
|
||||
This option is set up to add a period to the beginning of this option, to make a jQuery class name selector.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_update">
|
||||
<td><a href="#" class="permalink">lazyload_update</a></td>
|
||||
<td><code>'lazyloadUpdate'</code></td>
|
||||
<td>
|
||||
This option sets the event name that can be triggered to update the lazy load on newly loaded images.
|
||||
<div class="collapsible">
|
||||
<br>
|
||||
This widget will work with the pager and columnSelector widget, so you shouldn't need to trigger this event, but you may need to use it if new rows are added or a cell with an image is updated.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tbody class="tablesorter-infoOnly">
|
||||
<tr><th colspan="3">jQuery scrollstop plugin option</th></tr>
|
||||
</tbody>
|
||||
|
||||
<tbody>
|
||||
<tr id="lazyload_latency">
|
||||
<td><a href="#" class="permalink">lazyload_latency</a></td>
|
||||
<td><code>250</code></td>
|
||||
<td>
|
||||
Latency is the minimum time (in milliseconds) between the last scroll event and when the scrollstop event fires.
|
||||
<div class="collapsible">
|
||||
<br>
|
||||
For more details, see the <a class="external" href="https://github.com/ssorallen/jquery-scrollstop">main plugin page</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
|
||||
<tbody class="tablesorter-infoOnly">
|
||||
<tr><th colspan="3">jQuery lazyload plugin options</th></tr>
|
||||
</tbody>
|
||||
|
||||
<tbody>
|
||||
<tr id="lazyload_threshold">
|
||||
<td><a href="#" class="permalink">lazyload_threshold</a></td>
|
||||
<td><code>0</code></td>
|
||||
<td>
|
||||
By default images are loaded when they appear on the screen.
|
||||
<div class="collapsible">
|
||||
<p>If you want images to load earlier use threshold parameter. Setting threshold to 200 causes image to load 200 pixels before it appears on viewport.</p>
|
||||
For more details, see the <a class="external" href="http://www.appelsiini.net/projects/lazyload#setting-threshold">main documentation</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_failure_limit">
|
||||
<td><a href="#" class="permalink">lazyload_failure_limit</a></td>
|
||||
<td><code>0</code></td>
|
||||
<td>
|
||||
Setting failure_limit to 10 causes plugin to stop searching for images to load after finding 10 images below the fold.
|
||||
<div class="collapsible">
|
||||
<p>If you have a funky layout set this number to something high. Worst case being the actual number of images.</p>
|
||||
For more details, see the <a class="external" href="http://www.appelsiini.net/projects/lazyload#when-images-are-not-sequential">main documentation</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_event">
|
||||
<td><a href="#" class="permalink">lazyload_event</a></td>
|
||||
<td><code>'scrollstop'</code></td>
|
||||
<td>
|
||||
Event to trigger image loading.
|
||||
<div class="collapsible">
|
||||
<p>In the original plugin, the default for this option is "scroll". It was changed to "scrollstop" in this widget to only load images after scrolling stops.</p>
|
||||
<p>Alternative settings include "click" (user clicks on image to load it) or "mouseover".</p>
|
||||
<p>If this option is not set to "scrollstop", then the scrollstop plugin will not be initialized.</p>
|
||||
For more details, see the <a class="external" href="http://www.appelsiini.net/projects/lazyload#event-to-trigger-loading">main documentation</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_effect">
|
||||
<td><a href="#" class="permalink">lazyload_effect</a></td>
|
||||
<td><code>'show'</code></td>
|
||||
<td>
|
||||
By default plugin waits for image to fully load and calls <code>show()</code>.
|
||||
<div class="collapsible">
|
||||
<p>You can use any effect you want, e.g. <code>'fadeIn'</code> effect.</p>
|
||||
For more details, see the <a class="external" href="http://www.appelsiini.net/projects/lazyload#using-effects">main documentation</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_container">
|
||||
<td><a href="#" class="permalink">lazyload_container</a></td>
|
||||
<td><code>window</code></td>
|
||||
<td>
|
||||
You can also use plugin for images inside scrolling container, such as div with scrollbar.
|
||||
<div class="collapsible">
|
||||
<p>Just pass the container as jQuery object.</p>
|
||||
For more details, see the <a class="external" href="http://www.appelsiini.net/projects/lazyload#images-inside-container">main documentation</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_data_attribute">
|
||||
<td><a href="#" class="permalink">lazyload_data_attribute</a></td>
|
||||
<td><code>'original'</code></td>
|
||||
<td>
|
||||
You must alter your image tags. Address of the image must be put into <code>data-original</code> attribute.
|
||||
<div class="collapsible">
|
||||
<p>Use this option to set a different attribute.</p>
|
||||
If you have a higher quality image that you want to load under certain conditions, use the <code>lazyload_appear</code> callback to change this setting. Here is an example that switches to load retina images:
|
||||
<pre class="prettyprint lang-js">$(function() {
|
||||
$( 'table' ).tablesorter({
|
||||
theme : 'blue',
|
||||
widgets : [ 'lazyload' ],
|
||||
widgetOptions : {
|
||||
lazyload_appear : function( elements_left, settings ) {
|
||||
// elements_left = number of images left to load
|
||||
// settings = lazyload plugin settings; not the widgetOptions setting which don't matter at this point
|
||||
settings.data_attribute = window.devicePixelRatio > 1 && $( this ).attr( 'retina' ) ?
|
||||
// use "data-retina" for higher resolution images
|
||||
'retina' :
|
||||
// fallback to "data-original" for non-retina displays
|
||||
'original';
|
||||
}
|
||||
}
|
||||
});
|
||||
});</pre>
|
||||
For more details, see the <a class="external" href="http://www.appelsiini.net/projects/lazyload#how-to-use">main documentation</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_skip_invisible">
|
||||
<td><a href="#" class="permalink">lazyload_skip_invisible</a></td>
|
||||
<td><code>false</code></td>
|
||||
<td>
|
||||
There are cases when you have images which are in viewport but not <code>:visible</code>.
|
||||
<div class="collapsible">
|
||||
<p>To improve performance you can ignore <code>.not(":visible")</code> images by setting this option to <code>true</code>.</p>
|
||||
For more details, see the <a class="external" href="http://www.appelsiini.net/projects/lazyload#dealing-with-invisible-images">main documentation</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_appear">
|
||||
<td><a href="#" class="permalink">lazyload_appear</a></td>
|
||||
<td><code>null</code></td>
|
||||
<td>
|
||||
This function is called when image appears in the viewport, but before it is loaded.
|
||||
<div class="collapsible">
|
||||
<p>Use this callback function as follows:</p>
|
||||
<pre class="prettyprint lang-js">$(function() {
|
||||
$( 'table' ).tablesorter({
|
||||
theme : 'blue',
|
||||
widgets : [ 'lazyload' ],
|
||||
widgetOptions : {
|
||||
lazyload_appear : function( elements_left, settings ) {
|
||||
// elements_left = number of images left to load
|
||||
// settings = plugin settings; not the widgetOptions
|
||||
$( this ).parent().addClass( 'loading' );
|
||||
}
|
||||
}
|
||||
});
|
||||
});</pre>
|
||||
There doesn't appear to be any documentation for this callback in the main documentation.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_load">
|
||||
<td><a href="#" class="permalink">lazyload_load</a></td>
|
||||
<td><code>null</code></td>
|
||||
<td>
|
||||
This function is called after the image has completed loading.
|
||||
<div class="collapsible">
|
||||
<p>Use this callback function as follows:</p>
|
||||
<pre class="prettyprint lang-js">$(function() {
|
||||
$( 'table' ).tablesorter({
|
||||
theme : 'blue',
|
||||
widgets : [ 'lazyload' ],
|
||||
widgetOptions : {
|
||||
lazyload_load : function( elements_left, settings ) {
|
||||
// elements_left = number of images left to load
|
||||
// settings = plugin settings; not the widgetOptions
|
||||
$( this ).parent().removeClass( 'loading' );
|
||||
}
|
||||
}
|
||||
});
|
||||
});</pre>
|
||||
There doesn't appear to be any documentation for this callback in the main documentation.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="lazyload_placeholder">
|
||||
<td><a href="#" class="permalink">lazyload_placeholder</a></td>
|
||||
<td>See description</td>
|
||||
<td>
|
||||
This placeholder replaces the image until it gets loaded.
|
||||
<div class="collapsible">
|
||||
<p>By default, this setting is an encoded gif of a 1x1 transparent pixel.</p>
|
||||
<p>Default: <code>'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='</code></p>
|
||||
For more details, see the <a class="external" href="http://www.appelsiini.net/projects/lazyload#fallback-for-non-javascript-browsers">main documentation</a>.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<h3><a href="#">Method</a></h3>
|
||||
<div>
|
||||
<h3>Force lazyload widget update</h3>
|
||||
If you need to programmically force the lazyload widget to update, trigger a <code>'lazyloadUpdate'</code> event on the table:
|
||||
<pre class="prettyprint lang-js">$( 'table' ).trigger( 'lazyloadUpdate' );</pre>
|
||||
You can change this event name by setting the <code>lazyload_update</code> option.
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<p></p>
|
||||
|
||||
<h1>Demo</h1>
|
||||
<div id="demo"><table class="tablesorter">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>First Name</th>
|
||||
<th>Last Name</th>
|
||||
<th>Image</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>Bill</td><td>Murray</td><td><img class="lazy" data-original="http://www.fillmurray.com/300/500" width="300" height="500" alt="Murray" /></td></tr>
|
||||
<tr><td>Joyce</td><td>Young</td><td><img class="lazy" data-original="http://placehold.it/300x500/ff0000/ffffff" width="300" height="500" alt="Red" /></td></tr>
|
||||
<tr><td>John</td><td>Hood</td><td><img class="lazy" data-original="http://placehold.it/300x500" width="300" height="500" alt="Grey" /></td></tr>
|
||||
<tr><td>Clark</td><td>Kent</td><td><img class="lazy" data-original="http://placebear.com/300/500" width="300" height="500" alt="Bear" /></td></tr>
|
||||
<tr><td>Beverly</td><td>Pyle</td><td><img class="lazy" data-original="http://placehold.it/300x500/ff00ff/ffffff" width="300" height="500" alt="Blue" /></td></tr>
|
||||
<tr><td>Bruce</td><td>Almighty</td><td><img class="lazy" data-original="http://lorempixel.com/300/500" width="300" height="500" alt="Random" /></td></tr>
|
||||
<tr><td>Billy</td><td>Murray</td><td><img class="lazy" data-original="http://www.fillmurray.com/300/500" width="300" height="500" alt="Murray" /></td></tr>
|
||||
<tr><td>Frank</td><td>Bedlam</td><td><img class="lazy" data-original="http://placehold.it/300x500/0000ff/ffffff" width="300" height="500" alt="Blue" /></td></tr>
|
||||
<tr><td>Harry</td><td>Sally</td><td><img class="lazy" data-original="http://placebear.com/300/500" width="300" height="500" alt="Bear" /></td></tr>
|
||||
</tbody>
|
||||
</table></div>
|
||||
|
||||
<h1>Javascript</h1>
|
||||
<div id="javascript">
|
||||
<pre class="prettyprint lang-javascript"></pre>
|
||||
</div>
|
||||
<h1>HTML</h1>
|
||||
<div id="html">
|
||||
<pre class="prettyprint lang-html"></pre>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -478,6 +478,7 @@
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="example-widget-header-titles.html">Header titles widget</a> (v2.15.6; <span class="version updated">2.15.7</span>)</li>
|
||||
<li><a href="example-widget-lazyload.html">Lazyload widget</a> (<span class="version">v2.23.6</span>).</li>
|
||||
<li><a href="example-widget-math.html">Math widget</a> (<span class="version">v2.16</span>; <span class="version updated">v2.23.6</span>).</li>
|
||||
<li>
|
||||
<a href="example-widget-output.html">Output widget</a> (<span class="version">v2.16</span>; <span class="version updated">v2.22.2</span>)
|
||||
|
367
js/widgets/widget-lazyload.js
Normal file
367
js/widgets/widget-lazyload.js
Normal file
@ -0,0 +1,367 @@
|
||||
/*! Widget: lazyload - 10/25/2015 (v2.23.6) *//*
|
||||
* Requires tablesorter v2.8+ and jQuery 1.7+
|
||||
* by Rob Garrison
|
||||
*/
|
||||
/*jshint browser:true, jquery:true, unused:false */
|
||||
;( function( $, window ) {
|
||||
'use strict';
|
||||
var ts = $.tablesorter;
|
||||
|
||||
ts.lazyload = {
|
||||
init : function( c, wo ) {
|
||||
if ( wo.lazyload_event === 'scrollstop' && !ts.addScrollStopDone ) {
|
||||
ts.addScrollStop();
|
||||
ts.addScrollStopDone = true;
|
||||
$.event.special.scrollstop.latency = wo.lazyload_latency || 250;
|
||||
}
|
||||
ts.lazyload.update( c, wo );
|
||||
var events = [ wo.lazyload_update, 'pagerUpdate', wo.columnSelector_updated || 'columnUpdate', '' ]
|
||||
.join( c.namespace + 'lazyload ' );
|
||||
c.$table.on( events, function() {
|
||||
ts.lazyload.update( c, c.widgetOptions );
|
||||
});
|
||||
},
|
||||
update : function( c, wo ) {
|
||||
// add '.' if not already included
|
||||
var sel = ( /(\.|#)/.test( wo.lazyload_imageClass ) ? '' : '.' ) + wo.lazyload_imageClass;
|
||||
c.$table.find( sel ).lazyload({
|
||||
threshold : wo.lazyload_threshold,
|
||||
failure_limit : wo.lazyload_failure_limit,
|
||||
event : wo.lazyload_event,
|
||||
effect : wo.lazyload_effect,
|
||||
container : wo.lazyload_container,
|
||||
data_attribute : wo.lazyload_data_attribute,
|
||||
skip_invisible : wo.lazyload_skip_invisible,
|
||||
appear : wo.lazyload_appear,
|
||||
load : wo.lazyload_load,
|
||||
placeholder : wo.lazyload_placeholder
|
||||
});
|
||||
},
|
||||
remove : function( c, wo ) {
|
||||
c.$table.off( c.namespace + 'lazyload' );
|
||||
}
|
||||
};
|
||||
|
||||
ts.addWidget({
|
||||
id: 'lazyload',
|
||||
options: {
|
||||
// widget options
|
||||
lazyload_imageClass : 'lazy',
|
||||
lazyload_update : 'lazyloadUpdate',
|
||||
// scrollStop option (https://github.com/ssorallen/jquery-scrollstop)
|
||||
lazyload_latency : 250,
|
||||
// lazyload options (see http://www.appelsiini.net/projects/lazyload)
|
||||
lazyload_threshold : 0,
|
||||
lazyload_failure_limit : 0,
|
||||
lazyload_event : 'scrollstop',
|
||||
lazyload_effect : 'show',
|
||||
lazyload_container : window,
|
||||
lazyload_data_attribute : 'original',
|
||||
lazyload_skip_invisible : false,
|
||||
lazyload_appear : null,
|
||||
lazyload_load : null,
|
||||
lazyload_placeholder : 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
|
||||
},
|
||||
init: function( table, thisWidget, c, wo ) {
|
||||
ts.lazyload.init( c, wo );
|
||||
},
|
||||
remove : function( table, c, wo ) {
|
||||
ts.lazyload.remove( c, wo );
|
||||
}
|
||||
});
|
||||
|
||||
// jscs:disable
|
||||
ts.addScrollStop = function() {
|
||||
// jQuery Scrollstop Plugin v1.2.0
|
||||
// https://github.com/ssorallen/jquery-scrollstop
|
||||
/*
|
||||
(function (factory) {
|
||||
// UMD[2] wrapper for jQuery plugins to work in AMD or in CommonJS.
|
||||
//
|
||||
// [2] https://github.com/umdjs/umd
|
||||
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
// AMD. Register as an anonymous module.
|
||||
define(['jquery'], factory);
|
||||
} else if (typeof exports === 'object') {
|
||||
// Node/CommonJS
|
||||
module.exports = factory(require('jquery'));
|
||||
} else {
|
||||
// Browser globals
|
||||
factory(jQuery);
|
||||
}
|
||||
}(function ($) { */
|
||||
// $.event.dispatch was undocumented and was deprecated in jQuery 1.7[1]. It
|
||||
// was replaced by $.event.handle in jQuery 1.9.
|
||||
//
|
||||
// Use the first of the available functions to support jQuery <1.8.
|
||||
//
|
||||
// [1] https://github.com/jquery/jquery-migrate/blob/master/src/event.js#L25
|
||||
var dispatch = $.event.dispatch || $.event.handle;
|
||||
|
||||
var special = $.event.special,
|
||||
uid1 = 'D' + (+new Date()),
|
||||
uid2 = 'D' + (+new Date() + 1);
|
||||
|
||||
special.scrollstart = {
|
||||
setup: function(data) {
|
||||
var _data = $.extend({
|
||||
latency: special.scrollstop.latency
|
||||
}, data);
|
||||
|
||||
var timer,
|
||||
handler = function(evt) {
|
||||
var _self = this,
|
||||
_args = arguments;
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
} else {
|
||||
evt.type = 'scrollstart';
|
||||
dispatch.apply(_self, _args);
|
||||
}
|
||||
timer = setTimeout(function() {
|
||||
timer = null;
|
||||
}, _data.latency);
|
||||
};
|
||||
$(this).bind('scroll', handler).data(uid1, handler);
|
||||
},
|
||||
teardown: function() {
|
||||
$(this).unbind('scroll', $(this).data(uid1));
|
||||
}
|
||||
};
|
||||
special.scrollstop = {
|
||||
latency: 250,
|
||||
setup: function(data) {
|
||||
var _data = $.extend({
|
||||
latency: special.scrollstop.latency
|
||||
}, data);
|
||||
var timer,
|
||||
handler = function(evt) {
|
||||
var _self = this,
|
||||
_args = arguments;
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(function() {
|
||||
timer = null;
|
||||
evt.type = 'scrollstop';
|
||||
dispatch.apply(_self, _args);
|
||||
}, _data.latency);
|
||||
};
|
||||
$(this).bind('scroll', handler).data(uid2, handler);
|
||||
},
|
||||
teardown: function() {
|
||||
$(this).unbind('scroll', $(this).data(uid2));
|
||||
}
|
||||
};
|
||||
/*
|
||||
}));
|
||||
*/
|
||||
|
||||
};
|
||||
// jscs:enable
|
||||
|
||||
})( jQuery, window );
|
||||
|
||||
// jscs:disable
|
||||
/*!
|
||||
* Lazy Load - jQuery plugin for lazy loading images
|
||||
*
|
||||
* Copyright (c) 2007-2015 Mika Tuupola
|
||||
*
|
||||
* Licensed under the MIT license:
|
||||
* http://www.opensource.org/licenses/mit-license.php
|
||||
*
|
||||
* Project home:
|
||||
* http://www.appelsiini.net/projects/lazyload
|
||||
*
|
||||
* Version: 1.9.7
|
||||
*
|
||||
*/
|
||||
(function($, window, document, undefined) {
|
||||
var $window = $(window);
|
||||
$.fn.lazyload = function(options) {
|
||||
var elements = this;
|
||||
var $container;
|
||||
var settings = {
|
||||
threshold : 0,
|
||||
failure_limit : 0,
|
||||
event : "scroll",
|
||||
effect : "show",
|
||||
container : window,
|
||||
data_attribute : "original",
|
||||
skip_invisible : false,
|
||||
appear : null,
|
||||
load : null,
|
||||
placeholder : "data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
|
||||
};
|
||||
function update() {
|
||||
var counter = 0;
|
||||
elements.each(function() {
|
||||
var $this = $(this);
|
||||
if (settings.skip_invisible && !$this.is(":visible")) {
|
||||
return;
|
||||
}
|
||||
if ($.abovethetop(this, settings) || $.leftofbegin(this, settings)) {
|
||||
/* Nothing. */
|
||||
} else if (!$.belowthefold(this, settings) && !$.rightoffold(this, settings)) {
|
||||
$this.trigger("appear");
|
||||
/* if we found an image we'll load, reset the counter */
|
||||
counter = 0;
|
||||
} else {
|
||||
if (++counter > settings.failure_limit) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (options) {
|
||||
/* Maintain BC for a couple of versions. */
|
||||
if (undefined !== options.failurelimit) {
|
||||
options.failure_limit = options.failurelimit;
|
||||
delete options.failurelimit;
|
||||
}
|
||||
if (undefined !== options.effectspeed) {
|
||||
options.effect_speed = options.effectspeed;
|
||||
delete options.effectspeed;
|
||||
}
|
||||
$.extend(settings, options);
|
||||
}
|
||||
/* Cache container as jQuery as object. */
|
||||
$container = (settings.container === undefined ||
|
||||
settings.container === window) ? $window : $(settings.container);
|
||||
/* Fire one scroll event per scroll. Not one scroll event per image. */
|
||||
if (0 === settings.event.indexOf("scroll")) {
|
||||
$container.bind(settings.event, function() {
|
||||
return update();
|
||||
});
|
||||
}
|
||||
this.each(function() {
|
||||
var self = this;
|
||||
var $self = $(self);
|
||||
self.loaded = false;
|
||||
/* If no src attribute given use data:uri. */
|
||||
if ($self.attr("src") === undefined || $self.attr("src") === false) {
|
||||
if ($self.is("img")) {
|
||||
$self.attr("src", settings.placeholder);
|
||||
}
|
||||
}
|
||||
/* When appear is triggered load original image. */
|
||||
$self.one("appear", function() {
|
||||
if (!this.loaded) {
|
||||
if (settings.appear) {
|
||||
var elements_left = elements.length;
|
||||
settings.appear.call(self, elements_left, settings);
|
||||
}
|
||||
$("<img />")
|
||||
.bind("load", function() {
|
||||
var original = $self.attr("data-" + settings.data_attribute);
|
||||
$self.hide();
|
||||
if ($self.is("img")) {
|
||||
$self.attr("src", original);
|
||||
} else {
|
||||
$self.css("background-image", "url('" + original + "')");
|
||||
}
|
||||
$self[settings.effect](settings.effect_speed);
|
||||
self.loaded = true;
|
||||
/* Remove image from array so it is not looped next time. */
|
||||
var temp = $.grep(elements, function(element) {
|
||||
return !element.loaded;
|
||||
});
|
||||
elements = $(temp);
|
||||
if (settings.load) {
|
||||
var elements_left = elements.length;
|
||||
settings.load.call(self, elements_left, settings);
|
||||
}
|
||||
})
|
||||
.attr("src", $self.attr("data-" + settings.data_attribute));
|
||||
}
|
||||
});
|
||||
/* When wanted event is triggered load original image */
|
||||
/* by triggering appear. */
|
||||
if (0 !== settings.event.indexOf("scroll")) {
|
||||
$self.bind(settings.event, function() {
|
||||
if (!self.loaded) {
|
||||
$self.trigger("appear");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
/* Check if something appears when window is resized. */
|
||||
$window.bind("resize", function() {
|
||||
update();
|
||||
});
|
||||
/* With IOS5 force loading images when navigating with back button. */
|
||||
/* Non optimal workaround. */
|
||||
if ((/(?:iphone|ipod|ipad).*os 5/gi).test(navigator.appVersion)) {
|
||||
$window.bind("pageshow", function(event) {
|
||||
if (event.originalEvent && event.originalEvent.persisted) {
|
||||
elements.each(function() {
|
||||
$(this).trigger("appear");
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
/* Force initial check if images should appear. */
|
||||
$(document).ready(function() {
|
||||
update();
|
||||
});
|
||||
return this;
|
||||
};
|
||||
/* Convenience methods in jQuery namespace. */
|
||||
/* Use as $.belowthefold(element, {threshold : 100, container : window}) */
|
||||
$.belowthefold = function(element, settings) {
|
||||
var fold;
|
||||
if (settings.container === undefined || settings.container === window) {
|
||||
fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop();
|
||||
} else {
|
||||
fold = $(settings.container).offset().top + $(settings.container).height();
|
||||
}
|
||||
return fold <= $(element).offset().top - settings.threshold;
|
||||
};
|
||||
$.rightoffold = function(element, settings) {
|
||||
var fold;
|
||||
if (settings.container === undefined || settings.container === window) {
|
||||
fold = $window.width() + $window.scrollLeft();
|
||||
} else {
|
||||
fold = $(settings.container).offset().left + $(settings.container).width();
|
||||
}
|
||||
return fold <= $(element).offset().left - settings.threshold;
|
||||
};
|
||||
$.abovethetop = function(element, settings) {
|
||||
var fold;
|
||||
if (settings.container === undefined || settings.container === window) {
|
||||
fold = $window.scrollTop();
|
||||
} else {
|
||||
fold = $(settings.container).offset().top;
|
||||
}
|
||||
return fold >= $(element).offset().top + settings.threshold + $(element).height();
|
||||
};
|
||||
$.leftofbegin = function(element, settings) {
|
||||
var fold;
|
||||
if (settings.container === undefined || settings.container === window) {
|
||||
fold = $window.scrollLeft();
|
||||
} else {
|
||||
fold = $(settings.container).offset().left;
|
||||
}
|
||||
return fold >= $(element).offset().left + settings.threshold + $(element).width();
|
||||
};
|
||||
$.inviewport = function(element, settings) {
|
||||
return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) &&
|
||||
!$.belowthefold(element, settings) && !$.abovethetop(element, settings);
|
||||
};
|
||||
/* Custom selectors for your convenience. */
|
||||
/* Use as $("img:below-the-fold").something() or */
|
||||
/* $("img").filter(":below-the-fold").something() which is faster */
|
||||
$.extend($.expr[":"], {
|
||||
"below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },
|
||||
"above-the-top" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
|
||||
"right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },
|
||||
"left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },
|
||||
"in-viewport" : function(a) { return $.inviewport(a, {threshold : 0}); },
|
||||
/* Maintain BC for couple of versions. */
|
||||
"above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
|
||||
"right-of-fold" : function(a) { return $.rightoffold(a, {threshold : 0}); },
|
||||
"left-of-fold" : function(a) { return !$.rightoffold(a, {threshold : 0}); }
|
||||
});
|
||||
})(jQuery, window, document);
|
Loading…
Reference in New Issue
Block a user