Using VueJS transitions may require the use of setTimeout for them to work

My code successfully slides out a menu:

      Vue.set(this.filterStyles, filterIndex, {
        display: "block",
        height: "auto",
      });

      let filterValuesElHeight;
      Vue.nextTick().then(() => {
        let filterValuesEl = document.getElementById('parent-filter-values-' + filterIndex);
        filterValuesElHeight = filterValuesEl.clientHeight;

        Vue.set(this.filterStyles, filterIndex, {
          height: 0,
          display: "block"
        });

        return Vue.nextTick();
      }).then(() => {
        setTimeout(() => {
          Vue.set(this.filterStyles, filterIndex, {
            height: filterValuesElHeight + "px",
            display: "block"
          });
        }, 10);
      });

The initial setup of the menu includes the following rules:

display: none;
height: 0;
transition: all 500ms;

The first Vue.set sets the height to auto in order to accurately calculate its height using filterValuesEl.clientHeight

After returning to 0 on the next tick, the final tick then sets it to its natural height.

Despite using Vue.nextTick(), I found that adding a tiny timeout seems to be necessary. While this solution works, it feels somewhat messy. Is there a cleaner alternative?

Answer №1

It seems like you are utilizing the filterStyles within a template, possibly in this format: :style="filterStyles". In that scenario,

Vue.set(this.filterStyles, filterIndex, {
    display: "block",
    height: "auto",
});

will not immediately adjust the height to auto. Instead, it will update the component's data and reflect that change in the DOM after the next nextTick(). Essentially, your code is one tick behind.

However, continuously stacking nextTick calls is not recommended practice. If the component gets removed during this process, it can lead to unresolved references.

To simplify this, you could directly set the style to measure its natural height. For instance, if the element has a ref="filter" attribute:

this.$refs.filter.style.height = "auto";
filterValuesElHeight = this.$refs.filter.clientHeight;
this.$refs.filter.style.height = "0";

You may need to address conflicts between the template and inline JavaScript (template is not provided), but this approach should help you get started.

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

When placed in an absolute position, Overflow seems to be malfunctioning

Within a div that is positioned relatively, there is another div positioned absolutely. Despite setting overflow:hidden and border-radius on the main div, the inner div's edges are not contained within the main one, indicating that the overflow proper ...

Adding dashes as padding in HTML/CSS

I am currently working on updating the user management block on my website and I want to showcase it with some visual examples. I believe that using images is the most effective way to demonstrate changes. This is the current appearance of the block: ...

What is the process of reading an excel file in angularjs?

I attempted to read an Excel file by following a tutorial I found at . Unfortunately, I encountered an undefined situation in the highlighted line below while trying to do so in IE11. var reader = new FileReader(); reader.onload = function( ...

Is it impossible to access the length property of an undefined variable?

After developing a function that calculates the length of a string entered into an HTML textbox, I encountered an error when trying to display the result in another textbox. The function is designed to get the value from the 5th textbox on my HTML page and ...

The function documents.getElementsById() is hindering the effectiveness of Javascript validation

I'm facing an issue with this code. If I uncomment the specific line, the form bypasses validation and goes directly to the linked page in the action attribute. However, if I keep it commented out, the validation runs smoothly, and the alert box displ ...

Encountering problems with parsing a lengthy JSON file

Can you spot the issue here? let stringinsta = JSON.parse({ "access_token":"129261**5ea59a4da481c65", "user":{ "username":"carlos_bellesso", ...

SolidJS createEffect will only trigger on changes after the initial rendering

When I was searching for a solution, I came across the need to only trigger a reaction after my component had been rendered for the first time. For instance: function InputName() { const [name, setName] = createSignal(""); const [nameError, ...

The display:flex property with justify-content:space-around is malfunctioning in React and causing issues

I've been trying to troubleshoot the issue with my header, but so far I haven't found a solution. Could you please take a look at my code first? // Code simplified for clarity, no need to worry about variables const Header = () => { return ...

The vertical baseline alignment is not functioning as expected

In my setup, I have a straightforward configuration: <button> Lorem </button> <input type="text" value="Ipsum"> both positioned side by side with the help of display: inline-block: button, input{ height: 34px; line-height: ...

The challenge of using CSS Flexbox to position the logo with a margin-top

While learning flexbox, I encountered a challenge. I aim to center align h1, p, and button elements, with the logo positioned at the top margin-top of 1em. Essentially, I want the h1, p, and button to be centered, while the logo is positioned at the top wi ...

Tips for effectively utilizing the Angular ngIf directive for toggling the visibility of elements

<div *ngFor = "let element of myElements, let i=index" [ngClass]="{'selected':element[i] == i}"> <li> Name: {{element.element.name}}</li> <li> Description: {{element.element.description}}</li ...

What is the method for altering font color in HTML?

Here is the section of my code that I am struggling with: <p style="font-family:'impact', color:red"> something </p> The issue I am facing is that I am unable to use both the color and the font propert ...

Save the ID from the listview in the browser's localStorage

I have a listview that is generated by a loop: for(var i = 0; i < data.length; i++) { var garage = data[i]; $("#content-p").append( "<ul data-role=\"listview\">" + "<li><a href=\"#\" i ...

Are you planning to print the JSON information?

I've been working on mastering JavaScript and decided to create a script that displays all public Facebook statuses related to a specific keyword. However, things aren't going as planned. You can find a sample URL where the JSON data is located h ...

Storing a boolean value in MongoDB as a string

const myObject = { school: true, address: false }; const myArray = []; myArray.push(myObject); updateSchool(myArray); function updateSchool(thisSchool) { $.ajax ({ type: "POST", url: '/update_school', d ...

There was a SyntaxError that caught me by surprise in my Javascript code when it unexpectedly encountered

I have been encountering an error in my code consistently, and I am struggling to comprehend why this is happening. The problematic area seems to be in line 29 of my Javascript file. HTML <link href="Styles12.css"; type="text/css" rel="stylesheet"> ...

The error message regarding pdf.worker.min is stating that the use of 'import' and 'export' is limited to within module code and cannot be used outside of it

Upon attempting to deploy a build to Vercel, I encountered the following error message: Failed to compile. static/media/pdf.worker.min.50acc843.mjs from Terser x 'import', and 'export' cannot be used outside of module code ,-[18:1 ...

Is it not possible to implement a "literal" component in SFC?

I'm attempting to create a straightforward "literal" component using MyComponent inside a single file component in Vue 3: <template> <MyComponent>aha</MyComponent> </template> <script> import { ref } from "vue"; const ...

Enhance text search functionality using AngularJS

Just starting to learn angularjs. Below is the code I've written: $scope.styles = [{"name":"walking"},{"name":"Food"},{"name":"culture"}] I have created a checkbox list <div ng-repeat="style in styles"> <input type="checkbox"> {{ ...

Utilizing RxJS finalize in Angular to control the frequency of user clicks

Can someone provide guidance on using rxjs finalized in angular to prevent users from clicking the save button multiple times and sending multiple requests? When a button click triggers a call in our form, some users still tend to double-click, leading to ...