Two elements with the same height with jQuery – all the time

Posted on

Question :

I’m taking advantage of a gallery system that I found on the web where, depending on the proximity of the mouse image, it widens proportionally.

Link: Image Gallery

Well, I have adapted the code to be responsive (and as you can see it is under construction) and there is no apparent problem. My problem starts when you change the resolution (either by zooming in on the browser, or by ‘spinning’ your tablet or cell phone). When you move the mouse or click (with touchs) on top of any image, in its resolution whose page was loaded, with the description nothing happens, but when you zoom in, you can already see all the misalignment of the description with the images . The height of the image is equal to the width. The width is relative.

Imagine a single solution to my problem: using jQuery. I program with a basic level – intermediate in this framework and I can do good things. But I have no idea how, when you change the resolution, you change the size of the description automatically. So far, I use this code to make them both equal:

$(".gallery-description").height( $(".gallery-thumbs li a img").height());

I hope someone can help me by improving the function or giving another suggestion! Thank you. 🙂


Answer :

You can use the jQuery function .resize() :

    $(".gallery-description").height( $(".gallery-thumbs li a img").height());

Example: FIDDLE

What this function does is trigger an event each time a difference in browser window resolution is detected. The bad side is that if you fire some heavy function, it will run at each pixel changed in screen size, which can affect performance.


Leave a Reply

Your email address will not be published. Required fields are marked *