Animating Vue components for a popup menu

In my Vue project, I am exploring the use of transitions to create a popup menu positioned at the bottom of the sidebar. The closed menu displays the user icon, username, and an arrow that should move downwards when closed and rotate 180 degrees when opened. However, the image remains static in the open position without any rotation.

https://i.sstatic.net/sjqEz.png

.rotatingImage {
  -webkit-transition: -webkit-transform 0.8s ease-in-out;
  transition: transform 0.8s ease-in-out;
}

.rotate {
  -webkit-transform: rotate(180deg);
  transform: rotate(180deg);
}

When the menu opens, the submenus are supposed to slide in from the bottom but currently appear from the right side instead of the declared bottom position using transform: translateY(0) and transform: translateY(10rem).

https://i.sstatic.net/1vIZk.png

.slide-enter-active,
.slide-leave-active {
  transition: 0.4s;
  transform: translateY(0);
}

.slide-enter,
.slide-leave-to {
  transform: translateY(10rem);
}
    <div class="mt-auto">
      <div
        class="w-full flex p-3 cursor-pointer border-t-1 border-b-1 border-blue-sidebarBorder"
        @click="swapUserMenuOpened"
      >
        <img
          src="@/assets/img/sidebar/user.svg"
          alt="userIcon"
          class="mr-3 w-5"
        />
        <p class="text-lg">{{ userName }}</p>
        //THIS IS THE IMG THAT SHOULD ROTATE
        <img
          src="@/assets/img/sidebar/arrowDown.svg"
          alt="userIcon"
          class="mr-3 w-3 ml-auto rotatingImage"
          :class="{ rotate: swapUserMenuOpened }"
        />
      </div>
      <transition name="slide">
        <div v-if="userMenuOpened">
          <router-link
            v-for="item in menuItems"
            :key="item.title"
            class="flex items-center pl-3 cursor-pointer hover-item h-14 transparentBorder mb-0.5"
            :to="item.link"
          >
            <img
              v-bind:src="require(`@/assets/img/sidebar/${item.icon}`)"
              v-bind:alt="item.icon"
              class="mr-3 w-4"
            />
            <p>{{ item.title }}</p>
          </router-link>
        </div>
      </transition>
    </div>

Answer №1

In the code, the click handler

@click="swapUserMenuOpened"
is being used as a variable
:class="{ rotate: swapUserMenuOpened }"
. It would be more efficient to toggle a variable instead. Here's a suggested solution:

data() {
  return {
    isRotated: false
  }
},
methods: {
  swapUserMenuOpened() {
    this.isRotated = !this.isRotated;
  }
}

You can then bind the class to that variable like this:

:class="{ rotate: isRotated }"

-OR-

If swapUserMenuOpened is the boolean you want to toggle (without seeing your component instance, it's hard to know for sure), you can simplify the code to just toggle it on click:

@click="swapUserMenuOpened = !swapUserMenuOpened"

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

Adjust the Size of Bootstrap 4 Dropdown Caret Icon

Currently, I am using the Bootstrap v4 framework on my HTML page. I am looking to resize or increase the caret size in my dropdown list. Does anyone have any tips on how to accomplish this? https://i.sstatic.net/kCNPK.png Below is the code snippet: < ...

jPlayer - Retain user preferences during page redirections

I've been struggling with this task for a few days now. I just can't seem to make it work. It would be greatly appreciated if someone could assist me and guide me in the right direction. My goal is to use jPlayer () to save specific settings as ...

Having trouble implementing CORS in a Slim API

I'm facing challenges in setting up CORS with Slim and AngularJS. AngularJS HTTP Request: $http({ method: 'GET', headers: { 'Content-Type': 'application/json', Accepts: 'application/json&ap ...

Conflict arises between entities due to the speed of one

I am working on creating a scenario in my world where a ball enters a tube with multiple sections. To achieve this, I have constructed the tube using a series of box shapes that I manipulate by moving and rotating them to form the complete structure of the ...

The CSS animation spills out of the div container

I am encountering an issue with my code where the borders are not displaying as expected. The screen is divided into 8 equal parts, each of which should have a ripple effect. While everything works well in Chrome, Mozilla, Opera, and Safari, the ripple eff ...

Can the @keyframes in CSS3 be modified?

Exploring CSS3 animations has been a fun project for me, and I want to push the boundaries of what is possible. Currently, I have an infinite animation cycle that is working perfectly. However, I'm curious if it's possible to dynamically change ...

Creating a Website Optimized for Mobile Devices

As a beginner web developer, I am in the process of creating a mobile-friendly website system. Currently, I am utilizing Bootstrap for responsiveness, PHP5, MySQL, and occasionally Ajax/JQuery. Recently, I came across JQuery Mobile. While I have been usin ...

Tips for embedding the <img> element inside the <a> element

I'm attempting to place an image within a hyperlink tag. Current code: <div class="Sample"> <a href="http://www.Sample.com"> <img src="http://www.Sample.com/Sloth.jpg"> </a> </div> I wish to add <img class= ...

How to Place Buttons on the Left, Center, and Right in Bootstrap Framework

I'm currently working on developing some pages using bootstrap. In the form at the bottom, I have three buttons - Save, Cancel, and Commit. The issue I'm facing is that while the left and right buttons are perfectly aligned, the Cancel button is ...

selenium.common.exceptions.InvalidSelectorException: Alert: A selector that is invalid or illegal has been provided

Currently, I am facing an issue with writing a script to manage web pages containing multiple elements. Upon clicking on one of these elements, it should open a new window. However, my current script is struggling to identify the element correctly. I requi ...

Ways to guide user after logging out

My Angular front end includes the following code in app.js to handle user logout: .when('/logout', { templateUrl: 'mysite/views/logout.html', resolve: { authenticated: ['djangoAuth', function(djangoAuth){ return ...

JavaScript: Converting an array of strings into an array of objects with proper formatting

After scanning barcodes, I have an array of strings that currently contains the following data: var array = ['NEW', '1111', 'serial1', 'serial2, 'NEW', '2222', 'serial3', 'serial4'] ...

Utilizing Bootstrap 4: The Use of an anchor tag in a button element

I am facing a situation where I have an icon represented by the HTML code <a>, which acts as a link redirecting to a different page. This <a> is nested inside a collapsible button, meaning that clicking on the icon expands the menu before actua ...

When using i18next interpolation in React components, the displayed output may show as [object Object]

In my React application, I am utilizing the next-i18next package. I want to include a React component within my interpolation: import React from 'react'; import { useTranslation } from 'next-i18next'; import { serverSideTranslations } f ...

Overflow of Div Content

I need a way to showcase a collection of around 30 links in a horizontal layout without the links wrapping to a new line if they exceed the width of the parent container. A horizontal scroll should appear for users to navigate through the overflowing links ...

Error encountered when attempting to retrieve item by ID using an undefined getter

resident.js state : { residents: [] }, getters:{ getResidentsById: (state) => (id) => { return state.residents.find(resident => resident.id === id) } } Medication.vue data() { return { medications: [], }, computed: { ...ma ...

Tips on altering the Vue state upon clicking the checkbox

I am currently in the process of developing a To-Do Application utilizing Vue.js, Express.js, and MongoDB. My goal is to modify the status of the items displayed using v-for through the checkbox functionality. The desired outcome is to alter the state of ...

Create a button that matches the dimensions of the background image

I am working with a form that includes a submit button featuring a background image. <button type="button" class="button" alt="Send" onclick="validate_form_newsletter_wide( form )"></button> Everything works well when I define the dimensions ...

What varieties of ajax styles are there?

Although I am still relatively new to utilizing ajax, I have found a lot of success in my endeavors so far. The majority of my ajax calls tend to follow this format: function saveQueryProf(){ var currentDate = new Date(); var date=currentDate. ...

Disable css on an element when scrolling upwards

I am in the process of creating a framework that requires displaying navigation (previous/next) when the second slide becomes active. This works correctly while scrolling down, but now I need to address the case where, while scrolling up from the bottom, t ...