Dynamic Photo Gallery

Project Overview

Build an interactive photo gallery that demonstrates your understanding of arrays and DOM manipulation. This project builds on the basic photo gallery by adding dynamic features and array operations.

ArraysDOM ManipulationEvent HandlingIntermediate45 minutes

Learning Objectives

  • Work with arrays to manage image collections
  • Use array methods to filter and sort images
  • Implement dynamic DOM updates
  • Handle user interactions with event listeners
  • Practice array transformation methods

Project Requirements

1. Gallery Setup

  • Create an array of image objects with properties:
    • url: string (image URL)
    • title: string (image title)
    • category: string (image category)
    • date: Date (when added)
  • Display images in a responsive grid layout
  • Show image title and category below each image

2. Array Operations

  • Implement category filtering using array filter() method
  • Add sorting options (by date, title) using array sort() method
  • Create a search function using array find() or filter()
  • Allow adding new images to the array using push()

3. Interactive Features

  • Click image to view larger version in a modal
  • Add/remove images from favorites using array methods
  • Implement a slideshow feature using array iteration
  • Add image upload capability (URL input)

Getting Started

Step 1: Set Up Data Structure

const gallery = {
  images: [
    {
      url: 'path/to/image1.jpg',
      title: 'Mountain Landscape',
      category: 'Nature',
      date: new Date('2024-01-15')
    },
    // Add more images...
  ],
  favorites: [],
  currentCategory: 'all'
};

Step 2: Create Display Functions

function displayImages(images) {
  const gallery = document.querySelector('.gallery');
  gallery.innerHTML = images
    .map(img => `
      <div class="gallery-item">
        <img src="${img.url}" alt="${img.title}">
        <h3>${img.title}</h3>
        <p>${img.category}</p>
      </div>
    `).join('');

Step 3: Implement Array Operations

// Filter by category
function filterByCategory(category) {
  return gallery.images
    .filter(img => category === 'all' || img.category === category);
}

// Sort by date
function sortByDate() {
  return [...gallery.images]
    .sort((a, b) => b.date - a.date);
}

// Search by title
function searchImages(query) {
  return gallery.images
    .filter(img => 
      img.title.toLowerCase()
        .includes(query.toLowerCase())
    );
}

Tips for Success

  • Use array methods instead of loops where possible
  • Keep your code DRY (Don't Repeat Yourself)
  • Add error handling for image loading
  • Use meaningful variable and function names
  • Comment your code to explain complex operations

Bonus Challenges

  • Add image lazy loading for better performance
  • Implement drag-and-drop reordering of images
  • Add image categories using array grouping
  • Create an image carousel using array rotation