Skip to content

Commit d2bddd9

Browse files
authored
Lazy load background images on non-<img> elements
Per this PR: luis-almeida#128
1 parent eb0f88d commit d2bddd9

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

jquery.unveil.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
var source = this.getAttribute(attrib);
2424
source = source || this.getAttribute("data-src");
2525
if (source) {
26-
this.setAttribute("src", source);
27-
if (typeof callback === "function") callback.call(this);
26+
// if element is an image, set the src
27+
// else, set image as background
28+
+ $(this).is('img') ? $(this).attr("src", source) : $(this).css('background-image', 'url("' + source + '")');
29+
if (typeof callback === "function") callback.call(this);
2830
}
2931
});
3032

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

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

0 commit comments

Comments
 (0)