Interactive image grid with adjustable description field per image upon selection

My goal is to create a grid of images with a single text field below the grid. This text field should display the description of the image that was last clicked. The grid is implemented using floating divs within a main div, as shown in the code snippet below. While the exact implementation may not be crucial, I am seeking advice on how best to implement the dynamic description box. Would pure HTML/CSS suffice for this task, or will javascript/jquery be necessary? One approach could involve placing all descriptions in a single box and adjusting z-indices on click events. However, I am unsure if this is the optimal solution or if it would function correctly.

I have yet to find a definitive answer to this question online, so I hope that by asking here, it can also benefit others who may have similar inquiries.

Answer №1

Using jQuery makes it really simple. You can save the text you want to display in an element, or even in an attribute like `alt` or `data-` on the `img` tag. Then just grab that text and fill your div at the bottom.

$('.ingredient').on('click',function() {
  $('#caption').html($(this).find('.meta').html())
})
.meta {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ingredient-showcase">
  <div class="ingredient"><img src="..." alt="..." />
    <div class="meta">1</div>
  </div>
  <div class="ingredient"><img src="..." alt="..." />
    <div class="meta">2</div>
  </div>
  <div class="ingredient"><img src="..." alt="..." />
    <div class="meta">3</div>
  </div>
  <div id="caption"></div>
</div>

Answer №2

Avoid using the z-index property and instead utilize JavaScript or jQuery to populate the box with descriptions. You have the option to retrieve the description from the alt tag or by adding a data attribute containing the description and retrieving it from there, as shown below:

<div id="ingredient-showcase">
    <div class="ingredient"><img src="..." data-description="..." /></div>
    ...
</div>
<div id="description"></div>

With jQuery, you can simply implement the following script:

$(function () {
    $('.ingredient > img').click(function () {
        $('#description').text($(this).data('description'));
    });
});

Answer №3

var TEMP_URL = "https://cdn.example.com/photo.jpg";

var $grid = $('#grid');
var $description = $('#description');

var columns = 3;

var items = [{
    src: TEMP_URL,
    description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    alt: "I've no idea what alt is."
  },
  {
    src: TEMP_URL,
    description: "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
    alt: "I've no idea what alt is."

  },
  {
    src: TEMP_URL,
    description: "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
    alt: "I've no idea what alt is."

  }];

$description.text(items[0].description);

var $row;
items.forEach(function(item, index) {
  if (index % columns === 0) {
    $row = $('<div class="row">');
    $grid.append($row);
  }
  var $column = $('<div class="column">');
  var $img = $('<img alt="' + item.alt + '" src="' + item.src + '">');
  $column.append($img);
  $column.css({
    width: 100 / columns + '%',
    float: 'left'
  });
  $column.click(function() {
    $description.text(items[index].description);
  });
  $row.append($column);
});

var $row = $('<div>'); 
* {
  box-sizing: border-box;
}

#over {
  width: 80%;
  margin: 20px auto;
}

.grid {
  display: block;
}

.row {
  overflow: hidden;
  display: block;
}

.column {
  border: none;
  display: block;
}

.description {
  font-size: 12px;
}

img {
  width: 100%;
  padding: 3px;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="over">
  <div id="grid" class="grid"></div>
  <div id="description" class="description"></div>
</div>

Answer №4

#ingredient-list {
  margin: auto;
  width: 90%
}

.item {
  border: 1px solid #ccc;
  height: 170px;
  width: 170px;
  float: left;
  padding: 5px;
}

#description-box {
  clear: both;
  padding: 5px;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
  <div id="ingredient-list">
    <div class="item" ng-click="description = 'description 1'">
      <img src="..." alt="...">
    </div>
    <div class="item" ng-click="description = 'description 2'">
      <img src="..." alt="..." />
    </div>
    <div class="item" ng-click="description = 'description 3'">
      <img src="..." alt="..." />
    </div>
    ...
  </div>
  <p id="description-box">{{ description }}</p>
</div>

Answer №5

How about using angular.js for this?

var app = angular.module('app', []);

app.controller('IngredientController', function($scope) {
  $scope.description = '';

  $scope.setDescription = function(description) {
    $scope.description = description;
  };

  $scope.ingredients = [{
      src: "...",
      alt: "...",
      description: "description 1"
    },
    {
      src: "...",
      alt: "...",
      description: "description 2"
    },
    {
      src: "...",
      alt: "...",
      description: "description 3"
    }
  ];
});
* {
  box-sizing: border-box;
}

#over {
  text-align: center;
}

#ingredient-showcase {
  margin: auto;
  display: block;
  width: 90%;
  overflow: hidden;
  text-align: center;
}

.ingredient {
  border: 1px solid #ccc;
  display: inline-block;
  height: 170px;
  width: 170px;
  float: left;
  padding: 5px;
}

.ingredient img {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app="app" id="over" ng-controller="IngredientController">
  <div id="ingredient-showcase">
    <div class="ingredient" ng-repeat="ingredient in ingredients" ng-click="setDescription(ingredient.description)">
      <img ng-src="{{ ingredient.src }}" alt="{{ ingredient.alt }}" />
    </div>
  </div>
  <div>{{ description }}</div>
</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

Ensure the cursor is continually grabbing while moving items within a list using the @angular/cdk/drag-drop functionality

I have an example on stackblitz where I am using @angular/cdk/drag-drop in my project. I am attempting to change the cursor to cursor:grabb and cursor:grabbing when the cursor is over an element and when I drag a picked element. Here is the CSS line I am ...

Difficulty Uploading Files

I'm facing an issue when trying to upload multiple files. When I select more than 1 djz_file, no information is obtained from $_POST and $_FILES. However, if it's a single file, everything works as expected. <fieldset> ...

Add CSS styling to input text that is not empty

Is it possible to achieve this using CSS3 alone? var inputs = document.getElementsByTagName("INPUT"); for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == "text") { if (inputs[i].value != "") { inputs[i].s ...

The unexpected syntax error, indicated by an ampersand followed by a quotation mark, was encountered in [location] on line 7

Every time I try to run my code, I encounter the following error message: Parse error: syntax error, unexpected '"' in D:\server\WT-NMP\WWW\myproj1\insert.php on line 7 This is the code that is triggering the error: &l ...

jQuery's outerHeight() and height functions may experience flickering or fail to update properly when the window is resized to an odd number

Two elements are placed side by side on a webpage. One element, with a fixed size of 100vh, is named .hero-half, while the other element, holding text of varying lengths, is fluid and labeled as .project-details. When the text container extends beyond the ...

Ways to retrieve Payload following the Request url access

Currently utilizing Selenium with Python to conduct website testing, I successfully accessed the Request link and now aim to access the Payload. Below is an image displaying the process: view image description here driver = webdriver.Chrome(options=option) ...

Creating a JSON File with an Illustrator Script

Currently, I've been working diligently on developing a system that allows me to export my swatches from Illustrator as a JSON object. This will greatly simplify the process of updating my App. By utilizing the illustrator scripting API, I've suc ...

How can I retrieve the Azure subscription IDs of the currently logged in user using @azure/msal-angular?

I successfully authenticated a user using @azure/msal-angular and received the id_Token, access_Token and tenant Id. Now I am looking to retrieve the logged in user's azure subscriptions. Is there a way to achieve this through msal or are there any Ja ...

Styling with CSS: Creating a scrollable gallery of images with a hover effect

I'm struggling to align multiple images horizontally on the page with a hover effect for each image. I can achieve one or the other separately, but not both together. As a beginner in HTML/CSS, I know it's a simple process. Below is my code with ...

When using ng-repeat in Angular.js, an additional td is created

https://jsfiddle.net/gdrkftwm/ https://i.sstatic.net/CTi2F.jpg I have encountered a problem while creating a table from a Json object. There seems to be an extra td being generated, and I'm not sure why. I want the structure of my table to resemble ...

Repairing a syntax error in a jQuery selector variable

$(".className").click(function(){ var link = $(this).find("a").attr('href'); //output is '#myID' var findItems = $(link '.mydiv').length; //WRONG var findItems = $(link + '.mydiv').length; ...

Issue with implementing JQuery datepicker within Angular 7 CLI application

I've been working on my application and trying to implement the jQuery datepicker functionality. It's an Angular CLI app, and I have installed jquery-datepicker and jquery using npm. Here is a snippet of the dependencies in my package.json: "@a ...

Leverage the Power of Mongoose Schema Across Various Files (mongoose)

I recently encountered an issue while trying to utilize my Permissions schema in two different files. The error message "Cannot overwrite 'example' model once compiled" keeps popping up. Query: How can I effectively employ my Permissions schema ...

What is the best way to implement a never-ending scrolling grid loader within a scrollable area using Codeigniter?

In my Codeigniter framework and bootstrap installation, I have multiple sub-pages. On one of these pages, I am attempting to implement an infinite scroll loader using a jQuery script from a tutorial found at gridScrollFx.js. Here is the JS file I am using: ...

Prevent clicks from passing through the transparent header-div onto bootstrap buttons

I have a webpage built with AngularJS and Bootstrap. It's currently in beta and available online in (German and): teacher.scool.cool simply click on "test anmelden" navigate to the next page using the menu This webpage features a fixed transparent ...

The issue with updating the menu class in Internet Explorer 8 is not being resolved

Here is a code snippet using JavaScript: var x = jQuery(window).innerHeight(); jQuery(document).scroll(function() { if (jQuery(this).scrollTop() >= x) { jQuery('#nav').removeClass('nav').addClass('topfix_nav'); ...

How can I retrieve text instead of HTML using jQuery.get?

I'm facing an issue where I am trying to retrieve data from another site using the code below: jQuery.ajaxSetup({ crossDomain: true, dataType: "jsonp text" }); jQuery.get(url, function(rawContent) { console.log(rawContent); }); However, ...

javascriptEmbed youtube video thumbnail dynamically as users input a URL

I am currently working on a React frontend for my web app. One of the features I want to implement is a URL input box, with an image display panel below it. The goal is that when a user enters a YouTube URL into the input box, the thumbnail of the correspo ...

Creating a conditional query in Mongoose: A step-by-step guide

The code below functions without any query strings or with just one query string. For example, simply navigating to /characters will display all characters. However, if you specify a query string parameter like /characters?gender=male, it will only show ma ...

Error in jQuery: Null property causing TypeError when reading 'text'

Issue: I have a modal form that is loaded via an ajax call. Inside the modal, there is a span element containing an email address. I am trying to capture the value of this span element using JavaScript. Currently, I have a button click event that triggers ...