forked from ApoorvSaxena/lozad.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22b077a
commit 148ee99
Showing
3 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
node_modules | ||
|
||
dist | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*! lozad.js - v1.0.3 - 2017-09-14 | ||
* https://github.com/ApoorvSaxena/lozad.js | ||
* Copyright (c) 2017 Apoorv Saxena; Licensed MIT */ | ||
|
||
|
||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.lozad = factory()); | ||
}(this, (function () { 'use strict'; | ||
|
||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
|
||
var defaultConfig = { | ||
rootMargin: '0px', | ||
threshold: 0, | ||
load: function load(element) { | ||
element.src = element.dataset.src; | ||
} | ||
}; | ||
|
||
function markAsLoaded(element) { | ||
element.dataset.loaded = true; | ||
} | ||
|
||
var isLoaded = function isLoaded(element) { | ||
return element.dataset.loaded === 'true'; | ||
}; | ||
|
||
var onIntersection = function onIntersection(load) { | ||
return function (entries, observer) { | ||
entries.forEach(function (entry) { | ||
if (entry.intersectionRatio > 0) { | ||
observer.unobserve(entry.target); | ||
load(entry.target); | ||
markAsLoaded(entry.target); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
var lozad = function () { | ||
var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '.lozad'; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
|
||
var _defaultConfig$option = _extends({}, defaultConfig, options), | ||
rootMargin = _defaultConfig$option.rootMargin, | ||
threshold = _defaultConfig$option.threshold, | ||
load = _defaultConfig$option.load; | ||
|
||
var observer = void 0; | ||
|
||
if (window.IntersectionObserver) { | ||
observer = new IntersectionObserver(onIntersection(load), { | ||
rootMargin: rootMargin, | ||
threshold: threshold | ||
}); | ||
} | ||
|
||
return { | ||
observe: function observe() { | ||
var elements = [].filter.call(document.querySelectorAll(selector), function (element) { | ||
return !isLoaded(element); | ||
}); | ||
|
||
if (!observer) { | ||
elements.forEach(function (element) { | ||
load(element); | ||
markAsLoaded(element); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
elements.forEach(function (element) { | ||
observer.observe(element); | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
return lozad; | ||
|
||
}))); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.