Establish the dimensions of the element to match the dimensions of a responsive image

Currently, I am working on implementing flippable images on my website. It is important to me that the "back" of these images matches the dimensions of the front image. The challenge I am facing is that I have applied the Bootstrap img-responsive class to my images, which means the dimensions of the "back" image need to adjust dynamically to align with the "front".

This is the approach I envision (although I understand that the CSS code provided is not feasible, it serves to demonstrate my objective). I am utilizing ng-animate for transitioning effects and revealing the initially hidden "back" while concealing the front (which I have successfully achieved).

html:

<img class="front img-responsive" src="path/to/img.jpg" alt="">

<div class="back">Information about the image</div>

css:

.back {
    height: [current height of .front]
    width: [current width of .front]
}

Answer №1

To retrieve the dimensions of an image with jQuery, you can use the following script to set them for a specified div:

<img class="front img-responsive" id="img" src="path/to/img.jpg" alt="">
<div class="back" id="info">Information about the image</div>

$(document).ready(function() {        
var img = document.getElementById('img');
var width = img.clientWidth;
var height = img.clientHeight;
$("#info").width(width).height(height);
});

View DEMO

Answer №2

Just stumbled upon v1.3+ which includes added support, not sure if this is how it will be utilized...

Check out the plunker demo = http://plnkr.co/edit/utsD4RimPB6RSKXnAlLi?p=preview

<!DOCTYPE html>  
 <html ng-app>  
  <head>  
   <link rel="stylesheet" href="style.css">  
   <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.js"></script>  
   <style>  
    .mydiv {  
     height: {{height}}px;  
     width: {{width}}px;  
     background-color: red;  
    }  
   </style>  
  </head>  
  <body style="width:100%;height:100%;">  
   <div class="mydiv" ng-init="height= 100; width=200">&
lt;/div>  
   h: {{height}} w: {{width}}  
  </body>  
 </html>  

Another example can be found here:

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

Using Jquery in combination with Bootstrap modal and PHP for website

Upon submission of a form with numerous fields, I aim to display a bootstrap modal popup that prompts the user with a question before data is saved in the database. The user can respond with either "yes" or "no", leading to different actions being taken ba ...

Invoke a function in Angular when the value of a textarea is altered using JavaScript

Currently, I am working with angular and need to trigger my function codeInputChanged() each time the content of a textarea is modified either manually or programmatically using JavaScript. This is how my HTML for the textarea appears: <textarea class ...

The error message InvalidCharacterError is displayed when the attempt to create a new element using the 'createElement' method on the 'Document' object fails. This is due to the tag name provided ('/static/media/tab1.fab25bc3.png') not being a valid name

Hey everyone! I'm new to using React and I decided to try cloning Netflix by following a tutorial on YouTube. However, I've encountered an issue with rendering an image in a functional component. The error message I'm receiving is as follow ...

Tips for retrieving a single value from AJAX response data

I'm facing an issue where I am unable to retrieve a specific value from the data returned by an ajax call in order to make a decision. Below is my jQuery code that calls a PHP file. var displayMsg = $.ajax({ url:"displayMessage.php" }); displ ...

Unable to load more than one controller in a single partial view using AngularJS

I am having trouble loading a second controller to populate a select in my view. Despite my efforts, it just won't cooperate. This is the code snippet I'm using: app.js (function() { 'use strict'; angular .module('app.lazylo ...

Encountering an issue with HTMLInputElement when trying to input an integer value into a label using JavaScript

script code intext = document.getElementById("intext"); totalin = document.getElementById("totalin"); function myFunction() { var x = document.getElementById("totalin"); x.innerHTML = intext.toString(); } The snippet above shows a JavaScript functio ...

The function req.send('null') does not exist in the Node.js MySQL library

I am struggling with inserting a form into a MySql database. The values are stored in the database from the form, but the table displayed on the page does not refresh. I can only see the entries when I restart the server and revisit the page. Furthermore, ...

Which approach yields greater efficiency for an image gallery in a hybrid application?

I have plans to create a one-of-a-kind hybrid application that showcases an extensive collection of 70-100 captivating images and videos. My main concern revolves around the effectiveness and efficiency of Jquery mobile. So, should I opt for Angularjs an ...

The value of the jQuery data on click event handler becomes 'undefined' when used within an AJAX handler

When I click on the Positive or Negative buttons, a jQuery event handler function is triggered. Each button passes different data objects to the handler. However, within the AJAX POST handler, I am unable to access the data from the click event. $('# ...

Validating AngularJS form upon submission

In my AngularJS application, I am looking to incorporate a form validation that only displays errors after the user clicks the submit button. I want to avoid real-time validation as I type or upon exiting the field. Is there a method to achieve this? I an ...

Creating a Dual Y-Axis Chart with Two Sets of Data in chart.js

I utilized the chart.js library to write the following code snippet that generated the output shown below. My primary concern is how to effectively manage the labels on the horizontal axis in this scenario. CODE <!DOCTYPE html> <html lang="en"& ...

Display additional tiles within a compact container

I'm attempting to replicate the user interface used by foursquare! Positioning the map slightly off center like they have . I've figured out how to do one part but struggling with the second part. Initially, I loaded the map in a small div (t ...

Encountering an issue with retrieving the nth element using Python Selenium

<div class="booking-classes"> <h3 style="text-align: center;"> <p><a href="https://foreupsoftware.com/index.php/booking/20290#/teetimes" style="background-position:0px 0px;"><b> ...

Enhancing a popup with animated effects

I have been working on a popup that I want to add a subtle animation to. A fade effect seems like the perfect solution. Here is the code for the button: <a href="javascript:void(0)" onclick="document.getElementById('back_overlay').style.disp ...

There seems to be an issue with .children .val() returning an

I'm currently working on form validation, but encountering an issue where .val() is returning undefined. $(document).ready( function() { $('.formElem .input').each(function() { $(this).children(".inputField").on("keyup", function ...

Optimize your Number Selector to the maximum capacity

I am in search of a way to implement a max option on my function. I am contemplating whether using data attributes on the input is the best approach, but I have a feeling there might be a more straightforward solution in jQuery. It's important to note ...

Postback does not show updates made by JQuery

On Page_Load, I have two asp:BulletedLists - one is already filled with data while the other remains empty. Users have the ability to drag and drop <li>'s between these lists using the following function: function Move(element, source, target) { ...

Load HTML table values dynamically with Ajax post page load in PHP

My goal is to retrieve the connectivity status of available servers in a database on a PHP page. <tbody> <?php foreach ($data['servers'] as $server) { ?> <tr> <td class=""><?php echo $server->server_ ...

Is it possible to launch a browser window using javascript?

I need to open a new Internet Explorer browser window and I am currently using the following code: window.open("http://jsc.simfatic-solutions.com", "mywindow"); However, I would like to know how can I open a new IE window with a fresh session? ...

Utilizing Node.js, AngularJS, and MongoDB to build an interactive platform for online

As I ponder the compatibility of the MEAN stack (MongoDB, Express, Angular, Node) for constructing community websites, intra and/or extranet, I can't help but wonder its ideal use case. While options like Drupal and Liferay exist, I am intrigued to e ...