Modifying a leave transition in Vue.js dynamically following the completion of an enter transition

I am dealing with a dynamic transition for a slider element that moves left or right. Vue only allows me to have one transition, so I am using a dynamic transition property like this:

<transition v-bind:name="'slider-' + slideDirection">

This slider has enter and leave transitions that adjust based on the direction it's appearing or leaving to. Everything seems fine, but...

When the element meets the conditions to appear, the enter and enter-to transitions work correctly every time. However, the leave and leave-to transitions do not update if the direction is changed after the element has appeared.

You can see an example of this behavior in this fiddle: https://jsfiddle.net/nxLmdk9q/4/

Everything works in the example, except when you change directions, the element retains the previous direction animation for leave-to.

How can I resolve this issue?

Answer №1

It appears that the slide transition is starting before the slideDirection variable is updated. Consider using the nextTick function to ensure that the slide transition waits for the direction to be updated.

Here's an updated example:

new Vue({
  el: "#app",
  data: {
    slideDirection: "right",
    counter: 0
  },
  methods: {
    moveRight: function() {
      this.slideDirection = "right";
      this.$nextTick(() => {
        this.counter++;
      })

    },
    moveLeft: function() {
      this.slideDirection = "left";
      this.$nextTick(() => {
        this.counter--;
      })
    },
  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
  display: flex;
  flex-direction: column;
  width: 300px;
  height: 150px;
}

div#slider {
  position: relative;
}

div#slider div:nth-child(even) {
  background: green;
}

div#slider div:nth-child(odd) {
  background: red;
}

div {
  padding: 30px;
  width: 100%;
  height: 100%;
  position: absolute;
}

div:odd {
  background: blue;
}

.slider-right-enter {
  opacity: 0;
  transform: translatex(100%);
}

.slider-right-enter-to {
  opacity: 1;
  transition: all 0.5s ease-out;
}

.slider-right-leave-to {
  opacity: 0;
  transform: translatex(-100%);
  transition: all 0.5s ease-out;
}

.slider-left-enter {
  opacity: 0;
  transform: translatex(-100%);
}

.slider-left-enter-to {
  opacity: 1;
  transition: all 0.5s ease-out;
}

.slider-left-leave-to {
  opacity: 0;
  transform: translatex(100%);
  transition: all 0.5s ease-out;
}
<div id="app">

  <div id="slider">
    <transition v-bind:name="'slider-' + slideDirection">
      <div v-if='counter == 0'>
        0
      </div>
    </transition>

    <transition v-bind:name="'slider-' + slideDirection">
      <div v-if='counter == 1'>
        1
      </div>
    </transition>

    <transition v-bind:name="'slider-' + slideDirection">
      <div v-if='counter == 2'>
        2
      </div>
    </transition>

    <transition v-bind:name="'slider-' + slideDirection">
      <div v-if='counter == 3'>
        3
      </div>
    </transition>

  </div>

  <div>

    <button @click="moveLeft">
          &#x3C;&#x3C;
        </button>
    <button @click="moveRight"> 
          &#x3E;&#x3E;
        </button>
  </div>
</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

Attempting to utilize JavaScript in order to reset a text input field

I'm having trouble clearing a text field using this function. The alert is working but the field remains unchanged. What could be the issue? This function is designed to work on any text field. <!DOCTYPE html> <html> <head> ...

Connecting a Date object by using :value and @input within a VueJS component

Successfully linking a date form input with a date object using :value and @input instead of v-model as outlined in this method has been a great ongoing experience. It allows for displaying and modifying the date in the form input, as well as saving the up ...

Unable to invoke any fineUploader functions within a callback function

My autoUpload is currently set to false because I prefer uploading the images manually to my backend. To achieve this, I need the file object first. In the onSubmitted event callbacks, I am attempting to pass the image's ID to the getFile method to re ...

Is there a way to send a promise resolve or reject from a function code within router.post() in Express?

Below is my code in express (node.js) router.post('/example.json', function (req, res) { // getFileInfo is a function to return an array return getFileInfo(req.body.fileList).then((array) => { axios({ method: 'post', ...

Completely triggering a forced refresh on a single page application, disregarding cache, service workers, and all other

After experimenting with service workers on my Vue-built website, I made a critical error that has left Safari on my iPhone displaying only a blank page. To troubleshoot the issue, I connected my phone to my Mac and utilized Safari's inspector tool t ...

How can I make TypeScript properly export function names for closure-compiler?

Here is the TypeScript code I am working with: namespace CompanyName.HtmlTools.Cookie { export function eraseCookie(name:string, path:string) { createCookie(name, "", path, -1); } export function readCookie(name:string) { ...

Creating a dynamic dropdown menu to display nested arrays in Vuejs

I have some important data https://i.sstatic.net/Cq2t6.png Challenge I'm facing difficulty in accessing the tubes array within my data Solution script data() { return { types: [] } }, m ...

Utilizing Vue: Triggering Functions in Sibling/Child Components with Event Handlers

IMPORTANT: I am utilizing Laravel as the backend framework I'm exploring a more interactive approach to adding items to a list without needing to make a server call for each added item. I've come across code snippets where people use window.loc ...

Is it possible to change the CSS of a parent element using a child element?

My current challenge involves altering the background-color of a parent <td> element, without being able to directly modify the HTML structure. The software system utilized at my workplace strictly relies on SQL queries for data manipulation and gene ...

Implementation of Exporting to Excel

function fnshowAuditList() { if(auditListTable) auditListTable.fnDestroy(); jQuery.ajax({ type: 'POST', url: 'auditListAction', data: '', dataType: 'text&a ...

Prevent form submission without JavaScript

While the issue is easy to grasp, it poses a challenge in implementation. I am encountering clients who disable their browser's Javascript by default, causing complications on my website. This is because my website sends ajax requests upon form submis ...

The little DIV strayed beyond the boundaries of its parent DIV

I am facing a challenge with the positioning of nested divs within my parent div. The first two are aligned correctly, but the third one is dropping down outside of the parent div on the y-axis. I have tried various solutions from StackOverflow without suc ...

Automatic closure of Info Window on Google Maps is not functioning as expected

I have recently developed a map which you can view here. The issue I am facing is that when I click on the layers to see the InfoWindow, they do not automatically close when I click on another layer. The JavaScript code I am using is causing this problem. ...

Issue with div element not functioning properly within table declaration on Internet Explorer

There seems to be an issue with the div tag not functioning properly inside a table definition: <table> <tr></tr> <div id="choice"><tr> <td>-------</td> <td>-------</td> <td>-------</td> &l ...

Looking for a simple method to link JSON data to an svg element through Javascript?

Looking to harness the power of SVG for a basic graph creation, but uncertain about the process of assigning JSON data dynamically using a 'for loop' to draw rectangles within SVG. Seeking guidance on setting up 1 loop to assign each value for dr ...

What is the best way to send an array of grouped data to a table

Here's how I organized the array: { "2023-10-01": [ { "emp_id": 1, "name": "Aruna", "code": "DO", "date": "2023-10-01" }, { &qu ...

The navigation bar alignment to the right is malfunctioning

I'm puzzled as to why the navbar-right class is not properly closing out the two navigation bar elements on the right. Despite adding the correct code, it doesn't seem to be working as expected. <nav class="navbar fixed-top navbar-toggleable- ...

Illustrating SVG links

I'm working on a basic svg animation project where I want to create a simple shape by animating a line under a menu link. The goal is to have a single line consisting of a total of 7 anchors, with the middle 3 anchors (each offset by 2) moving a few p ...

What are your thoughts on the combination of Vue.js and Codenvy

I have been attempting to set up a basic Vue.js workspace, but everything I try seems to be unsuccessful. Whether I build it from scratch or use a Git repository, nothing seems to work at all. Is it even possible to get it to work? To Reproduce: Create ...

When I click away from the screen, the bottom border of the button disappears

view my fiddle Here is the HTML code snippet I am working with: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> <button> <i class="fa fa-5x fa-motorcycle gobumpr_icon"></ ...