Skip to content

Commit

Permalink
Refatorando functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ek1l committed May 10, 2023
1 parent 2f6b95e commit 8ea49bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
55 changes: 26 additions & 29 deletions 02-functions/00-galeria-fotos/assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ const data = {
};

let posicao = 0;
let total = data.pictures.length


renderImage(data.pictures[posicao]);
renderThumbnail(templateThumbnailList(data.pictures))

let total = data.pictures.length;

function renderThumbnail(htmlThumbnails) {
document.getElementById("container-fotos-lista").innerHTML = htmlThumbnails;
Expand Down Expand Up @@ -84,22 +79,13 @@ function renderImage(photoName) {
document.getElementById("img-principal").src = `fotos/${photoName}`;
}



function handleThumbnailClick(event) {
event.preventDefault();

posicao = parseInt(event.currentTarget.getAttribute("data-posicao"), 10);

renderImage(data.pictures[posicao]);
markArrows();
markThumbnail();
renderGallery()
}





function prevPosition(position) {
return position <= 0 ? 0 : position - 1;
}
Expand All @@ -116,22 +102,21 @@ function handleArrowsClick(event) {
? prevPosition(posicao)
: nextPosition(posicao, total);

renderImage(data.pictures[posicao]);

markArrows();
markThumbnail();
renderGallery()
}

const todasFotos = document.querySelectorAll(".foto-item");
function bindEvent() {
const todasFotos = document.querySelectorAll(".foto-item");

for (let i = 0; i < todasFotos.length; i++) {
todasFotos[i].addEventListener("click", handleThumbnailClick);
}
for (let i = 0; i < todasFotos.length; i++) {
todasFotos[i].addEventListener("click", handleThumbnailClick);
}

const botoes = document.querySelectorAll(".btn");
const botoes = document.querySelectorAll(".btn");

for (let i = 0; i < botoes.length; i++) {
botoes[i].addEventListener("click", handleArrowsClick);
for (let i = 0; i < botoes.length; i++) {
botoes[i].addEventListener("click", handleArrowsClick);
}
}

function markArrows() {
Expand All @@ -158,5 +143,17 @@ function markThumbnail() {

items[posicao].classList.add("border-primary");
}
markArrows();
markThumbnail();

const renderGallery = () => {
renderImage(data.pictures[posicao]);
markArrows();
markThumbnail();
};

function init() {
renderThumbnail(templateThumbnailList(data.pictures));
renderGallery();
bindEvent();
}

init();
2 changes: 1 addition & 1 deletion 02-functions/00-galeria-fotos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<img id="img-principal" class="img-fluid" />
</div>
</div>

<div id="fotos-lista" class="d-flex flex-nowrap align-items-center mt-5">
<button id="btn-anterior" class="btn btn-link disabled">
<svg width="2em" height="2em" viewBox="0 0 16 16" class="bi bi-arrow-left-circle-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
Expand Down

0 comments on commit 8ea49bc

Please sign in to comment.