Want to learn how to display a description below a photo when you hover over it in a photo gallery?

I am currently creating a basic photo gallery where hovering over a thumbnail displays an enlarged photo below it.

My goal is to also have text displayed inside a white text box when hovering over each thumbnail, with unique descriptions for each. I need help with a JavaScript code as I am not very familiar with jQuery.

Here is the code I have so far. Any assistance would be greatly appreciated!

<!DOCTYPE html>

<head>
<title>Photo Gallery</title>

<style type="text/css">
body {

margin-top: 100px;
}


input {
    height: 40px;
    word-wrap: break-word;
}


.thumbnails img {
height: 100px;
border: 4px solid #151515;
padding: 1px;
margin: 0 10px 10px 0;
margin-top: 40px;
}


.thumbnails img:hover {
border: 4px solid #00ccff;
cursor:pointer;
}

.preview img {
border: 4px solid #151515;
padding: 1px;
width: 800px;
}


a:hover + input {
    display: block;
}


</style>

</head>
<body>

<body bgcolor="#CCCCCC">


<form name="bgcolorForm" > 
<select onChange="if(this.selectedIndex!=0)
document.bgColor=this.options[this.selectedIndex].value">
<option value="choose">Set background color &nbsp; &nbsp;
<option value="c8e4f8">Blue
<option value="CCCCCC">Grey
<option value="FFFFFF">White
<option value="FBFBEF">Cream
</select>
</form>


<br>
  <form id="font_form">
 <select id ="font" onChange="setFont()">
<option value="choose">Set font style 
     <option style="font-family: 'Verdana'">Verdana</option>
     <option style="font-family: Arial">Arial</option>
     <option style="font-family: 'Times New Roman'">Times New Roman</option>
  </select> 
  </form>



<div class="preview" align="center">
    <img id="preview" src="http://i60.tinypic.com/2qjj62b.jpg" alt="No Image Loaded"/>
</div>
</br>
</div>


&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; 

<input style="width:800px" id="Text" type="text" float="right" value="Text"/>


<div class="gallery" align="center">


<div class="thumbnails">
    <img onmouseover="preview.src=img1.src" id="img1" src="http://i60.tinypic.com/2qjj62b.jpg" alt="Image Not Loaded"/>
    <img onmouseover="preview.src=img2.src" id="img2" src="http://i60.tinypic.com/mb4c21.jpg" alt="Image Not Loaded"/>
    <img onmouseover="preview.src=img3.src" id="img3" src="http://i61.tinypic.com/35avvpw.jpg" alt="Image Not Loaded"/>
    <img onmouseover="preview.src=img4.src" id="img4" src="http://i60.tinypic.com/29qnjme.jpg" alt="Image Not Loaded"/>
    <img onmouseover="preview.src=img5.src" id="img5" src="http://i62.tinypic.com/zkmvd2.jpg" alt="Image Not Loaded"/>
    <img onmouseover="preview.src=img6.src" id="img6" src="http://i61.tinypic.com/oqezus.jpg" alt="Image Not Loaded"/>
    <img onmouseover="preview.src=img7.src" id="img7" src="http://i57.tinypic.com/1tx6oj.jpg" alt="Image Not Loaded"/>  
    <img onmouseover="preview.src=img8.src" id="img8" src="http://i58.tinypic.com/143onsj.jpg" alt="Image Not Loaded"/>
    <img onmouseover="preview.src=img9.src" id="img9" src="http://i61.tinypic.com/2l16qf.jpg"  alt="Image Not Loaded"/>
    <img onmouseover="preview.src=img0.src" id="img0" src="http://i61.tinypic.com/21l0own.jpg"  alt="Image Not Loaded"/>


</div></br>

<script type="text/javascript">
var images = document.getElementsByClassName('thumbnails')[0].getElementsByTagName('img');
for (i = 0; i < images.length; i++) {
    images[i].onmouseover = function () {
        document.getElementById('preview').src = this.src;
    }
}


function setFont()
{
    var selectFont = document.getElementById("font");
    if (selectFont) {
        var selectFontValue = selectFont.options[selectFont.selectedIndex].value;
        if (selectFontValue == "Verdana") {
            document.getElementById("Text").style.font = "20px Verdana, sans-serif";
        }
        else if (selectFontValue == "Arial") {
            document.getElementById("Text").style.font = "20px Arial,sans-serif";
        }
        else if (selectFontValue == "Times New Roman") {
            document.getElementById("Text").style.font = "20px Times New Roman,serif";
        }
        else {
            document.getElementById("Verdana").style.font = "20px Verdana, sans-serif";
        }
    }
}
</script>


</body>
</html>

Answer №1

To implement this functionality, it is recommended to utilize data attributes. Here is an example of how you can achieve it:

<img src="http://lorempixel.com/100/100/nature/6" 
     data-full="http://lorempixel.com/300/300/nature/6"
     data-small="http://lorempixel.com/100/100/nature/6"
     onmouseover="this.src=this.getAttribute('data-full');"
     onmouseout="this.src=this.getAttribute('data-small')"
     />

http://codepen.io/anon/pen/nubmo/

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

Utilize jQuery to run a script once everything is prepared and loaded

My website utilizes ajax technology, and within the ajax page, there is a piece of javascript code that needs to run only after the entire ajax page has loaded. I have attempted to use the following: $( '#ajaxdiv' ).load(function() { } However ...

``There seems to be a problem with the ngb time picker when using the up and

Currently, I am utilizing Bootstrap 4 and NG Bootstrap time picker for a project in Angular 10. Despite correctly adding all the code, I have encountered an issue where the up and down arrows on the time picker are not functioning as expected. Below is a s ...

What is the best way to align these two divs centrally within a container set to display as inline-block?

I can't seem to get these two floated left divs centered within a container that has a height set to auto and a display of inline-block. The container has a max-width of 1600px, but when the window is stretched wider than that, it stays aligned to the ...

The iframe is set to take the source URL from url.com/id using the "id" parameter

Currently, I am attempting to set the src of an iframe by extracting the URL from the toolbar address. The URL consists of the domain plus an ID, which happens to be the same as the YouTube ID. For instance: www.youtube.com/watch=id In my case, my domain ...

Mastering Interpolation in React with TypeScript is essential for creating dynamic and interactive UI components. By leveraging the

Incorporating and distributing CSS objects through ChakraUI presents a simple need. Given that everything is inline, it seems the main issue revolves around "& > div". However, one of the TypeScript (TS) errors highlights an unexpected flagging of ...

Guide on implementing ES6 script's template literals

I've been tackling a template literal question on hackerrank. It's working smoothly on my local IDE, but encountering an error on the Hackerrank IDE. Here's the code to add two numbers and print the result using a template literal: const sum ...

I am facing an issue where an AJAX post to Express is not returning any data to req.query. I have tried various solutions but nothing seems to

I have encountered an issue with my setup where the body is empty when sending data through ajax. In Chrome's network tab, I can see the post and content with the correct payload: {"EventName":"asd","PrivacyLevel":1,"TypeInt":1,"ExpectedDate":"asd"," ...

Using AngularJS forEach to assign properties to objects within a found set in ng-repeat

I am using an ng-repeat to display items from a JSON file, and they can be filtered based on user input. <tr ng-repeat="i in filteredItems = (iso3166 | filter: {alpha_2: isoQuery})"> Everything is working as expected. Each item in the "iso3166" gro ...

Tips for maximizing the effectiveness of the .bind(this) method in Meteor js

Hey there, I've got a question for you. How do we go about using the bind method in Meteor? Take a look at this code snippet below. It feels like there's some repetition going on that bothers me. Thank you so much for sharing your thoughts! Bi ...

Wordpress Sub-Menu Being Eclipsed by Banner

After upgrading my Wordpress to version 3.5.1, I noticed a problem on my site where the Submenus are loading but quickly getting hidden behind the Banner. Despite trying various modifications in the CSS file, the issue remains unresolved (and might have wo ...

Show specific content based on which button is selected in HTML

I am working on a project where I have three buttons in HTML - orders, products, and supplier. The goal is to display different content based on which button the user clicks: orders, products, or supplier information. function showData(parameter){ i ...

Automatic Submission based on Checkbox Selection

Creating a user-friendly interface for project admins is my goal, but I am encountering difficulties in displaying all tasks. The data is retrieved from the database and put into a table, with each task having a status represented by a binary value - 1 f ...

Learn how to incorporate latitude and longitude coding into PHP to display a map icon that correctly redirects to the desired URL when clicked

I am in need of a table that includes buttons for adding, editing, deleting, and mapping. Everything works fine so far, but when I click on the map button, it should take me to Google Maps with coordinates linked from a MySQL database containing latitude ...

Checking for an exact value using the includes() method in JavaScript - a comprehensive guide

In order to populate checkboxes based on a string delimited with pipes, I have been using the includes() method. However, I am encountering an issue where items with similar names are both marked as true because they share the same string, even if they are ...

Working with JQuery to extract and append data stored in a JSON object

I am working with the following JSON data: [ { "MOD_AXL": 0, "MOD_CDS_ID": 110000168, "MOD_CV": 0, "MOD_CV_CTM": null, "MOD_ID": 168, "MOD_MFA_ID": 514, "MOD_PC": 1, "MOD_PCON_END": 199007, "MOD_PCON_START": 196303, ...

When using express and passport-local, the function `req.isAuthenticated()` will typically return a

I'm seeking some insight into my current situation. I've noticed that whenever I call req.isAuthenticated() in an app.router endpoint, running on port 3001 via the fetch API, it always returns false. It seems like the connect.sid is not being pro ...

Exploring the depths of nested object arrays and navigating through historical indexes

I am working with nested object arrays within an array and looking to determine the path of a specific key. For instance: const dataList = [ [ [{id: 100,name: 'Test1'}, {id: 120,'Test12'}], [{id: 101,name: 'Test1&apo ...

406 error is triggered by GET parameters

Here is a PHP script I have: <!DOCTYPE html> <html lang="es-ES" xmlns="http://www.w3.org/1999/xhtml" xml:lang="es-ES"> <head> <meta charset="iso-8859-1" /> </head> <body><p><?php if(isset($_GET['echo& ...

"Optimizing navigation with dynamic link routing in AngularJS

I am working on a list of apartments displayed using ng-repeat. I need each apartment to be a clickable link that directs the user to view more details about that specific apartment. However, I am facing an issue where the links are not being repeated dyna ...

Error: The variable "Set" cannot be found in the react.js code, specifically in Safari browser

An issue has been identified in Safari where a ReferenceError is thrown: "Can't find variable: Set" when using react.js. This error occurs only in Safari, while all other browsers work perfectly fine. The error message points to the main.js file which ...