Are there ways to implement Vue.js transitions without directly setting the height in the code?

I'm having an issue with a Vue app I created where I need to make an element expand and collapse when a button is clicked. I want the effect to be smooth and animated, but using the <transition> tag alone isn't working as expected. The problem is that I can only get the transition to work properly if I set a fixed height for the element in the CSS, which is not feasible since the content inside the element is dynamic and its height varies.

Here's my current code:

<template>
    <input type="button" value="Toggle" v-on:click="showIt = !showIt"/>
    <transition name="test-tran">
        <div class="test-block" v-show="showIt">
            <div v-for="item in items">{{ item }}</div>
        </div>
    </transition>
</template>

<script>
    export default {
        data() {
            return {
                showIt: false
            };
        }
    }
</script>

And here's the related CSS:

.test-block {
    /*
    Setting a fixed height makes the transition work,
    but this approach doesn't account for varying heights

    height: 100px;
    */
}

.test-tran-enter-active {
    transition: all .5s ease;
}

.test-tran-leave-active {
    transition: all .5s ease;
}

.test-tran-enter, .test-tran-leave-to
    height: 0;
}

.test-tran-leave, .test-tran-enter-to
    /*
    Setting a fixed height makes the transition work,
    but this approach doesn't account for varying heights

    height: 100px;
    */
}

Without explicitly setting the height property, the element does appear and disappear correctly, but without any animation. It just suddenly appears or vanishes.

I really need the animation to work without hard-coding the height in the CSS. Any suggestions on how to achieve this?

Answer №1

It appears to be solely a CSS problem. For transitions to function properly, a height parameter must be specified. However, there is a workaround that can be applied.

For more information, refer to How can I transition height: 0; to height: auto; using CSS?

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

CSS Stylelint rule to detect the 'missing selector' issue

My current process involves using stylelint to identify any CSS errors before building and deploying our site. However, a recent commit includes code that fails the CSS parser/minifier but passes stylelint validation. .example-class , /*.example-class-two ...

Alter the button's color seamlessly while staying on the same page

I have implemented a feature that allows me to change the category of photos without having to leave the page and it works perfectly. My next goal is to create a button system where the pre-defined category of a photo is indicated by a button with a green ...

Troubleshooting the Full Height Feature for Sidebar (Panel) in Twitter Bootstrap

I'm having issues with the Sidebar (Panel) in Twitter Bootstrap. I'm encountering some trouble with how it is displayed. Below is the CSS code I am using for the sidebar: .sharecartside .content { height: 100% !important; min-height: 10 ...

vue-router does not automatically navigate to a new route

Despite my numerous attempts to find the solution, I can't seem to figure out what is going wrong. I have been following this example. Here is how my VueRouter instance looks like. const router = new VueRouter({ mode: "history", routes: [ { ...

Enhancing functionality by integrating Jquery/JS with input fields and labels

I am currently facing a challenge in applying CSS to the label element when the corresponding input is selected. After attempting and failing to use input:checked + label due to limitations in the HTML structure that cannot be altered, I seek alternative ...

The performance of ASP.net text-boxes is inconsistent

After days of searching for a solution, I am still unable to resolve my issue. My website features a simple web-form with text-boxes that are supposed to appear when the user selects the "custom search" option. While testing locally, everything works fine. ...

Tips for repairing the gray margins on the right and left sides of a webpage

Currently utilizing Bootstrap 5 and incorporating columns within the framework. However, encountering an issue where there is black space on the right side of the screen and certain content disappearing on the left side as demonstrated here. Here is the c ...

What are some ways I can adjust line-height without changing the height of the cursor?

When working in a contenteditable div, I am utilizing line-height to create some spacing between lines. The following example illustrates the issue: <div style="padding: 50px; width: 90px; line-height: 2em;" contenteditable="true"> line height lin ...

Struggling with a malfunctioning Bootstrap dropdown that refuses to drop down

I've been working on a VueJS single-page application, but I can't seem to get the dropdown feature to work. I've followed the instructions from the Bootstrap guide and tried everything they recommended, but it just won't cooperate. Can ...

The appearance of unnecessary empty spaces at the conclusion of the page

.article-image1 { height: 256px; width: 256px; } .article-image1:before { content: ''; position: absolute; height: 100%; width: 100%; top: 0; left: 0; background-image: url(http://limootoys.com/wp-content/uploads/gentleman-e150 ...

I appear to be having issues with the pseudo-element. Is there something I am doing incorrectly? This relates to the first child

I initially tried to make the opening paragraph stand out by applying a red color, but unexpectedly, all elements on the page turned red. view image here <!DOCTYPE html> <html lang="en"> <head> <link rel="styleshee ...

What is the best way to retrieve a style property from a <style> tag using JavaScript?

Is there a way to retrieve CSS properties set in the <style> tag in JavaScript, rather than just those set in the element's style attribute? For example: <style> div{background:red;} </style> How can I access these styles, such ...

Tips for using jQuery to dynamically apply CSS styles to new content added to a webpage

I have encountered this question multiple times on stackoverflow and through google search, but unfortunately none of the suggested solutions have worked for me. My issue involves a page that loads an HTML page with empty sections, which are then dynamica ...

Radio button - arranging the background image and text in alignment

I'm struggling to align my custom radio buttons alongside text. Here is an example image: https://i.sstatic.net/2g5w8.jpg Despite using CSS for styling, I am unable to properly align the radio buttons. Below is a snippet of my HTML: label.item { ...

When importing my Vue components using NPM, I can only locate the modules if they are situated within the src directory, rather than in the

Within my application's root directory, I have both a /node_modules/ and /src/ folder. After running npm install and installing packages using commands like npm install axios --save for all the required dependencies, I can see that they are being add ...

CSS for Page Header with Scroll-Over Content

Having trouble creating a "collapsing header" design where the page content scrolls over the banner image. I've tried adjusting z-index and positioning properties in CSS, but can't achieve the desired effect. Any help or guidance would be greatly ...

Align the content vertically in a Bootstrap column that takes up the full height

Is there a way to make sure that the columns in a row are the full height of the row while keeping the text content vertically centered? <div class="row fixed-bottom mobile-navbar align-items-center"> <router-link to="/" cl ...

Preventing v-list-item-group from being deselected in Vuetify

Currently, my setup includes a v-list and v-list-item-group, resembling the example provided in the Vuetify documentation: https://vuetifyjs.com/en/components/lists/#flat The issue arises when a user clicks on the same v-list-item twice, causing it to be ...

Press anywhere else on the screen to dismiss the bootstrap menu

I've been attempting to find a solution for closing the Bootstrap menu when clicking outside of it in a mobile window size, but I just can't seem to make it work. I did manage to get it working when clicking one of the 'a' links using t ...

The svg line is drawn with a width that is half of the specified width

I am looking to create a horizontal line that is 10px wide. I tried using the code below <svg width="500" > <line x1="100" x2="460" y1="0" y2="0" stroke="red" stroke-width="10px&qu ...