Determining the container height for every two-image row

I am currently working on a project for this website.

My focus right now is on resizing vertical images using the following script:

function Gallery(selector) {
  this.add_module = function (type, image) {

    var portrait_text = image.next('.portrait_text');

    var container = $('<div />', {
      'class': 'gallery_container'
    }).append(image).append(portrait_text);

    if (type == 'horizontal') {
      var h_ar = image.attr('height') / image.attr('width');
      var c_width = selector.width();
      var c_height = selector.width() * h_ar
      container.css({
        'width': c_width,
        'height': c_height
      })
    }
    if (type == 'vertical') {
      var c_width = v_width;
      var c_height = v_height
      container.css({
        'width': Math.floor(v_width),
        'height': v_height
      })
    }
    container.css({
      'float': 'left',
    })
    container.find('img').attr({
      'width': '100%',
      'height': '100%'
    })
    container.attr('ar', c_height / c_width)
    container.appendTo(selector);

    //container.children('img').fitToBox();
  }

  this.resized = function () {
    $('.gallery_container').each(function () {
      if ($(this).attr('ar') >= 1) { // vertical
        $(this).css({
          'width': sel.width() / 2,
          'height': sel.width() / 2 * $(this).attr('ar')
        })
      } else { // horizontal
        $(this).css({
          'width': sel.width(),
          'height': sel.width() * $(this).attr('ar')
        })
      }
    })
  }
  var _this = this;
  var gutter = 0;
  
  var v_counter = 0;
  var w_pxls = 0;
  var h_pxls = 0;
  var v_ar;

  selector.children('img').each(function () {
    if (parseInt($(this).attr('width')) < parseInt($(this).attr('height'))) {
      v_counter++;
      h_pxls += $(this).attr('height');
      w_pxls += $(this).attr('width');
      v_ar = $(this).attr('height') / $(this).attr('width')
    }
  })

  var h_avrg = Math.floor(h_pxls / v_counter);
  var w_avrg = Math.floor(w_pxls / v_counter);
  var v_width = Math.floor((selector.width()) / 2);
  var v_height = v_width * v_ar;
  var sel = selector;
  selector.children('img').each(function () {
    if (parseInt($(this).attr('width')) > parseInt($(this).attr('height'))) {
      _this.add_module('horizontal', $(this));
    } else {
      _this.add_module('vertical', $(this));
    }
  })
  $(window).bind('resize', _this.resized);
}

var gallery = new Gallery($('#gallery_images_inner'));

http://jsfiddle.net/mZ2Ks/

The issue I am facing with the script is that it sets all the containers to the same height (based on the last image on the page), causing the first images to resize poorly. As seen in the example above, all row of 2 images have a fixed height of 613px.

Is there a method to individually control each pair of images containers to calculate their own height based on the respective images? Currently, it seems like the script takes the height from the last resized image and applies it to all other containers

Applying height: auto instead of 100% won't resolve the issue as it will not fit the images to the height of the vertical container.

How can I resolve this script-related problem?

Answer №1

A solution exists for your problem, although it may not be the most straightforward. Your gallery script calculates an average aspect ratio for all images inside, making it difficult to alter this behavior easily.

However, there is a workaround you can implement: place each pair of images in their own separate gallery!

var gallery1 = new Gallery($('#gallery_images_inner1'));
var gallery2 = new Gallery($('#gallery_images_inner2'));
var gallery3 = new Gallery($('#gallery_images_inner3'));

For more details, visit http://jsfiddle.net/mZ2Ks/2/. Please note that I had to tidy up your HTML code - ensure that you copy the clean source code directly rather than the "javascript-affected" code from tools like Firebug.

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

What is the best way to interact with the dynamic Twitter follow button using Selenium and Python?

Trying to interact with Twitter's follow button can be challenging, especially when it is dynamic and has attributes that are not well-documented in the Selenium documentation. Below is a snippet of the HTML code for the button: Below you will see my ...

Issue with data-ng-class function not being invoked

I'm currently working on a simple Angular project where I need to dynamically color list items based on a function called in data-ng-class. Below is an excerpt from my HTML file: <div> Rooms:<ul style="list-style:none;"> < ...

Managing incoming requests in Django

I'm fairly new to working with Django and recently encountered some challenges with my requests. I've been attempting to make a POST request from a Django form using json and AJAX. Here is the code snippet: form.py class PostForm(forms.Mode ...

Enhance your design with Bootstrap 4's innovative spacing

Recently started using Bootstrap. Here's the HTML code I have. How can I achieve consistent spacing between all my box elements without causing any of them to wrap on larger screens like laptops and desktops? In the past, I would use CSS-grid for this ...

Using JavaScript to manage form input values in React

I am currently coding a basic application using NextJS and bulma CSS. The snippet below shows the form I am working on: const MyPage = () =>{ const [firstName, setFirstName] = useState('') const [secondName, setSecondName] = useState('&ap ...

Is there a way to programmatically toggle a button's enabled state depending on the content of a text input field?

I want to create a functionality where a button will be enabled if the value in a textbox is 'mypassword'. If it's not equal to 'mypassword', then the button should be disabled. I've attempted the code below, but it doesn&apos ...

Challenge with SSRS report's cross-origin problem

I am currently facing a challenge while integrating an SSRS report into my Angular application. Below is the code snippet I am working with: Template: <div panel-charts style="width:100%:height:100%" </div> Directive: a ...

The $route.reload() function seems to be ineffective in Internet Explorer

I'm currently using AngularJs to develop an application. The issue I am encountering is related to data not being refreshed in IE, even after executing the $route.reload() function. Strangely enough, this problem only occurs in Internet Explorer and w ...

Unable to dismiss a Popup Dialog Box using Selenium Webdriver in Python(Element is not visible at the moment and cannot be interacted with)

I am facing difficulty closing a jQuery dialog box using Selenium Webdriver. See my code below. I am currently using Selenium WebDriver version 2.44.0 and Firefox version 33. Any suggestions would be greatly appreciated. from selenium import webdriver fro ...

Combining rows and columns in Flexbox design

https://i.stack.imgur.com/tDLii.png I'm able to easily create this layout using the float property, but I'm having some difficulties achieving the same with flexbox. CSS: .a { background: red; float: left; width: 30%; ...

Modify the indentation format in CSS/JS

When the Tab key is pressed in an HTML page and the tabbed link gets highlighted, is it feasible to customize the style of that highlight? ...

What is the process for retrieving the component along with the data?

As I work on compiling a list of div elements that require content from my firebase cloud storage, I find myself unsure about how to effectively run the code that retrieves data from firebase and returns it in the form of MyNotes. Consider the following: ...

Django and Ajax working together with GET requests

Despite having experience in working with Python, Django, and REST framework, I am facing a challenge when trying to make an Ajax request. No matter how many similar questions I come across, I just can't seem to solve the problem at hand. The situatio ...

Create a request for an update by utilizing Axios within a React and Material UI environment

I am completely new to React, and my experience with CRUD functionality is limited to ASP.NET MVC. I'm feeling a bit lost as none of the tutorials I've come across seem to cater to my specific situation. The tips I received previously were helpfu ...

Validation of JSF field with minimum length requirement

Looking for a solution for my registration form where there is an optional field that requires validation if a value is entered – but only if it is more than 2 characters long. How can I bypass the validation if the user leaves the field empty? <h:in ...

An error was encountered: The object 'object object' does not have the 'on' method available

I want to experiment with this website: However, I am encountering an error without any events triggering. I am confused why the method "on" is not being found even on page load. <head> <link href="css/scrollable-horizontal.css" rel="styleshe ...

A Guide to Displaying HTTP Error Messages on the Angular Login Page

When encountering a form validation failure on my login page, I utilize ngMessage to display an error message. However, I now want this div to be displayed in the event of an HTTP 500 error. While I can retrieve the corresponding error message when an HTTP ...

Calculate the length of a JSON array by using the value of one of its

What is the most efficient way to obtain the length of a JSON array in jQuery, based on the value of its attribute? As an illustration, consider the following array: var arr = [{ "name":"amit", "online":true },{ "name":"rohit", "online":f ...

How to make Angular 5 wait for promises to finish executing in a for loop

My task involves working with an array like this: arr = ['res1', 'res2', 'res3']; For each value in the arr, I need to make an API call that returns a promise. arr.forEach(val => this.getPromise(val)); The getPromise me ...

Obtaining an image from a URL, saving it, and then animating it? That definitely

Looking to create a dynamic animation with images that update every 15 minutes from the following URL: How should I go about storing and looping through the most recent 24 images for the animation? Is using a MySQL database the best option or are there o ...