Prevent animation when expanding the menu in Vue using CSS

The animation only works when collapsing the menu, not when expanding it.

I tried adding CSS for setting a max-height, but the animation only functions when collapsing the menu.

<template>
  <div>
    <p>
      <button @click="isShow = !isShow">click -- {{ isShow }}</button>
    </p>
    <ul ref="menu" :class="{ menu: true, fold: !isShow }">
      <li>item1</li>
      <li>item2</li>
      <li>item3</li>
      <li>item4</li>
    </ul>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";

const menu = ref("null");
const isShow = ref(true);
</script>

<style scoped>
.menu {
  max-height: 300px;
  transition: max-height 0.2s ease;
}

.menu.fold {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}


</style>

https://codepen.io/wgf4242/pen/XWRKORW

Answer №1

Simply include the following code snippet:

  overflow: hidden;

within your .menu class, not within .menu.fold

The animation will then start functioning properly. It is worth noting that the expand animation may appear faster than the collapse animation due to varied browser sizing calculations: going from 0 to 300px during expansion and from 72px to 0 during collapse. This issue can be addressed by introducing an extra container for the menu and adjusting its max-height to match the actual size upon click.

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

Mobile Image Gallery by Adobe Edge

My current project involves using Adobe Edge Animate for the majority of my website, but I am looking to create a mobile version as well. In order to achieve this, I need to transition from onClick events to onTouch events. However, I am struggling to find ...

What is the process for adjusting the color of a div?

Is there a way to adjust the background color of a div when hovering over another div in HTML? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.or ...

V-For with a Twist: Dynamic V-Model

UPDATE Here is a recreation of the issue I am facing I need to dynamically bind with the correct index, how can I achieve this? CLICK THE CODEPEN LINK ABOVE FOR THE OLD CODE CLICK THE CODEPEN LINK ABOVE FOR THE OLD CODE <template> <div> ...

Nuxt's axios is encountering difficulties in managing the server's response

Having just started working with Nuxt.js, I encountered an unusual issue. There is an endpoint in my backend API that allows users to reset their password by sending a token along with a new password. Although the request is being sent correctly and the s ...

The Challenges of Parsing HTML Source Code for Web Scraping

I've been attempting to scrape data from this website: (specifically rare earth material prices) using Python and BeautifulSoup. My query pertains not to Python, but rather the HTML source code of the site. When I utilize Firefox's "Inspect Elem ...

How can you verify the presence of an object containing specific data within an array using JavaScript?

Currently, I am working with the Vue programming language and have some demo code to showcase: <template> <button @click="checkInList">Check</button> </template> <script> import { ref } from "@vue/reactivity& ...

The variablewidth feature in Slick Carousel is malfunctioning

I've integrated slick slider 1.8.1 into my Rails app (v.5.2.0) and I'm encountering an issue with variablewidth set to true. My expectation was to achieve a layout similar to the example shown here: https://i.sstatic.net/5QFRx.jpg However, what ...

Using jQuery to replace the content of a div with a delay?

I am attempting to utilize jQuery to create a fade effect on an element, replace its innerHTML content, and then fade it back in once the new content has been added. I have successfully managed to replace the element's content using the .html() method ...

Chrome DevTools automatically translates all HEX color codes into their corresponding RGB values

Chrome DevTools now automatically converts all HEX colors to RGB values, even if the color is set in CSS or using DevTools. Is there a way to prevent this conversion? I know about the method of holding down Shift + clicking the color icon to convert color ...

Unable to alter or eliminate the grey background using material-ui

Struggling with a React application that incorporates material-ui. Despite my efforts, I can't seem to get rid of an unwanted grey background in one section of the app. Attempted solutions: body { background-color: #ffffff !important; } * { b ...

Sliding Dropdown Menu with Dynamic Title Change Upon Selection

I'm having an issue with a dropdown menu on hover that slides down and then back up. The active list item serves as the title of the dropdown. When a user clicks on an <li>, it becomes active and receives the class active-markup-style which sets ...

What could be the reason why both the add and remove functions are unable to work simultaneously within a JavaScript function?

Hi there! I recently started diving into JavaScript and encountered a little hiccup. I've been working on a dice game where images change randomly whenever a button is clicked. The images transition from one to another, but I wanted to add a rolling ...

Guide on positioning a span element to the left using the margin auto property in CSS for Angular 4

Having trouble with moving numbers highlighted to the left with names in CSS. I've tried using flex direction and margin auto but can't achieve the desired result. https://i.sstatic.net/kRJOb.png Here is my HTML code: <section class="favorit ...

Implementing VueJs Vuetify Autocomplete feature with clickable hyperlinks

I am working with Vuetify and I want to create an autocomplete feature similar to the one on their official site. However, I am encountering some issues: The items value is not appearing in the return list and I am unsure how to display the links. Here i ...

Designing the autocomplete dropdown feature in Bootstrap UI

I'm currently developing an app that is utilizing AngularJS and Bootstrap, with the assistance of Bootstrap UI. Specifically, I am working on implementing the typeahead feature. However, I am facing a challenge in trying to ensure that the dropdown li ...

Webpack and Keycloak Integration: Content blocked because of MIME type ("text/html")

I am currently using VueJS for my frontend with Keycloak handling authentication, along with Webpack to bundle my code. Upon the initial application load, if the user is not authenticated, they are redirected to the Keycloak page. During this process, an ...

Utilizing Vue.js: accessing a global value within a template string

I am working on a simple Vue.js application with the following structure: index.html (only the <body> for brevity) <div id="root"></div> main.js var settings = { title: 'My App', } new Vue({ el: '#root', tem ...

Obtaining material for optimizing a dynamic image

I'm facing a challenge with my responsive image setup. The image is filling the top half of the screen as intended, but I can't seem to get the content underneath it to stay below the image as it resizes. Instead, the content ends up overlapping ...

How can I use a Vue.js function to extract latitude and longitude data from a nested JSON file?

When using getcoords(), I am only getting the first latitude and longitude for all entries in my array. However, when using the normal syntax {{fire.latitude}},{{fire.longitude}}, I am able to retrieve all latitudes and longitudes. I'm not sure why t ...

What is the best way to dynamically update or display unique CSS styles when a service is invoked or provides a response in AngularJS using JavaScript

Seeking to display a unique CSS style on my HTML FORM prior to invoking a service in my code and then reverting back after receiving the response. I have implemented the use of ng-class to dynamically add the class when the boolean activeload1 is set to tr ...