Skip to content

Commit

Permalink
Lazy load background images on non-<img> elements
Browse files Browse the repository at this point in the history
Per this PR: luis-almeida#128
  • Loading branch information
nabrown authored Mar 4, 2017
1 parent eb0f88d commit d2bddd9
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions jquery.unveil.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
var source = this.getAttribute(attrib);
source = source || this.getAttribute("data-src");
if (source) {
this.setAttribute("src", source);
if (typeof callback === "function") callback.call(this);
// if element is an image, set the src
// else, set image as background
+ $(this).is('img') ? $(this).attr("src", source) : $(this).css('background-image', 'url("' + source + '")');
if (typeof callback === "function") callback.call(this);
}
});

Expand All @@ -38,7 +40,7 @@
et = $el.offset().top, // distance from document top to top of element
eb = et + $el.height(); // distance from top of document to bottom of element

// the bottom of the element is below the top of the browser (- threshold)
// the bottom of the element is below the top of the browser (- threshold)
// && the top of the element id above the bottom of the browser (+ threshold)
return eb >= wt - th && et <= wb + th;
});
Expand Down

0 comments on commit d2bddd9

Please sign in to comment.