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

Fetch image original either from data-original, or content or contentUrl attr #252

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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ $("img.lazy").lazyload();

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

If you have adopted microformats ([schema.org](https://schema.org/ImageObject)'s example):
```
<img class="lazy" itemprop="image" contentUrl="img/example.jpg" width="640" height="480">
```

then in your code do:

```
$("img.lazy").lazyload({attribute: "contentUrl"});
```

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

## Install
Expand Down
10 changes: 8 additions & 2 deletions jquery.lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
effect : "show",
container : window,
data_attribute : "original",
attribute : "contentUrl",
skip_invisible : false,
appear : null,
load : null,
Expand Down Expand Up @@ -57,6 +58,10 @@

}

function getOriginal($e) {
return $e.attr("data-" + settings.data_attribute) || $e.attr(settings.attribute);
}

if(options) {
/* Maintain BC for a couple of versions. */
if (undefined !== options.failurelimit) {
Expand Down Expand Up @@ -98,14 +103,15 @@
/* When appear is triggered load original image. */
$self.one("appear", function() {
if (!this.loaded) {
var original = getOriginal($self);

if (settings.appear) {
var elements_left = elements.length;
settings.appear.call(self, elements_left, settings);
}
$("<img />")
.bind("load", function() {

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

Expand Down