Preserving the original height of an element while its content undergoes modification

I have a unique image display setup where the main image is shown in full size to the left, while all other gallery images are displayed as thumbnails to the right. Here's how it's structured:

<div class='col-md-7'>
    <img src='something'>
</div>
<div class='col-md-5'>
    <ul>
        <li ng-repeat for all the images in the array>
            <img thumbnail for the image, on click it changes the source of the main image>
        </li>
    </ul>
</div>

My issue is that different image heights affect the overall height of the container and each thumbnail within the gallery. Is there a way to set an initial height for the gallery and maintain it even as users switch between images? I want the height to be dynamic based on screen size and ensure that multiple galleries on the same page remember their individual heights without static values.

As shown in the example, I'm using Bootstrap, AngularJS, and jQuery, so any solutions within these frameworks would be ideal.

Answer №1

Retrieve the initial offsetHeight of all img elements that you wish to maintain a constant height for, and then set their height property to this obtained value.

The specific line responsible for this operation in the code snippet below is:

img.style.height = img.offsetHeight + "px";

let gallery = [
  'https://picsum.photos/200/100/?image=1080',
  'https://picsum.photos/200/200/?image=1063',
  'https://picsum.photos/500/200/?image=1060',
  'https://picsum.photos/300/300/?image=1059',
  'https://picsum.photos/200/300/?image=1070',
  'https://picsum.photos/300/200/?image=1071'
];

let thumbs = document.getElementById("thumbnails");

for (let i = 1; i < gallery.length; i++) {
  thumbs.appendChild(thumbs.firstElementChild.cloneNode(true));
}

let img = document.querySelector(".col-md-7").firstElementChild;

gallery.forEach((url, i) => {
  thumbs.children[i].firstChild.src = url;
});

thumbs.addEventListener("click", event => {
  if (event.target.tagName === "IMG") {
    img.src = event.target.src;
  }
});

img.src = gallery[0];
img.style.height = img.offsetHeight + "px";
// The above last line is the code maintaining the initial height.
#thumbnails {
  list-style-type: none;
  padding: 0;
}

#thumbnails li {
  float: left;
  height: 35px;
  width: 50px;
  margin: 10px;
  background: #fde;
}

#thumbnails img {
  width: 100%;
  height: 100%;
}
<div class='col-md-7'>
    <img>
</div>
<div class='col-md-5'>
    <ul id="thumbnails">
      <li><img></li>
    </ul>
</div>

EDIT: (responding to a comment)

Based on my current understanding, an approach like the following can be taken:

HTML

Each gallery now has been encapsulated within a

<div class="gallery"></div>
. Thumbnail image sources are dynamically added using ng-repeat.

<div class="gallery">
  <div class='col-md-7'>
    <img src='something'>
  </div>

  <div class='col-md-5'>
    <ul class="thumbnails">
      <li ng-repeat="some-token">
        <img src="{{img.src}}"> 
               <!-- img.src represents the URL from each iteration in your data -->
      </li>
    </ul>
  </div>
</div>

<div class="gallery">
  <div class='col-md-7'>
    <img src='something'>
  </div>

  <div class='col-md-5'>
    <ul class="thumbnails">
      <li ng-repeat="some-token">
        <img src="{{img.src}}">
      </li>
    </ul>
  </div>
</div>

JS

This script iterates through each gallery in the galleries collection, ensuring that each gallery maintains its own scope.

let galleries = document.getElementsByClassName("gallery");

[].forEach.call(galleries, gallery => {
  let image = gallery.querySelector("img");
  image.style.height = image.offsetHeight + "px"; // Initial height

  let thumbs = gallery.querySelector(".thumbnails");
  thumbs.addEventListener("click", event => {
    if (event.target.tagName === "IMG") {
      image.src = event.target.src;
    }
  });
});

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

Steps for aligning a custom SVG icon in the middle of a Material-UI IconButton

Looking to create an icon button that resembles this design: https://i.sstatic.net/QCyZO.png My current progress is as follows: https://i.sstatic.net/NXfm0.png Below is the code snippet I have been working on: import { SvgIcon, IconButton } from '@m ...

How can I align a div in a vertical manner if it is displayed as a circle?

This is the creation I have come up with: This is the design I am aiming for: Below is the code I have written: <div id="OR"><span style=" vertical-align: middle;background:blue;">OR</span></div> Here is the corresponding CSS: ...

Error encountered when attempting to utilize the push() method on a two-dimensional

My goal is to arrange database data by their groupID (each with a different groupID in a separate sub-array). However, when I attempt to push the data into an array, I encounter the following error: TypeError: Cannot read property 'push' of unde ...

It appears that I may have erred in my Ajax syntax

I'm completely new to working with Ajax, and I've run into an issue that I can't seem to solve. Essentially, my goal is to replace a list item containing the word "Hello" with one that says "Goodbye": Here is the HTML code I have: <ul&g ...

Maximizing the potential of HTML5 email input fields with jQuery validate

As I work on validating an <input type="email"> using the jQuery validate plugin, I am experiencing a small issue. The validation itself is working well, but after entering an incorrect value and receiving an error message, the error message remains ...

Execute the command to load advertising scripts

Here is some code in HTML, JS, and CSS: $('.list li:nth-child(4)').after('<li class="new-elem"></li>'); li.new-elem {width:336px; height:280px; background:#faa} <script src="https://ajax.googleapis.com/ajax/libs/jquer ...

Unable to set child element to match parent element's height in Internet Explorer

Attempting to make a child element of a table cell inherit the height and width of the table cell through CSS. Below is the HTML provided: <tr> <td> <div class="yellowDiv"></div> </td> <td></td> ...

Analyzing various field data collectively

Is there a way to use jQuery to compare multiple input field values and trigger an alert message saying 'There are similar values' if any match is found? <input value="111"> //similar <input value="222"> <input value="111"> //s ...

"Exploring the Dynamic Duo: Web API and AngularJS

Delving straight into the issue: Within an AngularJs controller, there is a function that calls a factory-defined function. This factory function makes an API POST request which expects a '[FromBody]string' parameter. The problem arises at this ...

What could be preventing $watch from triggering despite setting objectEquality to true?

Within this code snippet, the $watch function is supposed to trigger the recalculateInfoBox function whenever the scores variable changes. However, it seems that nothing happens. I've tried adding true as the third argument of $watch, which usually s ...

Adjust div height to match the dynamic height of an image using jQuery

I'm facing an issue with my image setup. It has a width set to 100% and a min-width of 1024px, but I can't seem to get the 'shadow' div to stay on top of it as the window size changes. The height of the shadow div also needs to match th ...

Utilizing the Colon in URL parameter when making a jQuery AJAX request

Hey there, I'm currently working on fetching data from Envato using jQuery.ajax(). However, I'm encountering a problem because the URL parameter contains a colon, which the system does not accept. $('button').click(function(){ var ...

Iterating variables in Vue.js is reminiscent of the process in AngularJS

When working on my application using vue.js, I am interested in finding the last repeated element within a v-for directive. I understand that in angularjs, there is a similar concept using the $last loop variable in its ngRepeat directive: <div ng-repe ...

Ensure that all necessary fields are filled out by applying 'required' validation to each one

In the form I'm working with, there are various input elements like text boxes, date pickers, and drop downs. My goal is to make all other fields required once a value is entered in any field. For example, if a value is entered in a text box, then all ...

Text obstructed by a sticky sidebar in Bootstrap

Currently, I am utilizing bootstrap to create a webpage featuring an affixed sidebar. However, I have encountered two issues when adjusting the screen size to that of a phone: The sidebar obstructs the text behind it, halting its movement beneath the sid ...

Loading more Wordpress posts on click using jQuery AJAX request

After creating a custom theme from scratch with the _S starter theme, I encountered a problem trying to fetch Wordpress next posts using an ajax request when clicking on the read more button. Despite referring to multiple articles such as the ones listed b ...

Utilizing jQuery and AJAX for submitting multiple POST requests

Experiencing a problem with posting data via AJAX using a drag and drop interface. The data is being sent to the POST URL as intended, but there's a recurring issue where the POST request occurs twice, causing the processing URL to handle the data aga ...

Angular: Issue with ng-model directive when used with `<span>` tag

Currently, I am facing a challenge while working on a web application that combines bootstrap and angular.js. The issue revolves around setting the default value of a span within the HTML code of one of the pages. In my previous experience, I utilized ng- ...

Troubleshooting problem with sorting in Angular 4 material header

Using Angular 4 material for a table has presented me with two issues: 1. When sorting a table, it displays the description of the sorting order in the header. I would like to remove this. https://i.sstatic.net/5bHFO.png It displays "Sorted by ascending o ...

Individual User's Time Slot

Greetings! I am in the process of developing a web application where users can seek assistance for their problems: One challenge I am facing is how to inform users of the time a question was posted in their specific time zones. For instance, if I post a ...