Adjust element sizes in real time with Angular 5

In my current setup, I have 2 interconnected div elements. The left div contains a list of images while the right div displays the details of each image. By clicking on an image in the left div, the right div expands and shows the corresponding information. However, I am facing a challenge with the positioning of the divs.

My goal is to dynamically adjust the heights of both divs so that they always match in size. This means that regardless of the content, whether there is only one large image on the left or multiple images resulting in different heights, I want the divs to be consistent in height. I attempted a solution but it did not yield the desired result:

leftHeight: number;
rightHeight: number;

ngDoCheck() {
    if (window.innerWidth < 1336 && this.openDetails) {
      this.chRef.detectChanges();
      if (this.leftElement.nativeElement.clientHeight > this.rightElement.nativeElement.clientHeight) {
        this.rightHeight = this.leftElement.nativeElement.clientHeight;
      } else {
        this.leftHeight = this.rightElement.nativeElement.clientHeight;
      }
      this.chRef.detectChanges();

    }
  }

Here is the HTML structure being used:

<div class="wrapper">

 <div class="left-part" #leftElement [style.height.px]="leftHeight - 40">
  <!-- Images from ngFor loop -->
 </div>

 <div [@focusPanel]='smallScreen' *ngIf="openDetails" #rightElement class="right-part" [style.height.px]="rightHeight - 65">
  <!-- Details of each image -->
 </div>
</div>

I am seeking suggestions for a more effective approach to handle this issue and ensure the dynamic adjustment of div heights based on their content.

UPDATE: While my initial implementation sets the height of the elements initially, it fails to recalculate and adjust the height dynamically as the content changes.

Answer №1

Perhaps this suggestion could be of assistance. Have you attempted invoking your function ngDoCheck within the ngAfterViewChecked event?
It may look something like this:

 class CustomComponent implements AfterViewChecked {

    constructor(private cdRef:ChangeDetectorRef) {}

    ngAfterViewChecked() {
       this.ngDoCheck();
    }

Answer №2

To add scroll bars to the right div, include the following CSS:

.scrollBarClass {
  max-height: 500px;
  overflow-y: scroll;
}

Then, add the class to your right div:

<div class="scrollBarClass"> </div>

Updated solution:

If both divs have dynamically set heights, you can simply apply the

<div class="scrollBarClass"> </div>

class to both divs to enable scrolling when they exceed their height limits.

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

Encountering a problem with vis js events

While constructing a timeline in my vue.js application, I opted to utilize vis.js. Unfortunately, I encountered some issues when attempting to incorporate events. Initially, setting @drop="myDropCallback()" did not trigger the function upon dropping an ite ...

Issue with PrimeNG overlaypanel displaying error message "Functionality of this.engine.setProperty is not available"

After importing OverlayPanelModule as @NgModule in parent.module.ts, I added the following code in child.component.html: <p-overlayPanel [dismissable]="false" #overlay> Content </p-overlayPanel> However, upon testing, I encountered the error ...

Difficulty transmitting Heroku environment variable to Angular CLI application using Node.js

Currently, I am tackling a project that requires passing a stripe key as JSON within Angular. I've stored the key in Heroku config vars and have been attempting to pass that value through the Node.js backend to Angular using the process.env.STRIPE_KE ...

The current URL in the browser does not change

My HTML form has a JavaScript function that involves handling the window.location.href. I successfully remove two unwanted query string parameters from the source using this function, as shown in the first and second alert screenshots. However, I encounte ...

Recursive type analysis indicates that the instantiation of the type is excessively deep and may lead to potential infinite loops

In the process of developing a Jest utility, I have created a solution where an object mock is lazily generated as properties are accessed or called. Essentially, when a property is invoked like a method, it will utilize a jest.fn() for that specific path ...

A Step-by-Step Guide to Setting Up and Utilizing V-Calendar in Vue.js

I am currently trying to incorporate the V-Calendar library into my Vuetify application. Up until now, the app was working fine, but I seem to have hit a roadblock with the correct installation of the V-Calendar library. Although no error messages are bei ...

Having trouble with NPM build getting stuck in Azure DevOps

There seems to be a problem similar to the one discussed in this question: npm run build hangs indefinitely on azure. Our build process has been functioning smoothly for several months. However, we are currently facing an issue where npm build is failing ...

Having trouble configuring a basic express server to host a static JavaScript file

Struggling to set up a basic Express server with accompanying HTML and JS files. Despite multiple attempts, I can't get the JS file to properly load. index.js: const express = require("express"); const app = express(); const path = require ...

What is the best way to activate a click event when I set a radio button to checked?

I am facing an issue with an uninitialized property in my app.component.ts: color!:string; I am trying to automatically initialize the color property when a radio button is selected: <div> <input type="radio" name="colors" ( ...

Finding your way through Bootstrap Datepicker using the Previous and Next Buttons

Is there a way to implement Previous and Next Button functionality for navigating a Bootstrap Datepicker using jQuery, similar to the illustration below? https://i.sstatic.net/e9I7P.png I attempted the following approach: function PrevNext(isnextbtn) ...

Implementing a Radial Cursor with a Custom Background Image in JavaScript

I am attempting to implement a radial cursor on a website that features a background image. Currently, I am facing two main issues: The radial cursor works in Chrome but not in Firefox. When using Firefox, I encounter a parsing error related to the "bac ...

What is the best way to assign a unique background color to each individual card in my card list?

I have a list of cards and I want to assign a unique color to each card in the list. I attempted to do this on my own, but it ended up being more complex than I anticipated. Here is the list of cards: return ( <Container> <Row className=&qu ...

How can I animate SVG paths to fade in and out sequentially?

My current code is causing the path to fade in/out all at once instead of one after the other var periodClass = jQuery(this).parent().attr("class"); jQuery("svg path").each(function(i) { var elem = jQuery(this); if (elem.hasClass(periodClass)) ...

Dynamic data manipulation with Angular ReactiveForms

One of the challenges I am facing involves using formArray for my list of products. Specifically, I am trying to access the value of product_code in my .ts file similar to [ngModel] so that I can manipulate the data accordingly. Can anyone provide guidance ...

Calling this.$refs.upload.submit() is not providing the expected response from Element-UI

Currently working with element-ui and attempting to upload a file using the following code: this.$refs.upload.submit(); Is there a way to retrieve the response from this.$refs.upload.submit();? I have attempted the following: .then(response => { t ...

How to retrieve an array of objects with Express, Node JS, and Angular by making an HTTP Get call

After following a tutorial on setting up an Angular app with an Express server using Node.js from , I attempted to send a GET request to retrieve an array of objects with the code below: import { Component, OnInit } from '@angular/core'; import ...

Error message in Typescript with React: "The type 'ComponentClass<StyledComponentProps<{}>>' cannot be assigned to type 'typeof MyComponent'"

Currently experimenting with integrating the Material UI 1.0 (beta) @withStyles annotation into a React component. The documentation provides a JavaScript example (), however, it results in a compilation error when using Typescript. Despite the error, the ...

Using .get methods with jQuery's .on

I need to retrieve the tag name of the element that is clicked inside an li when the user interacts with it. The li element gets dynamically added to the HTML code. I have implemented the following code, but unfortunately, it does not seem to be functionin ...

Validation using Joi allows for a field to be optional, but if it is included, it must be a positive integer

I am facing a challenge with a field in my JOI schema that I want to make optional, allowing for both undefined and null values. However, if a value is provided, it must be a positive integer. How can I accomplish this? So far, I have attempted the follow ...

Getting the value from the object that holds the Provider/Consumer using React's Context API

Below is a demonstration using the Context API object with a library called 'react-singleton-context'. Check it out here. In my Menu.js file, I have the code snippet console.log(useSharedDataContext()). This displays an object containing Consume ...