Issues with CSS transitions causing undesired effects

I am facing an issue with my Angular application. In the HTML file, there is a div element with the class "row" which contains two div elements with the class "col". When clicking on a button, the size of these columns should change:

<button class="btn btn-primay" (click)="changeColumnsSize()"> change column sizes</button>
      <div class="row">
        <div id="leftColumn" class="col-sm-{{leftColumnSize}}" style="background-color:lavender;">
          .col-sm-8     
        </div>
        <div id ="rightColumn" *ngIf="this.state===true" class="col-sm-{{rightColumnSize}}" style="background-color:lavenderblush;">
          .col-sm-4
        </div>
      </div>

To ensure a smooth resize effect, I have added transition properties to the col classes in the CSS file:

.col-sm-8 { transition: width .5s ease; }

.col-sm-4 { transition: width .5s ease ; }

The TypeScript file containing the changeColumnsSize function is as follows:

export class BoxComponent {
  leftColumnSize: number = 12;
  rightColumnSize: number = 0;
  colDifference: number = 4;
  state: boolean = false;

  constructor() { }

  changeColumnsSize(){
    if (this.state===false)
      this.state = true;
    else
      this.state = false;
    if(this.state===true) {
      this.leftColumnSize-=this.colDifference;
      this.rightColumnSize+=this.colDifference;
    }
    else if (this.state===false) {
      this.leftColumnSize+=this.colDifference;
      this.rightColumnSize-=this.colDifference;
     }
    }
  }

However, there is an issue where the rightColumn appears below the leftColumn during the transition before moving up to its correct position.

Could you assist me in resolving this problem without utilizing Angular animations?

Answer №1

In version 4, the grid layout is not reliant on the width property (and box model), but instead on flex-basis + max-width (and flexbox model).

To make the transition smooth, you must set the transition on both properties. Alternatively, you can take the lazy and incorrect route by setting it on all.

Here's an example to showcase how it works:

function adjustColumnSize() {
  $('.column-switcher > div').toggleClass('col-sm-4 col-sm-8')
}
.column-switcher>div {
  transition: max-width .3s cubic-bezier(.4, 0, .2, .1), 
              flex-basis .3s cubic-bezier(.4, 0, .2, .1);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="container">
  <button class="btn btn-primary" onclick="adjustColumnSize()"> Change Column Sizes </button>
  <div class="row column-switcher">
    <div id="leftColumn" class="col-sm-8" style="background-color: lavender;">
      .col-sm-8
    </div>
    <div id="rightColumn" *ngIf="this.state===true" class="col-sm-4" style="background-color: lavenderblush;">
      .col-sm-4
    </div>
  </div>
</div>

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

Embracing Twitter's Bootstrap framework in your design?

When I declare my external CSS on a template within the HEAD tag, all hell breaks loose on the page if I declare another external CSS file in the same tag. However, everything seems to work fine if I keep the two CSS file declarations separate - one in the ...

Caching HTML5 videos in Google Chrome

I am currently in the process of building a website and have successfully added an HTML5 video. However, I encountered an issue when attempting to change the video file in the background for users to upload a new one. Despite updating the video URL, Chro ...

element obscured by overlapping elements

I'm unsure why this issue is occurring; it seems to be a CSS error, but I can't pinpoint the exact location. Working on this in Rails, so I'm relatively new to it, but it should follow standard CSS practices! https://i.sstatic.net/OtO6O.jp ...

Generating Unique Random DIV IDs with JavaScript

Does anyone know of a JavaScript solution to generate unique random DIV IDs? I am currently using PHP for this purpose. <?php function getRandomWord($len = 10) { $word = array_merge(range('a', 'z'), range('A', &apos ...

Angular 4 is unable to attach to 'formGroup' as it is not recognized as a valid property of 'form'

As a beginner with Angular 4, I decided to explore model driven forms in Angular 4. However, I keep encountering this frustrating error. Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form ...

The object's key values were unexpectedly empty despite containing data

There's an issue with my object - it initially gets key values, but then suddenly loses them all. All the key values become empty and a message appears in the console for the object: "this value was evaluated upon first expanding. it may have ch ...

Is there a way to incorporate borders into JqTree?

I attempted the following .jqtree-tree .jqtree-element { border-color: black; border-width: 10px; } It seems that I may be overlooking certain elements to which this CSS should be applied ...

What steps can I take to mimic a pen-like behavior for my cursor on my sketching website project?

My goal is to create a 16x16 grid with a white background, where each grid item changes color when the mouse is pressed and moved over it, similar to paint. I have written a script to achieve this: let gridContainer = document.getElementById('grid-con ...

Is it possible to modify the names of the months in bootstrapMaterialDatePicker?

I've been struggling to change the month name in my code, and despite trying multiple approaches, it still doesn't work. Here is a snippet of my code: HTML; <div class="form-line"> <input type="text" class="form-control datepicker" ...

Node server encountering issue with undefined data in POST request

I have been working on an Angular2/Node.js application. Everything seems to be working fine when I retrieve an object from the Node server, but I'm facing an issue when trying to post data to the server. The request.body always shows as undefined. Can ...

Personalized HTML checkbox featuring custom labels

I'm working on a website where I want to personalize the appearance of checkboxes with labels. Can anyone recommend some good libraries that can help me achieve this? For example, I have heard about the ibutton library, but I'd love to explore o ...

Exploring and Monitoring a Target in an Angular Module: Jasmine Investigation

My component utilizes a Subject<void>, which triggers another function - refresh() upon emission. It is crucial for my unit tests to validate this behavior, specifically checking if the refresh() function is invoked. Referring to the Jasmine documen ...

Challenges arise when trying to manually conceal Twitter Bootstrap tooltips when using HTML5 <object> components

In the process of creating my first JavaScript application, I am developing a widget-based designer that utilizes various widgets based on SVG within the main HTML body using object tags. One challenge I encountered is trying to associate a bootstrap toolt ...

Express causing textarea in http form to have empty req.body

Here is a form for users to upload a file and submit text: form(action='/createpost' enctype="multipart/form-data" method='post' id="imgForm") input(type='file' name='imgPath' size = "60") br textarea(na ...

Creating a custom script for a search field that retrieves information from an XML document

Attempting to create a script that iterates through an XML file, the provided code currently displays alerts but fails to show information when a valid value is entered into the search field. However, upon removing the error checks and keeping the final ...

SQL Server database is showing question marks instead of HTML symbols

Recently, I observed that special HTML symbols like: ★ are appearing as question marks in my database records. I have set the type of the field as varchar and am utilizing Microsoft SQL 2008 for my database management. Is there a solution to this symbo ...

Flashing visuals during page load using JQuery

Hey, I've run into a bit of a dilemma. My server has a Webinterface that constantly displays an image, but when I access it from another PC, the image starts flickering. I attempted to solve this issue using this solution, however, it only seems to w ...

Is there a way to conceal/remove the text displayed on the submit button?

I am facing an issue with a submit button in my PHP code. The button displays an integer value that is crucial for running a script, but I want to hide the value as it interferes with the design using CSS. I have attempted various solutions like removing t ...

The upper section of the pop-up modal is not fully visible when the screen size is decreased

My modal is experiencing an issue where the top part gets cut off when the screen is reduced, disappearing under the bookmarks and address bar. I attempted a solution mentioned in this resource by setting the top and bottom to 0, but it did not resolve the ...

How can we design a CSS layout where elements are positioned fixed on the left but dynamically adjust to prevent overlap?

I'm currently working on creating a layout that features a list of labels positioned at fixed percentage intervals. However, I am facing difficulty as I want these labels to move down onto separate lines if the page width is reduced in order to preven ...