Tips for animating the height of a transition-group container in Vue

Currently, I am working on a project where I need to animate the addition and removal of items from a list. While I've managed to animate the list itself successfully, unfortunately, the height of the container does not animate along with it.

Here is the CodeSandbox link for reference

In the preview, you can observe that the red container's height changes abruptly without any transition effects.

I would greatly appreciate any guidance or advice on how to properly animate the height of the container when an item is added or deleted from the list.

Thank you in advance,

Answer №1

To modify the height of your container dynamically, simply include the CSS attribute: transition: .3s, where .3s represents the transition time in seconds.

Answer №2

<template>
  <div :style="{'max-height': list.length * 24 + 'px'}" class="dynamic-height bg-red-400 container mx-auto">
    <transition-group name="fade-top" tag="ul">
      <li v-for="(item, index) in list" :key="index">
        <p>{{ item }}</p>
      </li>
    </transition-group>
    <button class="mt-4 px-5 py-2 bg-indigo-400 text-white" @click="add">Add</button>
    <button class="mt-4 ml-4 px-5 py-2 bg-indigo-400 text-white" @click="remove">Remove</button>
  </div>
</template>

<script>
export default {
  name: "DynamicList",
  data() {
    return {
      list: [1, 2, 3, 4]
    };
  },
  methods: {
    add() {
      this.list.push(Math.round(Math.random() * 10));
    },
    remove() {
      this.list.pop();
    }
  }
};
</script>
<style>
.fade-top-enter-active,
.fade-top-leave-active {
  transition: opacity 0.3s, transform 0.35s;
}
.fade-top-enter {
  opacity: 0;
  transform: translateY(8%);
}
.dynamic-height {
  height: 100vh;
  transition: max-height 300ms;
}
.fade-top-leave-to {
  opacity: 0;
  transform: translateY(-8%);
}
</style>

When implementing height transitions, consider using max-height. Begin by setting a large height such as 100vh, then calculate the max-height based on the length of your list.

:style="{'max-height': list.length * 24 + 'px'}"

If you're wondering about the value "24", it represents the height of your p element.

Additionally, apply a CSS class to your div:

.dynamic-height {
  height: 100vh;
  transition: max-height 300ms;
}

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 method to retrieve initial data in Vue 3 using async/await prior to rendering?

I am currently in the early stages of learning Vue 3 and I'm looking to fetch initial data from an API to populate certain fields before the App component is loaded. According to the [documentation][1], top-level await can be used within the <scrip ...

I'm working with Angular 12, Bootstrap 5, and ngPrime, and I'm looking to overlap a p-dialog with another element in my project

Is there a way in Angular 12 with Bootstrap 5 using ngPrime to overlap a p-dialog over any other element without using z-index and CSS, solely relying on PrimeNG? I have tried using z-index with: .my-class{ z-index: 2147483647 !important; } However, ...

using dynamic interpolation to determine the method name within a v-for loop in Vue.js

Within my Vue.js project, I am creating a list element using the ul li structure and the v-for directive in Vue.js. Here's an example: <ul> <li :class="{active: 'isActive+index'}" v-for="(car, index) in cars"></li> < ...

Building a tag cloud with only HTML5 and CSS3

I am looking to generate a word cloud similar to the one shown in this image : click here for reference Is there a way to create a tag cloud without relying on JavaScript? Using only HTML5 and CSS3. Appreciate any guidance. Does anyone have a solution fo ...

Trouble with intersecting DIV (and picture)

I'm facing an issue with a DIV overlap on my webpage. When the window is maximized, here's how the webpage looks: And this is how it appears when the window is resized: What happens is, when users scroll to the right (indicated by the red arro ...

Looking for assistance in determining a solution for progress bar implementation

Currently, I am developing a simple application using web technologies like HTML, CSS, and jQuery Mobile. This app enables me to create goals and save them to a listview using the <li> element. Every <li> representing a goal will have an assoc ...

Customizing the layout of div elements with Twitter Bootstrap for a responsive design

Utilizing Bootstrap's responsive style sheet, I have organized 4 squares in divs that span 12 columns - |3|3|3|3|. However, when I access this layout on a mobile browser, it displays as: |12| |12| |12| |12| My desired display format is: |6 ...

Looking to have my textarea display its readonly text with HTML formatting

I am seeking assistance with formatting a read-only textarea that displays my old blog posts. I would like to make the content resemble HTML code and have URLs automatically display as clickable href links. Do you know of any best practices for achieving ...

Prevent Opera Mobile from highlighting touched elements

Can anyone help with preventing Opera Mobile (Windows emulator) from showing blue outlines for touched HTML elements? A blue outline shows up when touched: I am looking for this: ...

I am trying to include a vertical line both before and after the list tag. However, the first list tag is not displaying properly with the vertical

I encountered an issue where, despite adding a line using the "before" concept, the first list tag's left sideline is missing. The error output can be viewed here. The desired output should look like this: here Here is the HTML Code: <div cla ...

Can styles be universally applied to specific elements or classes on a webpage by using hover on a single selector?

Is it possible, using only CSS and no JavaScript, to apply styles to specific elements or classes throughout an entire webpage without restrictions such as descendants or siblings, by hovering over a particular selector? For instance, I would like to remo ...

Can the card overlay slide appear solely on top of the image?

I am trying to create a gallery section of cards using Bootstrap 4 where I want the user to hover over each card and have an overlay slide or fade in, covering only the image. Currently, the overlay slides in over the entire card instead. I have been stru ...

Creating distinct web addresses for various sections of vue.js pagination

I've implemented pagination using vue.js for my website's gallery. However, I'm facing an issue where the URL doesn't change when navigating through pages and upon page refresh, it always resets to page 1. My goal is to have the URL ref ...

In order to design a v-btn-toggle with vertically aligned buttons, rather than horizontally

I'm currently in the process of developing a quiz using VueJS and Vuetify. My challenge lies in organizing the answer options vertically for the user to select. I attempted to utilize the v-btn-toggle component, but encountered an issue where the butt ...

Vue's bidirectional data binding presents a challenge when attempting to update the parent component from a child component

I have the following components: Parent Component: <template> <v-container> <v-row class="text-center"> <v-col cols="12" class="parent"> <p>I am the Parent component</p> ...

Developing Progressive Web Apps (PWAs) with Vue 3 and implementing offline

Currently, I am in the process of developing a Progressive Web Application (PWA) using Vue 3 and Firebase. One of the key features I need to incorporate is offline support, specifically ensuring that users remain logged in even when they lose internet conn ...

Customize Bootstrap breadcrumbs with Font Awesome icon divider using SASS

Looking to customize the default breadcrumb style using SASS. Following the guidelines from Bootstrap 4 beta 3 documentation, I made changes in the custom.scss: $breadcrumb-divider: "\f105"; //using FontAwesome icon for fa-angle-right However, setti ...

Utilizing background images within the space between the header and footer

Currently in the process of creating a web page layout with three sections: Header, main body, and footer. I am facing an issue when trying to add a background image to the main body section. The image does not cover the entire area, leaving some space at ...

Utilize Vite 2 to import the Monaco Editor into your project

After setting up my Vite 2 project with monaco-editor as a dependency, I encountered an issue where it says that the workers are not imported. editorSimpleWorker.js:454 Uncaught (in promise) Error: Unexpected usage at EditorSimpleWorker.loadForeignModu ...

Customizing table widths with CSS and JavaScript to mimic Excel's column resizing feature

Looking for a way to implement column resizing in a table similar to Excel where only the width of the current column increases without affecting the width of neighboring columns. This should create a scrollable table below. I've attempted using a pl ...