Home > Article > Web Front-end > Creating My Pokémon Collection App: Local Data, Interactivity, and JavaScript Events
When I started this project, my goal was simple: to create a Pokémon collection app where users could explore Pokémon and their details. By combining JavaScript, Node.js, and local data, I built an interactive app that allows users to view Pokémon images, abilities, and types—all while refining my skills with event listeners, dynamic content updates, and more. While I worked on this with a partner, I am going to focus on my work specifically. Here’s how it came together!
This app is designed to showcase a collection of Pokémon images and stats, making it easy for users to click on each Pokémon to view details and switch between main and alternate images with hover interactions. I served the Pokémon data locally, which allowed me to focus on how to render, update, and interact with the data within the app itself.
Instead of pulling from an external API, I created a local db.json file with Pokémon data, including attributes like name, type, ability, and image paths. Running a local server on http://localhost:3500/pokemon, I could fetch this data using the fetch method in JavaScript. The simplified setup allowed me to build and test the app entirely offline.
Here’s a look at how I approached key parts of the project.
function getAllPokemon() { return fetch(pokemonURL).then(response => response.json()); }
This function pulls in the Pokémon data from http://localhost:3500/pokemon, which I then use to dynamically render each Pokémon.
const displayPokemons = () => { getAllPokemon().then(pokemonArr => { pokemonArr.forEach(renderPokemon); handleClick(pokemonArr[0]); // Display first Pokémon by default }); }
The renderPokemon function creates an image for each Pokémon, adds styling, and attaches a click event to display details.
Form Submission:
To add a new Pokémon, I set up a submit listener on the form, which gathers values from input fields and adds the new Pokémon to the collection. This new Pokémon is rendered without needing to refresh the page:
function getAllPokemon() { return fetch(pokemonURL).then(response => response.json()); }
Here, event.preventDefault() stops the form from reloading the page, ensuring a smooth user experience.
Mouseover Event for Alternate Images:
When users hover over the Pokémon’s image in the details section, it switches to an alternate image, simulating an evolution or transformation. The mouseover event triggers this switch, while mouseout reverts it back:
const displayPokemons = () => { getAllPokemon().then(pokemonArr => { pokemonArr.forEach(renderPokemon); handleClick(pokemonArr[0]); // Display first Pokémon by default }); }
This effect gives users a fun way to interact with each Pokémon and visually explore its characteristics.
One challenge was structuring the JavaScript to keep it modular and manageable, as event listeners and dynamic elements can quickly become hard to track. I learned to compartmentalize my code into smaller functions and to use event listeners selectively to optimize performance and readability.
This Pokémon collection project was an exciting way to apply JavaScript, experiment with local data fetching, and add engaging event-driven interactivity. Building this app from scratch gave me valuable experience with both front-end and back-end concepts, leaving me inspired to explore more interactive projects in the future.
https://github.com/kelseyroche/phase-1-project-pokemon
The above is the detailed content of Creating My Pokémon Collection App: Local Data, Interactivity, and JavaScript Events. For more information, please follow other related articles on the PHP Chinese website!