-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcerca.js
More file actions
51 lines (46 loc) · 1.57 KB
/
cerca.js
File metadata and controls
51 lines (46 loc) · 1.57 KB
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
function onResponse(response){
return response.json();
}
function createImage(src) {
const image = document.createElement('img');
image.src = src;
return image;
}
function onJSON(json){
albumView.innerHTML="";
const results = json.hits;
for(foto of results)
{
const img_url = foto.webformatURL;
const image = createImage(img_url);
image.addEventListener('click', onThumbnailClick);
const div= document.createElement("div");
div.classList.add("element");
div.appendChild(image);
albumView.appendChild(div);
}
}
function cerca_foto(event){
event.preventDefault();
const form_data={method: "post", body: new FormData(event.currentTarget) }
fetch("search.php", form_data).then(onResponse).then(onJSON);
}
//modale
function onThumbnailClick(event) {
const image = createImage(event.currentTarget.src);
document.body.classList.add('no-scroll');
modalView.style.top = window.pageYOffset + 'px';
modalView.appendChild(image);
modalView.classList.remove('hidden');
}
function onModalClick() {
document.body.classList.remove('no-scroll');
modalView.classList.add('hidden');
modalView.innerHTML = '';
}
const modalView = document.querySelector('#modal-view');
modalView.addEventListener('click', onModalClick);
//______________
const albumView = document.querySelector('#album-view');
const form = document.querySelector('form');
form.addEventListener('submit', cerca_foto);