Display an image with HTML

My current project involves creating a chrome extension that will display a box over an image when hovered, while also zooming the image to 1.5 times its original size. In my research, I came across a similar example.

.zoomin img {
  height: 200px;
  width: 200px;
  -webkit-transition: all 2s ease;
  -moz-transition: all 2s ease;
  -ms-transition: all 2s ease;
  transition: all 2s ease;
}
.zoomin img:hover {
  width: 300px;
  height: 300px;
}
<div class="zoomin">
  <img src="http://www.corelangs.com/css/box/img/zimage.png" title="All you need to know about CSS Transitions " />
</div>

However, for my specific project, I needed the box to appear without zooming the image upon hover. Through some experimentation with this Using only CSS, show div on hover over <a> code snippet, I was able to achieve this.

main.js

div {
    display: none;
}

img:hover + div {
    display: block;
 height : 200px;
    width : 300px;
}

The challenge now is dynamically adjusting the size of the image based on what we are hovering over. Is there a way to automatically create a div that is 1.5 times the dimensions of the hovered image? Any suggestions or help would be greatly appreciated.

I have attached a screenshot below for reference. https://i.sstatic.net/BD85o.png

Answer №1

  img:hover  div {
        display: block;
var image = document.getElementById('imageID'); 
// fetch the dimensions of the picture using its ID
var widthValue = image.clientWidth;
var heightValue = image.clientHeight;
        height : width * 1.5;
        width : height * 1.5;
    }

Answer №2

To fix the issue, simply eliminate the + symbol that is targeting the immediate next div element after the img.

You might want to attempt this instead:

img:hover ~ div
{
//insert your desired height and width values here
}

Answer №3

It seems like the solution you were looking for.

I'm not sure if CSS alone can achieve this (but I'd be happy to be proven wrong).

I've created a for loop to add an event listener for mouseover and mouseout actions on images within the .zoomin class. It dynamically changes the image source based on the mouse actions.

var zoominSel = document.querySelectorAll(".zoomin img");
var zoomContSel = document.querySelector(".zoomcont img")

for (let i = 0; i < zoominSel.length; i++) {
  zoominSel[i].addEventListener("mouseover", function(event) {
    zoomContSel.setAttribute('src', event.target.getAttribute('src'));
    zoomContSel.style.width = event.target.offsetWidth + "px";
    zoomContSel.style.height = event.target.offsetHeight + "px";
    zoomContSel.parentElement.style.top = event.target.offsetTop + "px";
    zoomContSel.parentElement.style.left = (event.target.offsetLeft + event.target.offsetWidth + 2) + "px";
  });
  zoominSel[i].addEventListener("mouseout", function(event) {
    zoomContSel.setAttribute('src', '');
  });
}
body {
  margin: 0;
}
.zoomin img {
  max-width: 200px;
}
.zoomcont img[src=""] {
  display: none;
}
.zoomcont {
  z-index: 1000;
  position: absolute;
  transform: scale(1.5);
  transform-origin: 0 0;
}
<div class="zoomin">
  <img src="http://www.corelangs.com/css/box/img/zimage.png" />
</div>
<div class="zoomin">
  <img src="http://usabilitygeek.com/wp-content/uploads/2013/07/free-fonts-for-commercial-personal-use.jpg" />
</div>


<div class="zoomcont">
  <img src="" />
</div>

I trust that you will find this useful.

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

The issue with VS Code is that it is running the wrong file instead of

Whenever I try to run my Python file, it keeps opening the previous HTML file instead. Despite clicking "run" on my Python file in VS Code, the terminal is not executing (as it normally does), and instead VS Code continues to open the previously opened HT ...

"Pairing Angular's loader with RxJS combineLatest for seamless data

Hey there! Currently, I'm working on enhancing my Angular application by implementing a global loader feature. This loader should be displayed whenever data is being fetched from my API. To achieve this, I am utilizing ngrx actions such as fetchDataAc ...

The IDs and classes of HTML elements

I have two different implementations of a livechat script. On my sandbox site, the livechat is fixed to the bottom right of the page and scrolls with the window. However, on my live site (where this specific code comes from), it is attached to the footer. ...

Troubleshooting PHP ReadFile Problems Caused by Spaces

Currently facing a PHP issue that has me stuck. I have a download.php file which grants secure access to downloads stored in a private server folder, one level above httpd. The code snippet is as follows: $Document = new Documents($DID); ...

Switching the background image dynamically in Vue.js upon page access

My goal is to update the 'background-image' when the page is accessed. export default { created () { document.getElementsByTagName('html')[0].style.background = "url('../assets/background-blur.png') center" } } //Logi ...

Having an Issue with the jQuery .get Method

Here is some HTML code I have <ul> <li> <a href='#' class='approch' alt='1' > 111111 </a> </li> <li> <a href='#' class='approch' alt='2' > 222222 < ...

Validation message causing Bootstrap form inline to lose center alignment

Could someone please assist me with form inline in Bootstrap? I am trying to ensure that all inputs are of the same height (or centered), but I'm facing an issue when a validation message is displayed. DEMO: http://codepen.io/Tursky/pen/gpvgBL?editor ...

Sending properties through the React Router to a specific component

Within my component, I have a material table that triggers a function when the edit button is clicked: //MaterialTable.js ... const handleEdit = (e, Data) => { console.log(Data) return(<EditFunction id={Data.id} />) ... The purpose of ...

Is it possible to turn off Angular CLI ng build linting for a specific directory?

I am facing an issue with a specific directory in my project template that I want to exclude from linting. Despite excluding it in both tsconfig and eslint, running eslint works fine but when using ng build, the directory is still included in linting and e ...

A guide on identifying the data type of a value entered into an HTML table using JavaScript

Currently, I am tackling a contenteditable HTML table challenge. My goal is to enforce the insertion of only numeric values while alerting the user if they attempt to input strings or non-numeric characters. Can anyone provide guidance on how to achieve th ...

Incorporating fresh components and newly defined attributes in Angular

Is there a way for me to click on a new component button, specify a name, description, select type, and add attributes such as default value and type? I need all this information to be saved and for the new button to appear in the drag-and-drop section. ...

Modify the content of an iframe within a specific element

$(document).ready(function () { $('#keywordframe').contents().find("#step12").html(''); $('#keywordframe').contents().find("#step12").html('11'); $('#keywordframe').contents().find("#step13").ht ...

Update the Knockout JS viewmodel from an external source

Question: How can I replace an entire object within my viewmodel that was created with the knockout mapping plugin, and ensure that the controls bound to that object are updated accordingly? Additional Information: I am currently using knockout in conjun ...

Images displayed on cards using BOOTSTRAP

I'm interested in creating cards with categories such as Men, Women, and Kids. I envision these cards to have a design similar to the ones shown here. My goal is for users to be able to click on one of the cards and be directed to either the Men or W ...

Tips on implementing an AJAX request using JSON data in both JQuery and Golang

I have been attempting to make an ajax call using JQuery, but I am encountering an issue with receiving an empty response. Interestingly, when I perform the same action via curl, it is successful. Below is my JS code: time = new Date($.now()); requestJSON ...

Generate a collection of information by gathering metadata from Xray

I've been struggling to populate an array with metadata retrieved using Xray. The issue arises when the function is called by an API endpoint on my server to fetch links from my application. My main challenge seems to be related to promises, as there ...

Issues with Converting JSON to HTML Table using JavaScript

Issues with Converting JSON to HTML Table Using JavaScript I've been trying to create a function that will display the contents of a JSON file in an HTML table. However, I keep getting an undefined error. Despite being able to see the data displayed ...

Tips for adjusting the hue

When using the jQuery UI accordion, I noticed that when all the accordions are collapsed, the mouse over the collapsed sections appear in a different color. The current color is quite subtle and can be difficult for some of my users to notice when hoverin ...

Expanding the rowspan within a table column has the effect of reducing its overall width

I have created a table with two rows, where the first row has three columns and the second row has two columns. The middle column in the first row has been set to rowspan="2". However, the issue is that it appears smaller than its intended width. .kolon ...

Eliminate the CSS triggered by a mouse click

Having some difficulty toggling the switch to change the background color. Struggling with removing the applied CSS and getting it to toggle between two different colored backgrounds. Code Sample: <!Doctype html> <html lang="en"> <head> ...