Skip to content

Commit

Permalink
Merge pull request #5 from thackba/master
Browse files Browse the repository at this point in the history
Change registerLazyLoad - Visible images will be loaded directly without scrolling
  • Loading branch information
Fabian H authored Aug 23, 2016
2 parents ebd0b33 + 02fb9df commit d81e84e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
9 changes: 5 additions & 4 deletions spec/loadIfVisibleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("justlazy auto loader by html object", function() {
loadFixtures("loadIfVisible.html");
});

it("should load visible placeholder", function(done) {
it("should load visible placeholder after load", function(done) {
// given
var placeholder1 = document.getElementById("img1");
expect(placeholder1).toHaveAttr("data-src", base64Image);
Expand All @@ -37,9 +37,6 @@ describe("justlazy auto loader by html object", function() {
fail();
}
});

// when
triggerScrollEvent(0);
});

it("should load visible placeholder after scroll event", function(done) {
Expand Down Expand Up @@ -108,6 +105,10 @@ describe("justlazy auto loader by html object", function() {
expect(callCount).toBe(0);
done();
}, 30);

// placeholder was not changed
expect(document.getElementById("img3")).toHaveAttr("data-src", base64Image3);

});

it("should remove event listener after image loading", function(done) {
Expand Down
14 changes: 9 additions & 5 deletions src/justlazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,16 @@
* on the screen. E.g. 200px before it become visible.
*/
var registerLazyLoad = function(imgPlaceholder, options) {
var loadImgIfVisible = _loadImgIfVisible(imgPlaceholder, _validateOptions(options));

if (window.addEventListener) {
window.addEventListener("scroll", loadImgIfVisible, false);
var validatedOptions = _validateOptions(options);
if (_isVisible(imgPlaceholder, validatedOptions.threshold)) {
lazyLoad(imgPlaceholder, options);
} else {
window.attachEvent("onscroll", loadImgIfVisible);
var loadImgIfVisible = _loadImgIfVisible(imgPlaceholder, validatedOptions);
if (window.addEventListener) {
window.addEventListener("scroll", loadImgIfVisible, false);
} else {
window.attachEvent("onscroll", loadImgIfVisible);
}
}
};

Expand Down

0 comments on commit d81e84e

Please sign in to comment.