The issue with the transition element not functioning properly in the hamburger menu is being encountered while using Tailwind CSS alongside

I am currently working on creating a responsive navigation bar in nuxt3/vue.

<template>
  <nav
    class="flex justify-between items-center w-[92%] mx-auto gap-4 border border-2 border-black p-2"
  >
    <div>
      <span class="text-xl">Javascript</span>
    </div>
    <div
      class="absolute md:relative p-4 left-0 bg-yellow-500 left-0 w-full top-[9%] md:top-[0%] transition-all duration-300"
      :class="{ hidden: isActive }"
    >
      <ul
        class="flex md:flex-row flex-col items-center gap-[4vw] justify-center text-xl"
      >
        <li>
          <NuxtLink> 1 </NuxtLink>
        </li>
        <li>
          <NuxtLink> 2 </NuxtLink>
        </li>
        <li>
          <NuxtLink> 3 </NuxtLink>
        </li>
      </ul>
    </div>
    <div class="flex flex-row items-center justify-center text-center gap-4">
      <span class="flex items-center justify-center text-center text-xl"
        >Find us!
      </span>
      <div class="md:hidden cursor-pointer">
        <Icon
          @click="toggleMenu"
          :name="
            isActive
              ? 'material-symbols:format-list-bulleted-rounded'
              : 'material-symbols:close'
          "
          size="40px"
        />
      </div>
    </div>
  </nav>
</template>

<script setup>
import "animate.css";
import { ref } from "vue";
const isActive = ref(true);
function toggleMenu() {
  isActive.value = !isActive.value;
}
</script>

However, I'm facing issues with the transition after clicking the menu icon. I have tried using different tags and even included the "animate.css" library. Can I apply transitions to the display property or only to opacity (as tailwind creators may have intended)? Why isn't animate.css working if not using Tailwind CSS? I aim for a smooth, simple, and clean transition effect.

Answer №1

When the display property of an element changes from or to none within the same render frame, no transitions will occur. If you still desire a transition effect, consider toggling between visibility: hidden and visibility: visible. This approach makes the element non-interactable and interactable respectively, allowing for a transition on properties like opacity:

const { ref } = Vue;

Vue.createApp({
  setup() {
    const isActive = ref(true);
    function toggleMenu() {
      isActive.value = !isActive.value;
    }
    
    return {
      isActive,
      toggleMenu,
    };
  },
}).mount('#app');
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.3.4/vue.global.prod.min.js" integrity="sha512-39BSQXI5q1XlvVhLfFRidKG8KM6Tr6VS/XSnNo6N/A0ZXexHCeoUI/s+ulujQy3UREjoLNrMnFat8VI0mMugWA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" integrity="sha512-c42qTSw/wPZ3/5LBzD+Bw5f7bSF2oxou6wEb+I/lqeaKV5FDIfMvvRp772y4jcJLKuGUOpbJMdg/BTl50fJYAw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdn.tailwindcss.com/3.3.3"></script>

<div id="app">
  <nav
    class="flex justify-between items-center w-[92%] mx-auto gap-4 border border-2 border-black p-2"
  >
    <div>
      <span class="text-xl">Javascript</span>
    </div>
    <div
      class="absolute md:relative p-4 left-0 bg-yellow-500 left-0 w-full top-[9%] md:top-[0%] transition-all duration-300"
      :class="{ 'invisible opacity-0': !isActive }"
    >
      <ul
        class="flex md:flex-row flex-col items-center gap-[4vw] justify-center text-xl"
      >
        <li>
          <a> 1 </a>
        </li>
        <li>
          <a> 2 </a>
        </li>
        <li>
          <a> 3 </a>
        </li>
      </ul>
    </div>
    <div class="flex flex-row items-center justify-center text-center gap-4">
      <span class="flex items-center justify-center text-center text-xl"
        >Find us!
      </span>
      <div class="md:hidden cursor-pointer">
        <icon
          @click="toggleMenu"
          v-text="
            isActive
              ? 'material-symbols:format-list-bulleted-rounded'
              : 'material-symbols:close'
          "
          size="40px"
        ></icon>
      </div>
    </div>
  </nav>
</div>

Another option is utiliing the Vue built-in <Transition> component for adding or removing elements in the DOM, mimicking the behavior of display: none:

const { ref } = Vue;

Vue.createApp({
  setup() {
    const isActive = ref(true);
    function toggleMenu() {
      isActive.value = !isActive.value;
    }
    
    return {
      isActive,
      toggleMenu,
    };
  },
}).mount('#app');
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.3.4/vue.global.prod.min.js" integrity="sha512-39BSQXI5q1XlvVhLfFRidKG8KM6Tr6VS/XSnNo6N/A0ZXexHCeoUI/s+ulujQy3UREjoLNrMnFat8VI0mMugWA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" integrity="sha512-c42qTSw/wPZ3/5LBzD+Bw5f7bSF2oxou6wEb+I/lqeaKV5FDIfMvvRp772y4jcJLKuGUOpbJMdg/BTl50fJYAw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdn.tailwindcss.com/3.3.3"></script>

<div id="app">
  <nav
    class="flex justify-between items-center w-[92%] mx-auto gap-4 border border-2 border-black p-2"
  >
    <div>
      <span class="text-xl">Javascript</span>
    </div>
    <Transition
      enter-from-class="opacity-0"
      leave-to-class="opacity-0"
    >
      <div
        class="absolute md:relative p-4 left-0 bg-yellow-500 left-0 w-full top-[9%] md:top-[0%] transition-all duration-300"
        v-if="isActive"
      >
        <ul
          class="flex md:flex-row flex-col items-center gap-[4vw] justify-center text-xl"
        >
          <li>
            <a> 1 </a>
          </li>
          <li>
            <a> 2 </a>
          </li>
          <li>
            <a> 3 </a>
          </li>
        </ul>
      </div>
    </Transition>
    <div class="flex flex-row items-center justify-center text-center gap-4">
      <span class="flex items-center justify-center text-center text-xl"
        >Find us!
      </span>
      <div class="md:hidden cursor-pointer">
        <icon
          @click="toggleMenu"
          v-text="
            isActive
              ? 'material-symbols:format-list-bulleted-rounded'
              : 'material-symbols:close'
          "
          size="40px"
        ></icon>
      </div>
    </div>
  </nav>
</div>

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

Tips for navigating through a webpage by scrolling

My goal is to automatically scroll down to the bottom of the page and then perform a specific action. With the help of uiautomator, I was able to retrieve the following information: index=2, resource-id=com.manoramaonline.arogyam:id/pager,class=android.sup ...

The Philosophy Behind Structuring Node.js Modules

There appears to be a common understanding regarding the directory structure in node.js, but I have not come across any official documentation on this topic. Based on my exploration of open source projects, it seems that most projects typically include a ...

How can I make my webpage unselectable except for specific content within a div using CSS and JavaScript?

During a live editing session, I am looking to make only specific portions of the page selectable. Can someone provide guidance on how to disable selection for everything except within a particular div? ...

Navigate to the end of the progress bar once finished

I have a solution that works, but it's not very aesthetically pleasing. Here is the idea: Display a progress bar before making an ajax call Move the progress bar to the end once the call is complete (or fails) Keep the progress bar at 90% if the aj ...

Reposition the element at the bottom of its parent element

I am facing an issue with the HTML structure in my application: <div class="work-item"> <div class="image-container"> </div> <div class="text-container"> </div> </div ...

Is there a way to tally up the number of green items and display a <p> tag when every item has been marked green?

I have created a checklist that includes checkboxes. I am looking to display some text when all the checkboxes are checked and green. Can someone assist me with writing the code for this functionality? $(document).ready(function() { $("i").click(funct ...

Adding an active class to a selected list item can easily be accomplished by targeting the

Hey there, I'm attempting to apply a class to the selected list item and also add a class when scrolling to a specific div. For instance, if I scroll to div#six, the number six (6) in the menu should also have the 'active' class. [Check out ...

playing with JSON data in angular

Currently, I am utilizing AngularJS and making use of $http.get to fetch a JSON response which I then assign to $scope.myObjects. After implementing ng-repeat="object in myObjects" in the HTML, everything seems to be functioning properly. My query pertai ...

An object rotating in a loop with Three.js will not cast a shadow over the entire scene

Why are some cubes in my loop not casting shadows? Despite using a directional light that should cast shadows on all cubes, the shadowing stops after around 5 columns. let dirLight = new THREE.DirectionalLight(0xFFFFFF, 1.5); dirLight.position.set(300, - ...

conceal or reveal a button based on the authentication status of a user in a

I'd like to display a "Become Tutor" button if either: 1. The user is not logged in. 2. The logged-in user is not already a tutor. Otherwise, I want to show the "Tutor Dashboard" button for users who are already tutors. While my code is functional, i ...

Adjust Javascript to modify the URL by assigning a new URL without utilizing the origin as the parent

Currently, I am working on a script that is responsible for sending string values to the server in order to change the ipv4 information on a specific device. However, I am facing some difficulties and would appreciate some assistance with automatically upd ...

What is the most efficient method for conditionally rendering components in React?

I'm currently in a dilemma about how to render a component based on a certain condition in React. Which way is considered the best practice? Your expertise would greatly assist me as I navigate through this decision-making process. First approach: co ...

Steps for efficiently incorporating a template within another template

Just starting out with VueJS... I'm trying to figure out a way to dynamically add other components into a main component based on the dropdown selection. The main template always remains on the screen and includes a dropdown. What I want is to have a ...

Determining the selected option's value made by the user

On my index.php file, which contains the form and function, I have the following code: $(function() { $.ajax({ type: "POST", url: "invtype.php", data: "getrevenuetype=true", success: function(b){ $("#revenue ...

Refreshing select2 dropdown options dynamically with AJAX updates

I have a select dropdown for locations that is initialized using select2 on page load. I am looking to dynamically update the data in the dropdown at regular intervals using ajax calls. However, when I attempt to update the data in select2, the dropdown ...

Accessing Data from the Wikipedia API

After receiving a JSON response with the following structure: { "batchcomplete": "", "query": { "pages": { "97646": { "pageid": 97646, "ns": 0, "title": "Die Hard", "extract": "Die Hard is a 1988 ...

Angular HttpClient not recognizing hashtag

I'm trying to make a REST API call, but running into issues with the developerId parameter being sent incorrectly: let developerId = "123#212"; let url = \`\${Constants.BASE_URL}\${marketId}/developers/\${developerId}\`; retur ...

Troubleshooting problem with image loading in AngularJS using ng-repeat

Recently delving into using AngularJS in my projects has presented a rather significant issue when utilizing ngRepeat to load thumbnails from a dynamic array into a DIV. While I won't dive deep into the entire application's details here, let me ...

The page is not able to be uploaded successfully, receiving a HTTP 200 status

Currently, my application is running in Express and I have a button that sends a POST request to the server. After receiving a 200 OK response, the HTML page is displayed. The issue I am facing is that even though I can see the HTML payload in Firebug, my ...

Tips for combining all included files into one with Babel

My current project involves the use of Babel. Within my server.js file, I have the following line of code: import schema from "./data/schema"; The issue arises because data/schema.js is written in ES2015 syntax. After attempting to compile my server.js ...