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.

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

Get the game using electron / determine the game's version via electron

I'm currently working on creating a game launcher using electron. There are two main questions that I have: What is the best method for downloading files from the client (specifically in AngularJS)? FTP or HTTP? How can I implement a system to detect ...

Troubleshooting a Problem with the Horizontal Scrollbar in Dynamic jqGrid

Currently, I am working on an ASP.net MVC project where I have implemented both dynamic and static jq grids. However, I am encountering an issue with the horizontal scroll bar in my application. To address this problem, I attempted to modify the overflow ...

Perhaps dividing numbers with regular expressions

My HTML textarea allows users to input numeric serial numbers in various formats - either inline, separated by any character, or in columns from an Excel file. The serial codes are either 14 characters long if the first character is "1," or always 15 char ...

Update the network name in the Axios request

Currently, I am utilizing Axios for handling both GET and POST requests. One question that has been on my mind is how to modify the network name: At present, the name serves as the parameter accompanying each request. However, I ponder whether it's f ...

Could there be a mistake in the way array combinatorics are implemented in JavaScript?

Having encountered the necessity for generating unique combinations when dealing with multiple arrays, I developed this script. While it functions as intended during the combination process, storing the result in a final array yields unexpected outcomes. ...

Interactive pop-up text box for data cells in a table

I have implemented a textarea in the td cell of each row within column 3 to store description specific to that row. The goal is for the current description in the td's textarea to be copied over to the textarea located inside #div_toggle when the user ...

Steps for implementing Onclick event within the <Script> tag to display a div:

I am currently working on a code that allows me to show and hide a <div> element after clicking on an advertisement from any website. This advertisement is generated by a script. How can I implement the onclick event within the <script> tag? T ...

Error encountered in Node/Express application: EJS partials used with Angular, causing Uncaught ReferenceError: angular is not defined

I seem to be missing something important here as I attempt to incorporate ejs partials into a single-page Angular app. Every time I try, I encounter an Uncaught ReferenceError: angular is not defined in my partial. It seems like using ejs partials instead ...

Having Trouble with Angular 6 Subject Subscription

I have created an HTTP interceptor in Angular that emits a 'string' when a request starts and ends: @Injectable({ providedIn: 'root' }) export class LoadingIndicatorService implements HttpInterceptor { private loadingIndicatorSour ...

Is it possible to host three separate web pages within a single Firebase project on Firebase Hosting?

Imagine I have set up a project called ExampleApp on Firebase. I've designed a page with a form for users to submit data, hosted by Firebase. The Cloud Firestore database is storing this submitted data when the user completes the form and hits submit ...

Utilize grids to ensure consistency in the width of every <li> element across the browser

Is there a way to make the ul element span the entire width of the webpage regardless of window size? http://jsfiddle.net/32hp1w5p/ ul { grid-column: 1/13; list-style-type: none; display: table; margin: 0 auto; } li { float: left; border: ...

Is it possible to continuously update a Google line chart by programmatically adding rows at specific intervals using an AJAX call

Is there a way to dynamically add rows to the Google line chart each time data is retrieved via an AJAX call at set intervals? This is the existing code: google.charts.load('current', {'packages':['line']}); google.charts. ...

Troubleshooting Issue with Query Functionality in MEAN App's Find Request

I'm facing some challenges while working with queries in my MEAN App. Specifically, I am attempting to retrieve data that matches the input entered into a search field: $scope.searchInput = function(search){ $http({ method: 'GET', url: ...

How do you sort an array using jQuery?

I am currently utilizing Ajax to retrieve XML data and populating form fields with the results. One of the fields on the form is numerical, and I would like to sort the results based on this number (from highest to lowest). How can I achieve this using jQ ...

Please display the Bootstrap Modal first before continuing

Currently, I'm facing a challenge with my JS code as it seems to continue running before displaying my Bootstrap Modal. On this website, users are required to input information and upon pressing the Save button, a function called "passTimeToSpring()" ...

My attempt at utilizing the regex function was unsuccessful

When attempting to use a regex function for matching tags in the title and caption, it appears to work fine. However, adding more matching tags causes the function to fail. Below is the code snippet that functions correctly without any issues: $(".si ...

Tips for concealing XHR Requests within a react-based single page application

Is there a way to hide the endpoint visible in Chrome's devtools under the network tab when data is fetched in React? Can server-side rendering solve this issue? ...

At what point are routed components initialized?

Here is a route setup I am working with: path: ':id', component: ViewBookPageComponent }, After adding this route, an error keeps popping up: Error: Cannot read property 'id' of null I haven't included a null check in the compo ...

Typescript implementation for a website featuring a single overarching file alongside separate files for each individual webpage

Although I've never ventured into the realm of Typescript before, I am intrigued by its concept of "stricter JS". My knowledge on the subject is currently very limited as I am just starting to experiment with it. Essentially, I have developed my own ...

Stop the form from being submitted using ajax if there are no values in the form fields

Having issues with a basic form. Struggling to prevent submission when fields are empty. Is there a straightforward way to validate the fields and stop the form from submitting? Below is the HTML form: <form method="post" name="simpleForm" id="simpleF ...