Explore posts about plants and learn detailed information about each.
El archivo contiene código JavaScript que simula un sistema de publicaciones paginadas utilizando una API para obtener imágenes aleatorias. Este sistema muestra tarjetas con información simulada, permite la interacción con botones de "like" y "dislike", y muestra detalles en un modal al hacer clic en una tarjeta. También incluye navegación entre páginas y muestra mensajes interactivos para mejorar la experiencia del usuario.
The file contains JavaScript code that simulates a paginated post system using an API to fetch random images. This system displays cards with simulated information, allows interaction with "like" and "dislike" buttons, and shows details in a modal when a card is clicked. It also includes navigation between pages and displays interactive messages to enhance the user experience.
const
y
let
para declarar
variables, una práctica moderna
introducida en ES6. Estas
declaraciones son más seguras y
previenen errores relacionados con
variables globales.
document.createDocumentFragment
para manipular elementos de manera
eficiente y reducir el número de
operaciones directas en el DOM, lo que
mejora el rendimiento. Además, los
eventos están desacoplados del HTML
utilizando
addEventListener
, una
práctica recomendada para mantener el
código modular.
toLocaleDateString
(para formatear fechas de manera
local) y
addEventListener
(para manejar eventos) están en línea
con las prácticas actuales y ofrecen
mejor compatibilidad y flexibilidad.
const PLANT_IMAGE_API =
"https://picsum.photos/300/200";
const POSTS_PER_PAGE = 9;
const TOTAL_POSTS = 30;
let currentPage = 1;
const postsContainer =
document.querySelector("#posts-container");
const previousPageBtn =
document.querySelector("#previous-page");
const nextPageBtn =
document.querySelector("#next-page");
function sanitize(input) {
const div =
document.createElement("div");
div.textContent =
input;
return div.innerHTML;
}
function generatePosts(page) { ...
}
function updateNavigationButtons()
{
const totalPages =
Math.ceil(TOTAL_POSTS /
POSTS_PER_PAGE);
previousPageBtn.disabled =
currentPage === 1;
nextPageBtn.disabled =
currentPage === totalPages;
}
function showDetails(postId) { ...
}
function handleInteraction(event,
action, postId) { ... }
previousPageBtn.addEventListener("click",
...);
generatePosts(currentPage);
const
and
let
to declare variables,
a modern practice introduced in ES6.
These declarations are safer and
prevent errors related to global
variables.
document.createDocumentFragment
to efficiently manipulate elements and
minimize direct DOM operations,
improving performance. Additionally,
events are decoupled from the HTML
using
addEventListener
, a
recommended practice for maintaining
modular code.
toLocaleDateString
(to format dates locally) and
addEventListener
(for handling events) align with
current best practices, offering
better compatibility and flexibility.
const PLANT_IMAGE_API =
"https://picsum.photos/300/200";
const POSTS_PER_PAGE = 9;
const TOTAL_POSTS = 30;
let currentPage = 1;
const postsContainer =
document.querySelector("#posts-container");
const previousPageBtn =
document.querySelector("#previous-page");
const nextPageBtn =
document.querySelector("#next-page");
function sanitize(input) {
const div =
document.createElement("div");
div.textContent =
input;
return div.innerHTML;
}
function generatePosts(page) { ...
}
function updateNavigationButtons()
{
const totalPages =
Math.ceil(TOTAL_POSTS /
POSTS_PER_PAGE);
previousPageBtn.disabled =
currentPage === 1;
nextPageBtn.disabled =
currentPage === totalPages;
}
function showDetails(postId) { ...
}
function handleInteraction(event,
action, postId) { ... }
previousPageBtn.addEventListener("click",
...);
generatePosts(currentPage);
Description here
User:
Date: