1
1
/*!
2
- * jQuery & Zepto Lazy - v1.7.5
2
+ * jQuery & Zepto Lazy - v1.7.6
3
3
* http://jquery.eisbehr.de/lazy/
4
4
*
5
5
* Copyright 2012 - 2017, Daniel 'Eisbehr' Kern
61
61
}
62
62
63
63
// exit here if parameter is not a callable function
64
- if ( ! $ . isFunction ( loader ) ) return ;
64
+ if ( ! $ . isFunction ( loader ) ) {
65
+ return ;
66
+ }
65
67
66
68
// make parameters an array of names to be sure
67
69
names = $ . isArray ( names ) ? names : [ names ] ;
71
73
forced = config . _f || ( config . _f = { } ) ;
72
74
73
75
// 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 ] ] ) ) {
76
78
config [ names [ i ] ] = loader ;
79
+ }
80
+ }
77
81
78
82
// 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 ++ ) {
80
84
forced [ elements [ c ] ] = names [ 0 ] ;
85
+ }
81
86
} ;
82
87
83
88
/**
189
194
_isRetinaDisplay = window . devicePixelRatio > 1 ;
190
195
191
196
// prepare all initial items
192
- _prepareItems ( items ) ;
197
+ items = _prepareItems ( items ) ;
193
198
194
199
// 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
+ }
196
205
197
206
// if no delay is set or combine usage is active bind events
198
207
if ( config . delay < 0 || config . combined ) {
199
208
// create unique event function
200
209
events . e = _throttle ( config . throttle , function ( event ) {
201
210
// reset detected window size on resize event
202
- if ( event . type === "resize" )
211
+ if ( event . type === "resize" ) {
203
212
_actualWidth = _actualHeight = - 1 ;
213
+ }
204
214
205
215
// execute 'lazy magic'
206
216
_lazyLoadItems ( event . all ) ;
207
217
} ) ;
208
218
209
219
// create function to add new items to instance
210
220
events . a = function ( additionalItems ) {
211
- _prepareItems ( additionalItems ) ;
221
+ additionalItems = _prepareItems ( additionalItems ) ;
212
222
items . push . apply ( items , additionalItems ) ;
213
223
} ;
214
224
250
260
* prepare items before handle them
251
261
* @access private
252
262
* @param {Array|object|jQuery } items
253
- * @return void
263
+ * @return { Array|object|jQuery }
254
264
*/
255
265
function _prepareItems ( items ) {
256
266
// fetch used configurations before loops
279
289
elementImageBase = element . attr ( config . imageBaseAttribute ) || imageBase ;
280
290
281
291
// 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 ) ) {
283
293
element . attr ( srcsetAttribute , _getCorrectedSrcSet ( element . attr ( srcsetAttribute ) , elementImageBase ) ) ;
294
+ }
284
295
285
296
// add loader to forced element types
286
- if ( forcedTags [ tag ] !== undefined && ! element . attr ( loaderAttribute ) )
297
+ if ( forcedTags [ tag ] !== undefined && ! element . attr ( loaderAttribute ) ) {
287
298
element . attr ( loaderAttribute , forcedTags [ tag ] ) ;
299
+ }
288
300
289
301
// set default image on every element without source
290
- if ( tag === _img && defaultImage && ! element . attr ( _src ) )
302
+ if ( tag === _img && defaultImage && ! element . attr ( _src ) ) {
291
303
element . attr ( _src , defaultImage ) ;
304
+ }
292
305
293
306
// 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" ) ) {
295
308
element . css ( _backgroundImage , "url('" + placeholder + "')" ) ;
309
+ }
296
310
}
311
+
312
+ return items ;
297
313
}
298
314
299
315
/**
307
323
// skip if no items where left
308
324
if ( ! items . length ) {
309
325
// destroy instance if option is enabled
310
- if ( config . autoDestroy )
326
+ if ( config . autoDestroy ) {
311
327
// noinspection JSUnresolvedFunction
312
328
instance . destroy ( ) ;
329
+ }
313
330
314
331
return ;
315
332
}
339
356
// and is image tag where attribute is not equal source or source set
340
357
( tag === _img && ( elementImageBase + attribute !== element . attr ( _src ) || element . attr ( srcsetAttribute ) !== element . attr ( _srcset ) ) ) ||
341
358
// 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 ) )
343
360
) ||
344
361
// or custom loader is available
345
362
customLoader ) )
355
372
}
356
373
357
374
// when something was loaded remove them from remaining items
358
- if ( loadTriggered )
375
+ if ( loadTriggered ) {
359
376
items = $ ( items ) . filter ( function ( ) {
360
377
return ! $ ( this ) . data ( handledName ) ;
361
378
} ) ;
379
+ }
362
380
}
363
381
364
382
/**
400
418
// on load callback
401
419
var loadCallback = function ( ) {
402
420
// remove attribute from element
403
- if ( removeAttribute )
421
+ if ( removeAttribute ) {
404
422
element . removeAttr ( config . loaderAttribute ) ;
423
+ }
405
424
406
425
// mark element as loaded
407
426
element . data ( loadedName , true ) ;
451
470
452
471
// set image back to element
453
472
// do it as single 'attr' calls, to be sure 'src' is set after 'srcset'
454
- if ( tag === _img )
473
+ if ( tag === _img ) {
455
474
element . attr ( _sizes , imageObj . attr ( _sizes ) )
456
475
. attr ( _srcset , imageObj . attr ( _srcset ) )
457
476
. attr ( _src , imageObj . attr ( _src ) ) ;
458
- else
477
+ }
478
+ else {
459
479
element . css ( _backgroundImage , "url('" + imageObj . attr ( _src ) + "')" ) ;
480
+ }
460
481
461
482
// bring it back with some effect!
462
483
element [ config . effect ] ( config . effectTime ) ;
466
487
element . removeAttr ( srcAttribute + " " + srcsetAttribute + " " + retinaAttribute + " " + config . imageBaseAttribute ) ;
467
488
468
489
// only remove 'sizes' attribute, if it was a custom one
469
- if ( sizesAttribute !== _sizes )
490
+ if ( sizesAttribute !== _sizes ) {
470
491
element . removeAttr ( sizesAttribute ) ;
492
+ }
471
493
}
472
494
473
495
// mark element as loaded
514
536
// check if element is even in loadable area from right
515
537
( - threshold < elementBound . right ) ;
516
538
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
+ }
519
545
520
546
return vertical && horizontal ;
521
547
}
561
587
var entries = srcset . split ( "," ) ;
562
588
srcset = "" ;
563
589
564
- for ( var i = 0 , l = entries . length ; i < l ; i ++ )
590
+ for ( var i = 0 , l = entries . length ; i < l ; i ++ ) {
565
591
srcset += imageBase + entries [ i ] . trim ( ) + ( i !== l - 1 ? "," : "" ) ;
592
+ }
566
593
}
567
594
568
595
return srcset ;
584
611
585
612
function run ( ) {
586
613
lastExecute = + new Date ( ) ;
614
+ // noinspection JSUnresolvedFunction
587
615
callback . call ( instance , event ) ;
588
616
}
589
617
590
618
timeout && clearTimeout ( timeout ) ; // jshint ignore : line
591
619
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
+ }
594
626
} ;
595
627
}
596
628
603
635
-- _awaitingAfterLoad ;
604
636
605
637
// if no items were left trigger finished event
606
- if ( ! items . length && ! _awaitingAfterLoad ) _triggerCallback ( "onFinishedAll" ) ;
638
+ if ( ! items . length && ! _awaitingAfterLoad ) {
639
+ _triggerCallback ( "onFinishedAll" ) ;
640
+ }
607
641
}
608
642
609
643
/**
626
660
}
627
661
628
662
// 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 ) {
630
664
_initialize ( ) ;
665
+ }
631
666
632
667
// otherwise load initial items and start lazy after page load
633
- else // noinspection JSUnresolvedVariable
668
+ else {
669
+ // noinspection JSUnresolvedVariable
634
670
$ ( window ) . on ( _load + "." + namespace , _initialize ) ;
671
+ }
635
672
}
636
673
637
674
/**
683
720
* @return {LazyPlugin|* }
684
721
*/
685
722
_instance . config = function ( entryName , value ) {
686
- if ( value === undefined )
723
+ if ( value === undefined ) {
687
724
return _config [ entryName ] ;
725
+ }
688
726
689
727
_config [ entryName ] = value ;
690
728
return _instance ;
826
864
827
865
// register window load event globally to prevent not loading elements
828
866
// 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
+ } ) ;
830
870
} ) ( window ) ;
0 commit comments