Dynamic element substitution

Can you provide guidance on how to create a smooth transition in the height of a container based on the child element's height? Currently, my code does not include any animation effects.

setTimeout(() => {
  document.getElementById("page1").style = "display:none";
  document.getElementById("page2").style = "display:block";
}, 5000);

setTimeout(() => {
  document.getElementById("page1").style = "display:none";
  document.getElementById("page2").style = "display:block";
}, 15000);
.container {
  width: 50vmin;
  background: green;
  transition: all 5s ease;
  display: block;
}

#page1 {
  width: 25vmin;
  height: 50vmin;
  background: red;
  display: block;
}

#page2 {
  width: 25vmin;
  height: 10vmin;
  background: black;
  display: none;
}
<div class="container">
  <div id="page1"></div>
  <div id="page2"></div>
</div>

Answer №1

When implementing a transition, make sure to apply the transition animation to the specific element you wish to observe in order to achieve a smooth effect.

Additionally: Instead of toggling between display none and block, when focusing on height animation, modify the height style property.

Take a look at this practical example:

setTimeout(() => {
  document.getElementById("page1").style = "height:20vmin";
  document.getElementById("page2").style = "height:20vmin";
}, 5000);
.container {
  width: 50vmin;
  background: green;
  display: block;
}

#page1 {
  width: 25vmin;
  height: 50vmin;
  background: red;
  display: block;
  transition: all 5s ease;
}

#page2 {
  width: 25vmin;
  height: 10vmin;
  background: black;
  display: none;
  transition: all 5s ease;
}
<div class="container">
  <div id="page1"></div>
  <div id="page2"></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

Vue.js: Efficiently handling multiple buttons within a Dropdown menu

I am currently working on a Vue.js Project and have a Dropdown component. Here is the code snippet: <template> <v-menu close-on-click transition="slide-y-transition"> <template v-slot:activator="{ on, attrs }" ...

Attaching a $UI element to a <div> tag with JQuery may result in unexpected errors and issues

Attempting to connect SagePayments card elements to the paymentDiv. Followed their sample project for guidance, but encountering issues with populating the elements when running the program with a custom Sandbox merchantID and merchantKey. Chrome's de ...

Requesting Access-Control-Request-Headers using jQuery Ajax POST method

I am attempting to send an AJAX request to my server. Initially, I referenced a library (in the web) within a <script> tag in my HTML document and executed it using the following code: $.ajax({ url: api_url + "movie/create", type : "POST", con ...

Is Jquery Steps causing interference with the datepicker functionality?

I am currently using the jquery steps plugin for creating a wizard on my website. However, I am experiencing some issues with the functionality of the datepicker and qtip inside the steps. Even after switching the .js references, the problem still persists ...

Utilizing YouTube API information with AngularJS

I am currently working on a project where I need to fetch all the playlists from a specific channel and then display the name of each playlist in my AngularJS application. var myApp = angular.module('app', ['ngResource']); myApp.facto ...

Is it possible to smoothly transition to the next step in PLAYWRIGHT testing if a button click is not an option?

My question is whether it's possible to attempt a click action on a button, and if the button is not present on the page, have the test skip that action without getting stuck or throwing an error, and continue to the next one. To provide more context, ...

Unlock the Power of Heroku: Leveraging Node.js to Share Environment Variables Across JavaScript Pages

Currently, I am following the 'getting started' guide on the Heroku webpage. As part of this process, I have cloned their tutorial repository and decided to add my own index.html and app.js files to the existing /public folder. The directory str ...

I am puzzled as to why my code in React is rendering twice without any apparent reason

I ran into a strange issue where my console.log("hi") was being displayed twice. I was working on a simple todo-list project and noticed that everything was getting double clicked. After some troubleshooting, it seems like the code is executing any JavaScr ...

The functionality of CSS Inline Block display is not displaying inline as expected

I have the following HTML and CSS code, but I am having issues getting the dropdown to display inline. .project Type{ display: inline-block; } <form class="smart-form"> <div class="row"> <div class="col col-sm-6 col-md-2 col-lg- ...

The CSS styles on my GitHub Pages website are not being reflected on the HTML page

I've been struggling for a while now to host my Portfolio CV on GitHub and I can't seem to get my CSS styles to apply correctly on the URL. - This is how I have linked it in the HTML document: <link rel="stylesheet" type="text/ ...

Discover the steps for dynamically adding a new row in an Angular application

I am trying to add new rows to a table in Angular, but I'm having some trouble. Initially, there is one empty row, and when I click on the "add new" button after entering details, a new row should be added. I was able to do this using jQuery, but I&ap ...

A div element appears without any height if there is an image contained within it

I am facing a challenge with a div[div1] that contains other elements. The elements inside have an absolute positioned image, and both div1 and the elements have a float left property without showing any height. Is there a solution to give div1 a height so ...

Encountering a 503 Error in Loading .js Files from Index.html in a .NET 7.0 Angular Application

I recently came into possession of an Angular project that I am trying to set up in IIS, but I am encountering some unusual behavior. Since I am relatively new to Angular, I ask for your patience as I navigate through this issue. Upon loading the website, ...

Struggling to traverse through intricate layers of nested objects in React and showcasing them without going crazy

Dealing with an API that returns data structured in deeply nested objects has been a challenging task. The goal is to extract specific data from these nested objects and then display them within a React project. Despite numerous attempts, finding a simple ...

What could be causing the black spaces to appear after my text in HTML?

I recently tried to implement text on an image following a tutorial I found at https://css-tricks.com/text-blocks-over-image/. Here is the code snippet I used: .image{ position: relative; } h2{ position:absolute; top: 200px; left: 200px; wi ...

Communication between Laravel and controller using AJAX for exchanging information

I have a specific AJAX function being called from a view: function gatherProductData() { var productIds = []; $('#compare-widget tbody tr').each(function(i, ele) { productIds[i] = $(ele).data('product-id'); }); ...

Looking for assistance in transferring information from one webpage to another dynamic webpage

I'm in the process of building a website to feature products using NextJs. My goal is to transfer data from one page to another dynamic page. The data I am working with consists of a json array of objects stored in a data folder within the project. Wh ...

Locating Mongoose users who have registered within specific date ranges

My user model looks like this const guestSchema = mongoose.Schema({ facebook: { id: String, token: String, email: String, name: String, phone: String, dates: [ { type: mongoose.Schema.Types.ObjectId, ref: "l ...

Warning: Typescript is unable to locate the specified module, which may result

When it comes to importing an Icon, the following code is what I am currently using: import Icon from "!svg-react-loader?name=Icon!../images/svg/item-thumbnail.svg" When working in Visual Studio Code 1.25.1, a warning from tslint appears: [ts] Cannot ...

Utilizing Angular to apply multiple ng-repeat directives with multiple filters

I am working on a project that involves multiple ng-repeat lists with several filters. Currently, I am using (ex:A.value) if (ex:B.value), but I would like to implement multiple filters. The filters I want to incorporate are recommend_search, skill_searc ...