Exploring Vue: Creating smooth transitions for dropdown menus and dynamically toggling classes on different elements

Here's a simplified version of what I'm trying to achieve:

<div class="dropdownMenuWrapper">
  <ul class="dropdownMenu">
    <li class="dropdownMenuItem"&rt; Menu 1 </li>
    <button class="arrow" @click="toggleActive">></button>
  </ul>
</div>

<div class="dropdownListWrapper">
  <ul class="dropdownList">
    <li class="dropdownItem">DropdownItem</li>
    <li class="dropdownItem">DropdownItem</li>
    <li class="dropdownItem">DropdownItem</li>
  </ul>
</div>

I am looking to create a dropdown menu that expands downwards with a smooth transition. The height should increase from 0 to perhaps 100px over a 1s transition.

My idea is to toggle the class of dropdownList. Initially, it will have a class with a height of 0, and when the arrow is clicked, it will switch to another class with a greater height.

Now, my question is: How can I change the class of this element using the click event on the arrow?

Answer №1

Answer: Utilize class binding for conditional styling

If it's not specified which version of Vue you are using, let's assume Vue3+.

To achieve this functionality, define a Ref in your script section to reference in the template. In this case, since you are checking for a button click event, use Boolean type.

const isActive = ref(false)

Next, employ class binding by prefixing the class attribute with a colon (:), allowing you to dynamically apply classes based on the value of isActive using the JS Ternary operator

<ul :class="isActive ? 'active-dropdown-list' : 'inactive-dropdown-list'">

Essentially, if isActive is true, the element will have the class active-dropdown-list, otherwise it will be assigned as inactive-dropdown-list

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

The `part:` modifier plays a crucial role in the node import statement

While exploring a javascript/Node project, I stumbled upon this code. Surprisingly, there doesn't seem to be any online resources explaining its purpose and functionality. import PatchEvent, {set, unset} from 'part:@sanity/form-builder/patch-even ...

Numerous toggles paired with various forms and links

I am currently facing an issue with toggling between forms on my website. Although the toggle functionality works fine, I believe I may have structured the steps in the wrong order. My ideal scenario is to display only the 'generating customer calcul ...

Utilizing Javascript to have each line displayed with separate returns upon form submission

How can I make each link unique when placed on separate lines? Line 1 = <a href="LINE 1 CONTENTS" target="_blank">Link - 01</a> Line 2 = <a href="LINE 2 CONTENTS" target="_blank">Link - 02</a> Line 3 = <a href="LINE 3 CO ...

What is the proper way to utilize the dot operator to access a key on an object that starts with :_ in JavaScript?

I am currently working on a project that involves creating a beginner's Nodejs API. As part of the project, I need to send a post request to the API endpoint /api/users/:_id/exercises. When I log req.body, it appears in the following format: ...

Illustrative demonstration of Vue with TypeScript

I am currently working on developing a HelloWorld application using Vue.js and TypeScript. index.html <script data-main="app.js" src="node_modules/requirejs/require.js"></script> <div id="app">{{text}}</div> app.ts import Vue f ...

What is the best way to dynamically assign a random file from a folder as the background image using CSS property in JavaScript?

Algorithm in Pseudo Code: if currentCondition equals snow { aFunction() } else { // execute something different } function aFunction { search for images inside the snow directory at /images/condition/snow select a random jpeg/jpg from th ...

Using JavaScript to pass the href attribute value

On my HTML page, there are multiple links that reload the page when clicked. I want to capture the href value of each link the user clicks and store it in a variable called xhash: Here is the HTML: <a href='#taba' id='passid'>Li ...

Utilizing Jquery/Ajax and PHP sessions for seamless data entry into a Laravel-powered database

As I work on redesigning my web app, I am considering what "Best Practice" might entail. This application allows users to select items from a dropdown list, add them to a queue, and then save the information to the database under their user profile. Curre ...

Is there a maximum number of rows that can be displayed in ReactGrid?

I recently came across this helpful example: ReactGrid Getting Started The example shows an array of People objects with a length of 3 being rendered. However, when I attempt to add several hundred or even thousand additional People objects, only around ...

Why isn't the pulse animation functioning correctly on Edge browser?

Encountering an issue with the pulse animation in Microsoft Edge. Allow me to explain my objective: I have an animated pulsing dot (changing width and height) positioned in the center of a website. The animation works perfectly everywhere except in Edge, w ...

Refreshed page causing hide/show div to reset

Hello there, I'm currently working on a web application and I need to implement some JavaScript code. The application consists of three sections, each with its own title and button. When the button is clicked, a hidden div tag is displayed. Clicking t ...

Best practices for organizing CSS, JS, and image files in a Vue project

Currently I am in the process of transitioning an HTML template to a Vue project. I find myself facing the decision of whether to store my CSS and JS files in the public directory or within the src directory. Any guidance on this matter would be greatly ...

The utilization of the Angular date pipe significantly impacts the way dates are

When I use the pipe date:'MM/dd/YYYY' to display the date 2022-01-01T00:00:00, it shows as 1/01/2021 instead of 1/01/2022. This issue only occurs with this specific date. Why does this happen? The value of pharmacyRestrictionDate is 2022-01-01T0 ...

The IntroJs step is only partially visible on the screen

Currently, I am incorporating introJS into my application and encountering an issue where one of the steps is only partially visible on the screen. Despite trying various position settings such as auto, left, right, etc., this particular item consistentl ...

Article Topic: Alerting Users of Unnecessary CSS File Load in Next.js App

I'm encountering an error with this Next.js code: The code utilizes Shadcn for the UI and Convex as a backend. Here is my folder structure: https://i.sstatic.net/zOvubRg5.png Below is my main layout file: import type { Metadata } from "next&q ...

I'm experiencing an issue where the code I copy from jsfiddle does not function properly when pasted into notepad++

While browsing through the code on this page javascript countdown timer pause resume, I came across the following interesting snippet: var CountDown = (function ($) { // Length ms var TimeOut = 10000; // Interval ms var TimeGap = 1000; var CurrentTime = ...

Generating a random selection of files from a directory and distributing them randomly

In an attempt to create a command for my Discord.js bot that selects a random image from a folder and sends it, I am facing an issue. The array containing the images cannot be set manually due to the constantly changing number of images in the folder. The ...

Struggling to find and interact with a button element, however Selenium Webdriver is throwing a NoSuchElementException

After experimenting with various selectors and attributes in an attempt to target and click a sign-up button on a website's homepage, I have been unsuccessful. The website in question can be found at: When the webdriver is initiated, all actions are ...

Is there a way for me to position my logo in the middle of my navigation bar?

How can I center my logo in the navbar? I've attempted to use mx-auto with Bootstrap 4 Utilities, but it didn't work. It seems like I need to set up some flexbox properties, but I'm not sure which ones to use due to the messy code structure. ...

Executing memory function with JQuery replaceWith() and event handlers

Imagine you have the following HTML code: <button id="click-me" type="button">Click Me!</button> Now, picture this jQuery snippet being executed: var button = $('#click-me'); button.on('click', function() { console.l ...