forked from gradiweb/Gradi-Gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
58 lines (54 loc) · 1.75 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Constantes
const apiPath = "api/gradiAuthors.json";
const cont = 12;
// Contadores
var photosApi;
var init = 0;
var end = cont;
// Selector
let imageGrid = document.querySelector('#imageGrid');
// Cargar JSON y asignarlo a var photosApi
function loadJSON() {
// Crear objeto para el Request
const apiJSON = new XMLHttpRequest();
apiJSON.overrideMimeType("application/json");
apiJSON.open('GET', apiPath, true);
apiJSON.send();
apiJSON.onreadystatechange = function () {
// Validar estado del Request
if (this.readyState == 4 && this.status == "200") {
// Asignar respuesta del request
photosApi = JSON.parse(this.responseText);
// Agregar las primeras imagenes al DOM
addToDom(photosApi.slice(init, end));
}
};
}
// Agregar mas objetos al DOM
const addToDom = photos => {
photos.forEach(photo => {
// Crear estryctura del elemento
let el = document.createElement("div");
el.classList.add('card-item');
el.innerHTML =
`<img class="card-image" src="${photo.download_url}">
<div class="card-overlay">
<h3 class="card-id">${photo.id}</h3>
<img class="card-logo" src="assets/img/logo-white.png">
<h3 class="card-author">${photo.author}</h3>
</div>`;
imageGrid.appendChild(el);
});
// Aumentar contadores
init = init + cont;
end = end + cont;
};
// Validar tamaño del scroll
window.addEventListener('scroll', function() {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 10) {
// Ejecutar función para agregar objetos al DOM
addToDom(photosApi.slice(init, end));
}
});
// Inicializar API
loadJSON();