summaryrefslogtreecommitdiffstats
path: root/js/lazyload.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lazyload.js')
-rw-r--r--js/lazyload.js45
1 files changed, 19 insertions, 26 deletions
diff --git a/js/lazyload.js b/js/lazyload.js
index e885cf7..f22357e 100644
--- a/js/lazyload.js
+++ b/js/lazyload.js
@@ -1,23 +1,19 @@
+// lazyload
( function( window, factory ) {
- 'use strict';
// universal module definition
-
+ /* jshint strict: false */
if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
- 'classie/classie',
- 'eventie/eventie',
'./flickity',
'fizzy-ui-utils/utils'
- ], function( classie, eventie, Flickity, utils ) {
- return factory( window, classie, eventie, Flickity, utils );
+ ], function( Flickity, utils ) {
+ return factory( window, Flickity, utils );
});
- } else if ( typeof exports == 'object' ) {
+ } else if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
window,
- require('desandro-classie'),
- require('eventie'),
require('./flickity'),
require('fizzy-ui-utils')
);
@@ -25,23 +21,22 @@
// browser global
factory(
window,
- window.classie,
- window.eventie,
window.Flickity,
window.fizzyUIUtils
);
}
-}( window, function factory( window, classie, eventie, Flickity, utils ) {
+}( window, function factory( window, Flickity, utils ) {
'use strict';
Flickity.createMethods.push('_createLazyload');
+var proto = Flickity.prototype;
-Flickity.prototype._createLazyload = function() {
- this.on( 'cellSelect', this.lazyLoad );
+proto._createLazyload = function() {
+ this.on( 'select', this.lazyLoad );
};
-Flickity.prototype.lazyLoad = function() {
+proto.lazyLoad = function() {
var lazyLoad = this.options.lazyLoad;
if ( !lazyLoad ) {
return;
@@ -51,16 +46,14 @@ Flickity.prototype.lazyLoad = function() {
var cellElems = this.getAdjacentCellElements( adjCount );
// get lazy images in those cells
var lazyImages = [];
- for ( var i=0, len = cellElems.length; i < len; i++ ) {
- var cellElem = cellElems[i];
+ cellElems.forEach( function( cellElem ) {
var lazyCellImages = getCellLazyImages( cellElem );
lazyImages = lazyImages.concat( lazyCellImages );
- }
+ });
// load lazy images
- for ( i=0, len = lazyImages.length; i < len; i++ ) {
- var img = lazyImages[i];
+ lazyImages.forEach( function( img ) {
new LazyLoader( img, this );
- }
+ }, this );
};
function getCellLazyImages( cellElem ) {
@@ -88,8 +81,8 @@ function LazyLoader( img, flickity ) {
LazyLoader.prototype.handleEvent = utils.handleEvent;
LazyLoader.prototype.load = function() {
- eventie.bind( this.img, 'load', this );
- eventie.bind( this.img, 'error', this );
+ this.img.addEventListener( 'load', this );
+ this.img.addEventListener( 'error', this );
// load image
this.img.src = this.img.getAttribute('data-flickity-lazyload');
// remove attr
@@ -106,14 +99,14 @@ LazyLoader.prototype.onerror = function( event ) {
LazyLoader.prototype.complete = function( event, className ) {
// unbind events
- eventie.unbind( this.img, 'load', this );
- eventie.unbind( this.img, 'error', this );
+ this.img.removeEventListener( 'load', this );
+ this.img.removeEventListener( 'error', this );
var cell = this.flickity.getParentCell( this.img );
var cellElem = cell && cell.element;
this.flickity.cellSizeChange( cellElem );
- classie.add( this.img, className );
+ this.img.classList.add( className );
this.flickity.dispatchEvent( 'lazyLoad', event, cellElem );
};