Skip to content

Commit 11ebed9

Browse files
committed
1.7.6
- fixed bug where items where not correctly filtered in _prepareItems - added support for imageBase config to picture plugin - updated jsdeliver links to github format - updated dev dependencies - some code style update in edited files
1 parent b9a600d commit 11ebed9

11 files changed

+182
-82
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ Some examples below:
6565
Lazy and all plugins are available over [cdnjs](http://cdnjs.com) and [jsDelivr](http://jsdelivr.com) CDN and can directly included to every page.
6666
```HTML
6767
<!-- jsDeliver -->
68-
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazy/1.7.5/jquery.lazy.min.js"></script>
69-
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazy/1.7.5/jquery.lazy.plugins.min.js"></script>
68+
<script type="text/javascript" src="//cdn.jsdelivr.net/gh/eisbehr-/jquery.lazy@1.7.6/jquery.lazy.min.js"></script>
69+
<script type="text/javascript" src="//cdn.jsdelivr.net/gh/eisbehr-/jquery.lazy@1.7.6/jquery.lazy.plugins.min.js"></script>
7070

7171
<!-- cdnjs -->
72-
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.5/jquery.lazy.min.js"></script>
73-
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.5/jquery.lazy.plugins.min.js"></script>
72+
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.6/jquery.lazy.min.js"></script>
73+
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.6/jquery.lazy.plugins.min.js"></script>
7474
```
7575

7676
#### Self-Hosted

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jquery-lazy",
33
"description": "Lazy is a fast, feature-rich and lightweight delayed content loading plugin for jQuery and Zepto. It's designed to speed up page loading times and decrease traffic to your users by only loading the content in view. You can use Lazy in all vertical and horizontal scroll ways. It supports images in 'img' tags and backgrounds, supplied with css like 'background-image', by default. On those elements Lazy can set an default image or a placeholder while loading and supports retina displays as well. But Lazy is even able to load any other content you want by plugins and custom loaders.",
4-
"version": "1.7.5",
4+
"version": "1.7.6",
55
"main": "jquery.lazy.min.js",
66
"license": [
77
"MIT",

jquery.lazy.js

+71-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* jQuery & Zepto Lazy - v1.7.5
2+
* jQuery & Zepto Lazy - v1.7.6
33
* http://jquery.eisbehr.de/lazy/
44
*
55
* Copyright 2012 - 2017, Daniel 'Eisbehr' Kern
@@ -61,7 +61,9 @@
6161
}
6262

6363
// exit here if parameter is not a callable function
64-
if( !$.isFunction(loader) ) return;
64+
if( !$.isFunction(loader) ) {
65+
return;
66+
}
6567

6668
// make parameters an array of names to be sure
6769
names = $.isArray(names) ? names : [names];
@@ -71,13 +73,16 @@
7173
forced = config._f || (config._f = {});
7274

7375
// add the loader plugin for every name
74-
for( var i = 0, l = names.length; i < l; i++ )
75-
if( config[names[i]] === undefined || $.isFunction(config[names[i]]) )
76+
for( var i = 0, l = names.length; i < l; i++ ) {
77+
if( config[names[i]] === undefined || $.isFunction(config[names[i]]) ) {
7678
config[names[i]] = loader;
79+
}
80+
}
7781

7882
// add forced elements loader
79-
for( var c = 0, a = elements.length; c < a; c++ )
83+
for( var c = 0, a = elements.length; c < a; c++ ) {
8084
forced[elements[c]] = names[0];
85+
}
8186
};
8287

8388
/**
@@ -189,26 +194,31 @@
189194
_isRetinaDisplay = window.devicePixelRatio > 1;
190195

191196
// prepare all initial items
192-
_prepareItems(items);
197+
items = _prepareItems(items);
193198

194199
// if delay time is set load all items at once after delay time
195-
if( config.delay >= 0 ) setTimeout(function() { _lazyLoadItems(true); }, config.delay);
200+
if( config.delay >= 0 ) {
201+
setTimeout(function() {
202+
_lazyLoadItems(true);
203+
}, config.delay);
204+
}
196205

197206
// if no delay is set or combine usage is active bind events
198207
if( config.delay < 0 || config.combined ) {
199208
// create unique event function
200209
events.e = _throttle(config.throttle, function(event) {
201210
// reset detected window size on resize event
202-
if( event.type === "resize" )
211+
if( event.type === "resize" ) {
203212
_actualWidth = _actualHeight = -1;
213+
}
204214

205215
// execute 'lazy magic'
206216
_lazyLoadItems(event.all);
207217
});
208218

209219
// create function to add new items to instance
210220
events.a = function(additionalItems) {
211-
_prepareItems(additionalItems);
221+
additionalItems = _prepareItems(additionalItems);
212222
items.push.apply(items, additionalItems);
213223
};
214224

@@ -250,7 +260,7 @@
250260
* prepare items before handle them
251261
* @access private
252262
* @param {Array|object|jQuery} items
253-
* @return void
263+
* @return {Array|object|jQuery}
254264
*/
255265
function _prepareItems(items) {
256266
// fetch used configurations before loops
@@ -279,21 +289,27 @@
279289
elementImageBase = element.attr(config.imageBaseAttribute) || imageBase;
280290

281291
// generate and update source set if an image base is set
282-
if( tag === _img && elementImageBase && element.attr(srcsetAttribute) )
292+
if( tag === _img && elementImageBase && element.attr(srcsetAttribute) ) {
283293
element.attr(srcsetAttribute, _getCorrectedSrcSet(element.attr(srcsetAttribute), elementImageBase));
294+
}
284295

285296
// add loader to forced element types
286-
if( forcedTags[tag] !== undefined && !element.attr(loaderAttribute) )
297+
if( forcedTags[tag] !== undefined && !element.attr(loaderAttribute) ) {
287298
element.attr(loaderAttribute, forcedTags[tag]);
299+
}
288300

289301
// set default image on every element without source
290-
if( tag === _img && defaultImage && !element.attr(_src) )
302+
if( tag === _img && defaultImage && !element.attr(_src) ) {
291303
element.attr(_src, defaultImage);
304+
}
292305

293306
// set placeholder on every element without background image
294-
else if( tag !== _img && placeholder && (!element.css(_backgroundImage) || element.css(_backgroundImage) === "none") )
307+
else if( tag !== _img && placeholder && (!element.css(_backgroundImage) || element.css(_backgroundImage) === "none") ) {
295308
element.css(_backgroundImage, "url('" + placeholder + "')");
309+
}
296310
}
311+
312+
return items;
297313
}
298314

299315
/**
@@ -307,9 +323,10 @@
307323
// skip if no items where left
308324
if( !items.length ) {
309325
// destroy instance if option is enabled
310-
if( config.autoDestroy )
326+
if( config.autoDestroy ) {
311327
// noinspection JSUnresolvedFunction
312328
instance.destroy();
329+
}
313330

314331
return;
315332
}
@@ -339,7 +356,7 @@
339356
// and is image tag where attribute is not equal source or source set
340357
(tag === _img && (elementImageBase + attribute !== element.attr(_src) || element.attr(srcsetAttribute) !== element.attr(_srcset))) ||
341358
// or is non image tag where attribute is not equal background
342-
(tag !== _img && elementImageBase + attribute !== element.css(_backgroundImage))
359+
(tag !== _img && elementImageBase + attribute !== element.css(_backgroundImage))
343360
) ||
344361
// or custom loader is available
345362
customLoader ))
@@ -355,10 +372,11 @@
355372
}
356373

357374
// when something was loaded remove them from remaining items
358-
if( loadTriggered )
375+
if( loadTriggered ) {
359376
items = $(items).filter(function() {
360377
return !$(this).data(handledName);
361378
});
379+
}
362380
}
363381

364382
/**
@@ -400,8 +418,9 @@
400418
// on load callback
401419
var loadCallback = function() {
402420
// remove attribute from element
403-
if( removeAttribute )
421+
if( removeAttribute ) {
404422
element.removeAttr(config.loaderAttribute);
423+
}
405424

406425
// mark element as loaded
407426
element.data(loadedName, true);
@@ -451,12 +470,14 @@
451470

452471
// set image back to element
453472
// do it as single 'attr' calls, to be sure 'src' is set after 'srcset'
454-
if( tag === _img )
473+
if( tag === _img ) {
455474
element.attr(_sizes, imageObj.attr(_sizes))
456475
.attr(_srcset, imageObj.attr(_srcset))
457476
.attr(_src, imageObj.attr(_src));
458-
else
477+
}
478+
else {
459479
element.css(_backgroundImage, "url('" + imageObj.attr(_src) + "')");
480+
}
460481

461482
// bring it back with some effect!
462483
element[config.effect](config.effectTime);
@@ -466,8 +487,9 @@
466487
element.removeAttr(srcAttribute + " " + srcsetAttribute + " " + retinaAttribute + " " + config.imageBaseAttribute);
467488

468489
// only remove 'sizes' attribute, if it was a custom one
469-
if( sizesAttribute !== _sizes )
490+
if( sizesAttribute !== _sizes ) {
470491
element.removeAttr(sizesAttribute);
492+
}
471493
}
472494

473495
// mark element as loaded
@@ -514,8 +536,12 @@
514536
// check if element is even in loadable area from right
515537
(-threshold < elementBound.right);
516538

517-
if( direction === "vertical" ) return vertical;
518-
else if( direction === "horizontal" ) return horizontal;
539+
if( direction === "vertical" ) {
540+
return vertical;
541+
}
542+
else if( direction === "horizontal" ) {
543+
return horizontal;
544+
}
519545

520546
return vertical && horizontal;
521547
}
@@ -561,8 +587,9 @@
561587
var entries = srcset.split(",");
562588
srcset = "";
563589

564-
for( var i = 0, l = entries.length; i < l; i++ )
590+
for( var i = 0, l = entries.length; i < l; i++ ) {
565591
srcset += imageBase + entries[i].trim() + (i !== l - 1 ? "," : "");
592+
}
566593
}
567594

568595
return srcset;
@@ -584,13 +611,18 @@
584611

585612
function run() {
586613
lastExecute = +new Date();
614+
// noinspection JSUnresolvedFunction
587615
callback.call(instance, event);
588616
}
589617

590618
timeout && clearTimeout(timeout); // jshint ignore : line
591619

592-
if( elapsed > delay || !config.enableThrottle || ignoreThrottle ) run();
593-
else timeout = setTimeout(run, delay - elapsed);
620+
if( elapsed > delay || !config.enableThrottle || ignoreThrottle ) {
621+
run();
622+
}
623+
else {
624+
timeout = setTimeout(run, delay - elapsed);
625+
}
594626
};
595627
}
596628

@@ -603,7 +635,9 @@
603635
--_awaitingAfterLoad;
604636

605637
// if no items were left trigger finished event
606-
if( !items.length && !_awaitingAfterLoad ) _triggerCallback("onFinishedAll");
638+
if( !items.length && !_awaitingAfterLoad ) {
639+
_triggerCallback("onFinishedAll");
640+
}
607641
}
608642

609643
/**
@@ -626,12 +660,15 @@
626660
}
627661

628662
// if event driven or window is already loaded don't wait for page loading
629-
if( config.bind === "event" || windowLoaded )
663+
if( config.bind === "event" || windowLoaded ) {
630664
_initialize();
665+
}
631666

632667
// otherwise load initial items and start lazy after page load
633-
else // noinspection JSUnresolvedVariable
668+
else {
669+
// noinspection JSUnresolvedVariable
634670
$(window).on(_load + "." + namespace, _initialize);
671+
}
635672
}
636673

637674
/**
@@ -683,8 +720,9 @@
683720
* @return {LazyPlugin|*}
684721
*/
685722
_instance.config = function(entryName, value) {
686-
if( value === undefined )
723+
if( value === undefined ) {
687724
return _config[entryName];
725+
}
688726

689727
_config[entryName] = value;
690728
return _instance;
@@ -826,5 +864,7 @@
826864

827865
// register window load event globally to prevent not loading elements
828866
// since jQuery 3.X ready state is fully async and may be executed after 'load'
829-
$(window).on("load", function() { windowLoaded = true; });
867+
$(window).on("load", function() {
868+
windowLoaded = true;
869+
});
830870
})(window);

0 commit comments

Comments
 (0)