Skip to content

Commit

Permalink
Intersection Observer - Manipulación del DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
FROSTYLAN committed Mar 17, 2023
1 parent b7e231c commit b354fff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/index.js
@@ -1,7 +1,4 @@
/**
* This file is just a silly example to show everything working in the browser.
* When you're ready to start on your site, clear the file. Happy hacking!
**/
import { registerImage } from "./lazy";

const min = 1;
const max = 122;
Expand All @@ -21,14 +18,13 @@ const createImageNode = () => {
return container;
};

const nuevaImagen = createImageNode();
const mountNode = document.getElementById("images");

const addButton = document.querySelector("button");

const addImage = () => {
const newImage = createImageNode();
mountNode.appendChild(newImage);
registerImage(newImage);
};

addButton.addEventListener("click", addImage);
20 changes: 20 additions & 0 deletions src/lazy.js
@@ -0,0 +1,20 @@
const isIntersecting = (entry) => {
return entry.isIntersecting; // True -> dentro de la pantalla
};

const accion = (entry) => {
const nodo = entry.target;
console.log("que fue manito");

// Cuando ya cargó la imagen desregistra.
observer.unobserve(nodo);
};

// Entries -> Todos los elemento que está observando.
const observer = new IntersectionObserver((entries) => {
entries.filter(isIntersecting).forEach(accion);
});

export const registerImage = (imagen) => {
observer.observe(imagen);
};

0 comments on commit b354fff

Please sign in to comment.