Definitions that are displayed dynamically when hovering over a particular element

I am seeking a way to implement popup definitions that appear when a div is hovered over. My website showcases detailed camera specifications, and I want users to see a brief definition when they hover over the megapixel class called '.mp'. One of my main goals is to be able to simply add the class '.mp' to any existing div or class, so that a popup will appear above it with the relevant information. This flexibility will allow me to reuse definitions in multiple locations on the site.

I am looking for an efficient solution as my current method is both inefficient and cumbersome.

In my site layout, the specifications are displayed using PHP and fetched from a database. Each specification is wrapped in '.spec_item'. The names of the specs are stored in an array which can be dynamically added to each div using a loop:

$sensor_db = array("mp", "sensor_size", "sensor_type", "light_sensitivity");

For example, this generates divs like:

<div class="spec_item mp">
    12.9
</div>
<div class="spec_item sensor_size">
    0.95X
</div>
<div class="spec_item sensor_type">
    APS-C
</div>

When a user hovers over any div with the class '.mp', I want the corresponding definition to display below it in a small popup, similar to the effect shown in my mockup image. I could achieve this by storing all definitions in another array and toggling between display none/block on hover. However, this approach would not allow me to easily apply the '.mp' class to any div and have the definition automatically show up, which is my desired functionality.

Any guidance or assistance on how I can achieve this would be greatly appreciated.

Answer №1

Perhaps this solution could be of assistance to you. It may require generating the appropriate HTML within your PHP script.

.tooltip-container {
  width: 200px;
  position: absolute;
  display: none;
  margin-top: 12px;
}

.arrow-up {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 5px 12px 5px;
  border-color: transparent transparent #a6a6a6 transparent;
  position: absolute;
  top: -12px;
  left: 15%;
}

.tooltip {
  border: thin solid darkgray;
  padding: 10px;
}

h5 {
  margin: 0;
  text-align: left;
}

.mp {
  width: 200px;
  height: 50px;
  border-radius: 5%;
  background-color: red;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  position: relative;
}

.mp:hover +.tooltip-container {
  display: block;
}
<div class="mp">
  <p>12.9</p>
</div>
<div class="tooltip-container">
  <span class="arrow-up"></span>
  <div class="tooltip">
    <h5>Hello World</h5>
    <span>Lorem ipsum dolor sit amet</span>
  </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

Ways to remove unwanted line breaks following PHP code within HTML documents

Currently working on building my blog, and I'm facing some challenges with the post div. It seems like every element within is automatically getting line breaks. Despite trying to float them or adjust padding, it's not giving me the desired layou ...

AngularJS postback method fails to trigger

Seeking assistance with my angularJS AJAX postback method. Below is the HTML code I have created: <html xmlns="http://www.w3.org/1999/xhtml" ng-app> <head runat="server"> <title></title> <script src="Scripts/angular.js ...

Troubleshooting JavaScript: Dealing with JSON Import Issues Involving Arrays of Objects

I'm encountering an issue while trying to import a JSON file that contains an array of blog-posts. Although all the data from the JSON file is successfully imported, I am facing troubles with accessing the Array of objects (edges). This specific code ...

Choosing particular contenteditable divisions using jQuery

Consider the following HTML structure for a specific type of blog post editor: <div class="entry"> <div class="title" contenteditable="true"> <h2>Title goes here</h2> </div> <div class="content" contenteditable ...

Encountering an NPM ELIFECYCLE error when attempting to start the node server with the

After following the instructions on deploying test-bot on IBM Watson from this link https://github.com/eciggaar/text-bot, I encountered errors while attempting to deploy the code locally using CLI foundry. My setup includes Node.js version 6.10.3 and npm ...

Utilizing Node.js and Express to call a function twice - once with the complete req.body and once with an empty body

Trying to articulate this may be a bit challenging, but I'll give it my best shot. I have an iOS app and Android app that both access the same node.js app through their respective web views. The iOS version is able to open the node.js app without any ...

When I attempt to run JavaScript code on the server, it fails to execute properly

When I run my code on my PC without putting it on the server, it works perfectly fine. However, when I upload it to the server and try to call it, I encounter the following error: Uncaught ReferenceError: crearLienzo is not defined at onload ((index): ...

Creating HTML content in a new window with Vue.js - a step by step guide

Recently, I encountered a problem with jsPDF regarding Unicode support in table generation. To work around this issue, I decided to utilize the browser's print feature instead. I achieved this by creating a new HTML document with the table and display ...

Routes for Express are throwing a 500 internal server error

My server is unable to locate the APIs that I have created in the API directory, which is resulting in a 500 internal server error. I have thoroughly checked routes.js and everything appears to be correct. Additionally, I have an error.js file for handlin ...

Having trouble getting my Jquery files to function properly

Take a look at my code: $('li.food').addClass('red'); The website is currently in production. I have created a jQuery file named hide.js in the app/assets/javascripts folder. This snippet is from my application.html.erb file: <he ...

Video Background, Adjusting Scale to Fit Height and Cropping Width as Necessary

Forgive me if this question has already been posed, but my search through various related discussions has not yielded the exact solution I'm looking for. My dilemma involves a 1280x720 video that I want to use as the background on my webpage. I need ...

Limit not reached by substring function

When the character limit exceeds 20 characters, the substring function extracts the first 20 characters typed in the input. It replaces any additional characters that are entered after reaching the max limit of 20. In my program, however, I am able to con ...

What advantages does event delegation in JavaScript offer compared to using jQuery's event handling?

Vanilla JavaScript: parentNode.addEventListener("click", function(event) { if (event.target === childNode) { code here } }); versus jQuery: $(parentNode).on("click", childNode, function() {}); ...

Issue encountered when attempting to retrieve elements within an HTA's IFrame

We are currently working on automating an Intranet site using HTA and JavaScript. To bypass the browser security settings, we have implemented an Iframe within the HTA itself instead of opening the site in a browser. Despite being able to successfully logi ...

If the width is below 767, the previous code will not be effective in jQuery

Challenge: How can I ensure that the code within the if statement only executes when the screen width exceeds 767px, and does not work if it is less than 767px? Using jQuery: $(document).ready(function() { $(window).resize(function() { if ($( ...

Encountering NPM install gyp errors in VSCode indicating that gyp is searching for Visual Studio

Running npm install on a local project has been quite challenging for me, as I keep encountering errors every time I try. Fortunately, some valuable information I found related to gyp and Python helped me make some progress. However, I'm currently fac ...

Javascript: recursive function fails to return existing value

I'm attempting to recursively loop through an array in search of a key that matches a specified regex pattern. Once the condition is met, the loop should halt and return the value of the key. The issue I am facing is that although the loop stops corr ...

I recently started delving into React Native and am currently exploring how to implement custom fonts in my application. However, I have encountered an error that is preventing me from successfully integrating

The Issue: The error I encountered only appeared after including font-related code (such as importing from "expo-font" and using "AppLoading" from "expo", and utilizing the "Font.loadAsync()" function). Error: Element type is invalid: expected a string (fo ...

Using Django to pass context data in a JsonResponse

Currently working on a webpage that incorporates filters to refine the displayed results. Upon triggering an Ajax call, the filters are transmitted to the Django backend for processing. The filtered data is then expected to be returned to the front-end. T ...

Strategies for preserving context throughout an Ajax request

In my project, I am looking to implement an Ajax call that will update a specific child element within the DOM based on the element clicked. Here is an example of the HTML structure: <div class="divClass"> <p class="pClass1">1</p> &l ...