Tips for preventing the parent element's height from fluctuating when displaying a child div using v-if

I have a main container and an inner container. The inner container is displayed using v-if. I want to include a transition effect on the inner element, but after the transition ends, the height of the parent container changes abruptly and looks unattractive.

I am looking for something similar to jQuery's slideToggle() function.

Below is the HTML in which I am applying a fade effect through a transition in opacity:

<div class="my-div">
    <p>some content</p>
    <transition name="fade" mode="out-in">
      <p key=1 v-if="show">hello</p>
    </transition>
  </div>

And here is the CSS for the transition:

.fade-enter-active,
.fade-leave-active {
  transition: opacity .5s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}

.my-div {
  background: lightgreen;
}

Here is the fiddle with my code:

https://jsfiddle.net/x15Lw6a3/

I have attempted to achieve a height transition by changing from opacity to height or max-height as suggested by other questions, but it results in a sudden snap up and down.

If you have any ideas or can point me to a tutorial, I would greatly appreciate it. Thank you!

Answer №1

One way to improve the smoothness of the animation is by utilizing the max-height property. Add max-height: 100px; to the

fade-enter-active,
.fade-leave-active
rule, and in the .fade-enter, .fade-leave-to rule, set it to 0 as shown below:

Vue.config.devtools = false;
Vue.config.productionTip = false;

new Vue({
  el: '#demo',
  data: {
    show: true
  }
})
.fade-enter-active,
.fade-leave-active {
  transition: all 0.5s ease;
  max-height: 100px;
  opacity: 1;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
  max-height: 0px;
}

.my-div {
  background: lightgreen;
}
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<div id="demo">
  <button v-on:click="show = !show">
    Toggle
  </button>
  <div class="my-div">
  <p>some content</p>
  <transition name="fade" mode="out-in">
    <p key=1 v-if="show" >hello</p>
  </transition>
  </div>
</div>

Note:

If you implement this change, you may notice an improvement in the smoothness of the animation.

Answer №2

After exploring various methods, I ultimately decided to utilize a component, and I found that VueSlideToggle worked perfectly for my needs. It incorporates CSS transitions that target the height property.

new Vue({
  el: '#demo',

  data() {
    return {
      show: true
    }
  }
});
.my-div {
  background: lightgreen;
}
<div id="demo">
  <button v-on:click="show = !show">Toggle</button>

  <div class="my-div">
    <p>some content</p>

    <vue-slide-toggle :open="show" tag="div" :duration="500">
      <p v-if="show">hello</p>
    </vue-slide-toggle>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-slide-toggle"></script>

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 ERR_EMPTY_RESPONSE when binding ports to a Docker container

Vuejs3 has been successfully installed and npm run dev is running inside a Docker container using the node:latest image. It's a very basic setup. While inside the container, it's definitely starting on http://localhost:5173, as confirmed by curl ...

Is it necessary for CSS Media query to load all the referenced CSS files?

When utilizing a CSS Media Query to assign two different stylesheets, such as desktop.css and mobile.css based on the max-width, how does this process function? Is it necessary for both stylesheets to be loaded by the browser every time, and then the appr ...

Customize the position of the date picker for HTML native <input type="date">

I have implemented the HTML native input date element (found here- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date) and encountered an issue in Safari on macOS where my date picker is being displayed off-screen. How can I resolve this ...

Tips for aligning two canvases with different heights at the top in HTML5

My problem involves two canvases housed within a single <div>. Despite their different heights, they are currently aligned at the bottom. +-------+ + + +-------+ + + + + + + +-------+ +-------+ Is the ...

"Make Vuetify's v-list-group stand out by adding an active state and

Trying to dynamically change the append-icon based on group status (open/close) isn't working as expected: v-list-group( color="success" @click:append="this.marker = !this.marker" :append-icon="marker ? ...

How can I position a form next to a title when utilizing the Bootstrap Panel feature?

I have experimented with multiple solutions found on various sources, but unfortunately none have been successful. I came close to achieving the desired result, but encountered an issue with the panel's default background color not displaying properly ...

What is the method for switching the pre font family back to the default Bootstrap 5 style?

The default font for <pre> tags in Bootstrap 5 is typically a monospaced style, which suits most coding and programming needs. However, I'm looking to utilize a <pre> tag for formatting a poem where line breaks are crucial. In this case, I ...

Left-aligned collapsible navigation menu built with Bootstrap

Looking for a unique template or code snippet to implement the same menu as the one on this website: I want the menu to slide in from the left while darkening the rest of the screen, just like the site I referenced. Is there a bootstrap template that can ...

The carousel caption in Bootstrap 5 vanishes when viewing the website on a smaller device

Currently, I'm dealing with an issue using bootstrap carousel code. The carousel text displays properly on desktop screens but disappears when viewed on smaller sized screens. It appears that the text is being hidden behind the image. Various attempts ...

Angular Markdown Styling: A Guide

Currently, I am using Angular and Scully. I would like to add style to the image in a markdown file within Angular, but I am unsure of how to accomplish this task. The image is currently displaying too large. Can anyone provide me with useful advice on how ...

What is the most effective method to verify the visibility of an element hidden by v-show when using testing-library in Vue?

When working with Vue and the testing-library, checking for the presence of an element in the DOM is straightforward. For instance, if the v-if condition for the element is set to false: element = screen.queryByTestId("my-element") expect(element).toBeNul ...

Can you guide me on setting a background image URL for rails using javascript?

Within my Rails application, I have a collection of content paired with image buttons (each piece of content has an associated image). When a user clicks on one of these buttons, my goal is to display the corresponding image. All of my images are stored u ...

Fetching information from a data object using asyncData in Nuxt

Is there a method to retrieve object properties as keys from the asyncData() function? data() { return { bookmark_btn: { status: null, loading: false } } } I attempted to access data object properties in the following ...

Unable to adjust layout when code is functioning alongside background-color

I'm looking to dynamically change the position of an item on my webpage when it is clicked. Is there a way I can achieve this without relying on id names? I currently have a code snippet that successfully changes the background color, but for some rea ...

Concealing just the portion of an element that moves underneath a fixed see-through navigation bar

After extensive searching for a solution, I have encountered multiple similar issues but none have provided a resolution that works for my specific case. It seems I may be overlooking something obvious. The problem at hand involves a fixed transparent nav ...

What are the steps to change table characteristics?

I currently have this segment of HTML code: <table cellspacing="0" cellpadding="0" width="100%"> After validating my HTML document, I discovered that these attributes are outdated. However, removing them would affect the alignment of my website. I ...

Ways to prevent v-bottom-sheet or v-dialog from obstructing interaction with the main content

I'm currently working on developing a music playing interface for a Vue page that features a bottom player, similar to the example provided here: https://vuetifyjs.com/en/components/bottom-sheets/#music-player However, I've encountered an issue ...

Is there a way to set a max-width using a Pseudo class

I am utilizing a css tooltip that is linked to a span element in the following manner: <span class="mytooltip" data-mytooltip="Text here. Text here. Text here. Text here. Text here. Text here. Text here. Text here. Text here."> Has Tooltip </ ...

Automatically transferring CSS class attributes to a newly created class

The Issue I've encountered a problem while developing a small app for a website. The form in the app requires validation, but conflicts with existing jQuery scripts have been causing errors. These conflicting styles are essential to maintain the desi ...

The problem of undefined icons in Material UI's Stepper StepLabel

Having some trouble incorporating a custom Step Label Icon within the nodes of the Stepper Component provided by Material UI. I want to add an icon to each circle, similar to what is shown in this Material UI demo: https://i.sstatic.net/WLOcS.png However, ...