Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added destroy methods #227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 79 additions & 43 deletions jquery.lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,31 @@
*
*/

(function($, window, document, undefined) {
(function($, window){
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 : true,
appear : null,
load : null,
placeholder : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC"
};
var plugin_name = 'lazyload';
var methods = {
init: function(options) {
options = options || {};
var self = this;
if(!this.length){
return;
}
var elements = this;
var $container;
var settings = {
threshold : 0,
failure_limit : 0,
event : "scroll",
effect : "show",
container : window,
data_attribute : "original",
skip_invisible : true,
appear : null,
load : null,
placeholder : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC"
};

function update() {
var counter = 0;
Expand All @@ -42,11 +49,11 @@
}
if ($.abovethetop(this, settings) ||
$.leftofbegin(this, settings)) {
/* Nothing. */
// Nothing.
} else if (!$.belowthefold(this, settings) &&
!$.rightoffold(this, settings)) {
$this.trigger("appear");
/* if we found an image we'll load, reset the counter */
// if we found an image we'll load, reset the counter
counter = 0;
} else {
if (++counter > settings.failure_limit) {
Expand Down Expand Up @@ -77,11 +84,11 @@

/* Fire one scroll event per scroll. Not one scroll event per image. */
if (0 === settings.event.indexOf("scroll")) {
$container.bind(settings.event, function() {
$container.on(settings.event + '.lazyload', function() {
return update();
});
}

var images = [];
this.each(function() {
var self = this;
var $self = $(self);
Expand All @@ -102,8 +109,8 @@
var elements_left = elements.length;
settings.appear.call(self, elements_left, settings);
}
$("<img />")
.bind("load", function() {
images.push($("<img />")
.one("load", function() {

var original = $self.attr("data-" + settings.data_attribute);
$self.hide();
Expand All @@ -127,7 +134,7 @@
settings.load.call(self, elements_left, settings);
}
})
.attr("src", $self.attr("data-" + settings.data_attribute));
.attr("src", $self.attr("data-" + settings.data_attribute)));
}
});

Expand All @@ -139,32 +146,61 @@
$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. */
$window.on("resize.lazyload",update);
// With IOS5 force loading images when navigating with back button.
// Non optimal workaround.
var apple_hack = function(event) {
if (event.originalEvent && event.originalEvent.persisted) {
elements.each(function() {
$(this).trigger("appear");
});
}
};
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");
});
}
});
$window.on("pageshow.lazyload", apple_hack);
}

/* Force initial check if images should appear. */
$(document).ready(function() {
update();
});
update();

return this;
$(this).data(plugin_name, {
destroy: function(){
$window.off("resize.lazyload", update);
$window.off("pageshow.lazyload", apple_hack);
$container.off(settings.event + '.lazyload');
$container = null;
elements.each(function(){
$(this).off('appear');
});
elements = null;
$(images).each(function(){
$(this).off();
});
images = [];
}
});
},
destroy: function() {
var $this = $(this),
data = $this.data(plugin_name);
if (data) {
data.destroy();
$this.removeData(plugin_name);
}
}
};

$.fn[plugin_name] = function(method) {
// Method calling logic
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist');
}
};

/* Convenience methods in jQuery namespace. */
Expand Down
2 changes: 1 addition & 1 deletion jquery.lazyload.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.