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

Leaks fix #354

Open
wants to merge 2 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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ $("img.lazy").lazyload();

This causes all images of class lazy to be lazy loaded.

For destroying event listeners:

```js
$("img.lazy").lazyload.destroy();
```

More information on [Lazy Load](http://www.appelsiini.net/projects/lazyload) project page.

## Install
Expand All @@ -45,4 +51,3 @@ $ npm install jquery-lazyload
# License

All code licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php). All images licensed under [Creative Commons Attribution 3.0 Unported License](http://creativecommons.org/licenses/by/3.0/deed.en_US). In other words you are basically free to do whatever you want. Just don't remove my name from the source.

21 changes: 17 additions & 4 deletions jquery.lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

$.fn.lazyload = function(options) {
var elements = this;
var eventNameSpace = '.lazyload';
var $container;
var settings = {
threshold : 0,
Expand Down Expand Up @@ -57,6 +58,18 @@

}

$.fn.lazyload.destroy = function() {
$window.off(eventNameSpace);
$container.off(eventNameSpace);

elements.each(function() {
var self = this;
var $self = $(self);

$self.off(eventNameSpace);
});
}

if(options) {
/* Maintain BC for a couple of versions. */
if (undefined !== options.failurelimit) {
Expand All @@ -77,7 +90,7 @@

/* Fire one scroll event per scroll. Not one scroll event per image. */
if (0 === settings.event.indexOf("scroll")) {
$container.on(settings.event, function() {
$container.on(settings.event + eventNameSpace, function() {
return update();
});
}
Expand Down Expand Up @@ -133,7 +146,7 @@
/* When wanted event is triggered load original image */
/* by triggering appear. */
if (0 !== settings.event.indexOf("scroll")) {
$self.on(settings.event, function() {
$self.on(settings.event + eventNameSpace, function() {
if (!self.loaded) {
$self.trigger("appear");
}
Expand All @@ -142,14 +155,14 @@
});

/* Check if something appears when window is resized. */
$window.on("resize", function() {
$window.on("resize" + eventNameSpace, 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.on("pageshow", function(event) {
$window.on("pageshow" + eventNameSpace, function(event) {
if (event.originalEvent && event.originalEvent.persisted) {
elements.each(function() {
$(this).trigger("appear");
Expand Down