Animating Vue lists using Greensock JS hooks allows for seamless transitions by ensuring that the leave animation completes before triggering the enter

How can Vue be configured to ensure that elements leave a list before new ones enter? Currently, the animations for entering and leaving elements in the list occur simultaneously:

var SPEED = 3;

var app = new Vue({
  el: '.container',
  data: {
    nodes: [
      {id: 0, value: 5, name: "Alan"},
      {id: 1, value: 15, name: "Bob"},
      {id: 2, value: 25, name: "Charles"}
    ]
  },
  methods: {
    enter(el, done) {
      var bar = $(el).children(".bar");
      TweenMax.fromTo(bar, SPEED, {width: 0}, {width: $(el).attr("width") * 10, onComplete: done});
    },
    leave(el, done) {
      var bar = $(el).children(".bar");
      TweenMax.fromTo(bar, SPEED, {width: $(el).attr("width") * 10}, {width: 0, onComplete: done});
    },
    swapBobDave() {
      this.nodes = [
        {id: 0, value: 5, name: "Alan"},
        {id: 2, value: 25, name: "Charles"},
        {id: 3, value: 35, name: "Dave"}
      ];
    },
    reset() {
      this.nodes =  [
        {id: 0, value: 5, name: "Alan"},
        {id: 1, value: 15, name: "Bob"},
        {id: 2, value: 25, name: "Charles"}
      ];
    }
  }
});
body {
  margin: 30px;
  font-family: sans-serif;
}

.bar {
  background-color: pink;
  padding: 10px;
  margin: 10px;
}
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <h1>Vue JS Hooks</h1>

<transition-group name="list" tag="div" v-on:enter="enter" v-on:leave="leave" v-bind:css="false" appear>
<div v-for="(node, index) in nodes" v-bind:key="node.id" v-bind:width="node.value">
<div class="bar" v-bind:style="{width: node.value + '%'}">{{node.name}}</div>
</div>
</transition-group>

<button type="button" class="btn btn-default" @click="swapBobDave">Add Dave and remove Bob</button>
  <button type="button" class="btn btn-default" @click="reset">Reset</button>

</div>

Can you provide insights on why the leaving bar doesn't reach width: 0 before disappearing?

Answer №1

When using a transition-group, each change to the array triggers a unique animation. One way to add an element and remove another with a smooth transition is by utilizing a setTimeout on the enter hook, setting the duration to match that of SPEED.

enter(el, done) {

    window.setTimeout( () => { 

       var bar = $(el).children(".bar");

       TweenMax.fromTo(bar, SPEED, {width: 0}, {width: $(el).attr("width") * 10, onComplete: done});

    }, SPEED * 1000 )

},

Another suggestion is to modify your array like this:

swapBobDave() {

    this.nodes.splice(1,1);

    this.nodes.push({id: 3, value: 35, name: "Dave"});

},

Regarding the bar, if it reaches width: 0 but there's a padding: 10px in the CSS, you can resolve this by adding margin directly to the text inside and removing padding from the bar.

I hope this information proves useful to you.

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

Issue with setting .mtl properties in a custom shader in three.js

In my custom three.js application, I am loading an OBJ/MTL model for rendering. I am trying to apply a custom shader to the model, but the color and specular uniforms that I manually pass to the RawShaderMaterial are not updating correctly. Instead, they a ...

Having four videos displayed on one webpage can cause the browser to function at a slower

I have videos that are around 30 seconds long and 15mbs. I'm wondering if it's feasible to include them on a page or if I should opt for cover images instead. I would like to use the video as a separator similar to the design showcased at Does a ...

Changing the owl-carousel height

My website's owl-carousel is currently too tall, exceeding the full screen size. I've explored adjusting the height using the demo provided by owl-carousel for displaying one image per slide and utilizing the latest version of owl carousel 2. Try ...

Issue with opening modal when clicking row action buttons in DataTable (CodeIgniter 3 & Bootstrap 4)

Currently, I am facing an issue with my DataTable in bootstrap 4. I have successfully loaded data onto it, but the problem arises when clicking on a row action button (delete or edit) as their respective modals do not open. Below is the current output of ...

What is preventing me from disabling the ordering feature on my DataTable?

I am facing an issue where I am unable to disable ordering in my JavaScript code. Despite several attempts, nothing seems to work. Below is the code snippet that I have been working on: <script src="//code.jquery.com/jquery-1.12.0.min.js"></scr ...

the primary way JavaScript defines its "this" binding

As I delve into a book, it mentions that this within a function can be established in four different ways: default, implicit, explicit binding, and through the use of the new syntax. Regardless of the method, this is determined at the time of the function ...

Create an AngularJS directive that formats the model value for visual display, while retaining the original value in the model

I am facing a challenge in building an AngularJS directive for a currency textbox. The directive should take values (amount and currency code) from the model in the scope and then apply currency formatting to it. I am struggling to build this directive usi ...

Combining both the Javascript and PHP components of the Facebook SDK

I recently integrated the JavaScript version of Facebook SDK, but now I'm unsure how to also integrate the PHP version without causing any conflicts. Here are my questions: If I implement the PHP SDK from Facebook, is there a way to detect if the J ...

Converting PHP arrays into JavaScript arrays with the help of json_encode()

Is there a way to pass an array from PHP to the JavaScript function console.log()? I'm currently simulating a database and need help with this specific task. Despite not having an array declared in my code, I attempted to use the .getJSON() function w ...

Instructions for loading static HTML content in the browser from a specific directory

Exploring my file hierarchy: public---| |-index.html |-static-| |-static.html After launching the project at localhost:4000, it directs to the public folder and displays index.html. However, I am looking to load static ...

Error Message for Sequelize Validation Fails to Appear as Expected

I am trying to implement Sequelize model validation in order to validate a form field. When the book or author fields are left empty, I want this message to be displayed: Oops! An error occurred. "Title" is required. "Author" is required. The issue I&ap ...

JavaScript Cookie to Ensure Form Submission is Limited to a Single Instance

Seeking assistance with automatically submitting a form on page load using JS cookies. Here is the code I have, but it's not functioning as expected. Any guidance would be greatly appreciated. Thank you. I'm unsure about the section in my code th ...

Connecting the v-model in a Vue.js child component to update the parent value

I've encountered an issue with a vue component where I'm trying to link a text input in the child template to a variable in the parent using v-model, but it doesn't seem to be working. How can I make this work? Currently, I am using Vue.js ...

Halting the execution of the jQuery script

Hidden away from the page, one part of a div is visible with CSS: div{ left: -100px; width: 150px; height: 50px; } To reveal the entire div, I utilized jQuery: $("div").hover(function(){ $("div").animate({left: '0px'}); },func ...

Discover the process for breaking down JSON data into separate stages using the Express API

My API architecture is causing a problem as I try to insert it into an array called items[]. https://i.stack.imgur.com/qe802.png The setup involves a simple API built on express + mongodb. The challenge lies in figuring out how to store data from the pos ...

The Vue.js development server compiler is hitting a roadblock at 49% progress

My Vue JS project keeps getting stuck at 49% or sometimes 62% when I run npm run serve. No matter how long I wait, it never seems to move past that point. I have searched online many times for a solution to this issue, but it appears that no one else is e ...

What is the proper way to define an element's style property in strict mode?

Assuming: const body = document.getElementsByTagName('body')[0]; const iframe = document.createElement('iframe'); iframe.src = protocol + settings.scriptUrl + a; iframe.height = 1; iframe.width = 1; iframe.scrolling = 'no'; ...

Trouble with Callback firing in Select2's 'Infinite Scroll with Remote Data' feature

After reviewing the tutorial on the Select2 project page, I am implementing a feature to load additional records as the user scrolls to the end of the results. <script> $(document).ready(function() { $('#style_full_name').select2({ ...

The MongoDB regex is failing to provide the expected outcome

I'm facing an issue with searching data in MongoDB. I have a table with approximately 5000 entries of data that need to be searched based on multiple columns with specific priority criteria. The first priorities for the search are symbol, name, co_nam ...

Postponing the execution of a controller until all JSON files have been fully loaded

I am currently trying to grasp the concepts of AngularJS, so my question might not be perfect. Is it feasible to delay the execution of a controller until the json files are fully loaded in a separate function? Below is the controller code: app ...