Creating an Online Photo Album with JavaScript

Looking for assistance to finish up my photo gallery project. I have created thumbnails that display 5 images at the top of my page, and below them is a div that should show the selected image in its original size when clicked. Can anyone guide me on what CSS and JavaScript code I need to add to complete this functionality? Your help would be greatly appreciated.

Thank you.

HTML:

div id="img-thumbnails">
    <img src="assets/imgs/img-01.jpg">
    <img src="assets/imgs/img-02.jpg">
    <img src="assets/imgs/img-03.jpg">
    <img src="assets/imgs/img-04.jpg">
    <img src="assets/imgs/img-05.jpg">
</div>

<div id="img-original">
</div>

CSS:

#img-thumbnails {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
}

#img-thumbnails img {
    width:200px;
    height:100px;
    cursor: pointer;
    margin-left:20px;
}

#img-original {
    height:500px;
    width:800px;
    border:10px solid black;
    margin:30px auto;
}

Answer №1

Here is a sample implementation that can serve as a reference for resolving your problem.

In the absence of available images, I have utilized div elements in this demonstration.

var preview = document.getElementById('pretend_view');
var images = document.getElementById('pretend_images');
var thumbnails = document.querySelectorAll('#pretend_images > div');

thumbnails.forEach(function(elem){
elem.onclick = onClick.bind(null, elem);
});

function onClick(elem, e){
clearPreview();
  addToPreview(elem);
}

function clearPreview(){
var elem = document.querySelector('.preview');
  
  if(elem){
    elem.className = elem.className.replace('preview', '');
    images.appendChild(elem);
  }
}

function addToPreview(elem){
elem.className = 'preview';
  preview.appendChild(elem);
}
#pretend_images > div {
    display: inline-block;
    width:50px;
    height:50px;
    cursor: pointer;
    margin-left:20px;
}

#pretend_preview {
    height:200px;
    width:300px;
    border:10px solid black;
    margin:30px auto;
}

#a {
  background-color: black;
}

#b {
  background-color: blue;
}

#c {
  background-color: green;
}

#d {
  background-color: pink;
}

.preview {
    height:200px;
    width:300px;
}
<div id="pretend_images">
  <div id="a"></div>
  <div id="b"></div>
  <div id="c"></div>
  <div id="d"></div>
</div>

 <div id="pretend_view"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Implement Snap-Enabled Dragging in a Container Using jQuery UI

Issue Description (Fiddle): I am using the jQuery UI's .draggable() function to make the red element snap inside the blue container. However, it is snapping to one edge only instead of completely fitting inside (both edges). It requires further dragg ...

What occurs when a function component is passed as a state variable?

Can a function component be passed as a state variable? Is it possible for the code below to work correctly? I attempted it but encountered an error stating props.children isn't a function. Do you know if this approach can be successful? const App = ...

The function for the "load next page" action in ngInfiniteScroll is continuously triggered

After attempting to implement infinite scrolling using the "loading remote data" example from the ngInfiniteScroll website, I am facing a problem. The function nextPage() is being called continuously until all records have been loaded (controlled by an of ...

Convert JavaScript files into JSON files using Laravel

I have a JavaScript file containing key-value pairs as shown below: es.js export default { currency: "Dinero", void: "Vacio", } Now, I need to convert this file into a JSON format. es.json { "currency" : "Dinero", "void" : "Vacio", } W ...

Include a fresh attribute in the Interface

How can I include a boolean property isPhotoSelected: boolean = false; in an API interface that I cannot modify? The current interface looks like this: export interface LibraryItem { id: string; photoURL: string; thumbnailURL: string; fi ...

What is the best way to ensure that a Material UI transition component fully occupies the space of its parent component?

I've been experimenting with a Material UI transition component in an attempt to make it completely fill its parent container. So far, I've achieved some success by setting the width and height to 100% and applying a Y-axis translation for the co ...

Error: Invalid Argument - The argument 'PanelController' is expected to be a function, but it is undefined

Here is a snippet of my HTML code: <body ng-controller="StoreController as store"> ........... <section ng-controller="PanelController as panel"> <ul class="nav nav-pills""> <li ng-class="{active:panel.isSe ...

Tips for customizing the appearance of Material UI's time picker diolog styles

I am currently incorporating the Material UI Time Picker from GitHub, specifically TeamWertarbyte's repository. My task involves modifying the color of the time displayed. https://i.sstatic.net/3ezSY.png In the image provided, I aim to customize th ...

What is the best way to utilize toggle("slide") in order to reveal a message letter by letter?

I am currently experimenting with the `.toggle("slide") function to create an effect where a piece of text appears as though each letter is sliding in. However, I've noticed that instead of a smooth slide, it looks more like the text is flying in abru ...

Using jQuery to retrieve the text content of child elements

Struggling to extract the text of child elements using jQuery. I've been at this for a couple of days and can't seem to make it work. If anyone could spare a moment to review, I would greatly appreciate it! SCRIPT: function generateRemoveSect ...

Angular and D3.js: Enhancing StackedBar Chart ToolTips with Added Functionality

I have modified the code from this example to be compatible with the latest versions of Angular and D3. Although it works well after a small tweak, I am unable to see the tooltips when hovering over the chart... https://i.sstatic.net/Rpebe.png <h3> ...

Uploading files in ASP.NET MVC without creating a view, utilizing Valums Ajax Uploader technology

I recently completed a tutorial on ASP.NET MVC file uploads using Valums plugin and made sure to place all the necessary js, css, and gif files in their respective folders. However, I am facing an issue where the view is not displaying anything. <link ...

JavaScript random number functionality experiencing an unexpected problem

function generateRandomNumbers(){ var max = document.getElementById('max').value; var min = document.getElementById('min').value; var reps = document.getElementById('reps').value; var i; for (i=0; i<reps ...

Building a loading spinner component in a react-native project

I have successfully implemented a loading spinner that is currently being used in various components of my React project. However, as I am now working on a react-native version of the application, I am faced with the challenge of recreating this loading sp ...

Create a d3 map specifically for a selected region based on the provided latitude and longitude coordinates

I am currently working on developing a d3 map inspired by the codepen created by Andy Barefoot: https://codepen.io/nb123456/pen/zLdqvM?editors=0010. My goal is to adjust the initiateZoom() function in a way that setting specific lat/lon coordinates for a b ...

Displaying a list of terms and their corresponding definitions side by side

I need to display multiple rows of data, with a title and corresponding data on the same line. While I have found answers on sites like SO (e.g. How to style dt and dd so they are on the same line?), the suggested solutions don't seem to work for my s ...

Expanding Your jQuery Library from a Content Delivery Network

As of now, I have a jQuery hosted on my FTP along with other web files. YSlow recommends using a CDN for jQuery. However, I am wondering how I can extend that jQuery. Is it possible to add custom functions to it without being able to edit the original co ...

How is it possible that both of my AngularJS services are being directed to the exact same Restful Java EE web service?

I am facing an issue with my two angularjs services. One is supposed to retrieve a single user while the other should return an array of users. However, both seem to be calling the service that returns an array of users. Can you help me understand why this ...

Issues with dropdown checklist functionality

I'm having trouble creating a dropdown checkbox list for my blog. Every time I try to check one of the boxes, the list closes unexpectedly. Any assistance would be greatly appreciated! The element is essentially a styled dropdown with checkboxes embed ...

Node.JS executes Sandbox within a RESTful service environment

Utilizing the Node Restify Module to develop a REST service that accepts POST requests. Inside the service, I am attempting to create a Sandboxed process using the Node Sandbox module in order to execute dynamically inserted JavaScript without impacting th ...