CSS animation is activated solely upon its initial creation

Exploring the world of Vuejs for the first time with my debut project.

The core of my project lies in the main component where I have an array as a data variable. Here's an example snippet:

export default {
    name: 'example-component',
    data() {
        return {
            options: [
                {
                    text: {
                        nl: 'Jan',
                        en: 'John',
                    },
                    action: 'x1',
                },{
                    text: {
                        nl: 'Jansen',
                        en: 'Doe',
                    },
                    action: 'x2',
                },
            ],
        }
    },
}

Within the template of this <example-component>, a v-for loop is used inside another component. Here's a glimpse:

<my-other-component v-for="option in options" v-on:click.native="select(option.action)"
    :key="option.id"></my-other-component>

Everything seems to be functioning smoothly.

<my-other-component> includes an animation that triggers when it is first displayed on the screen.

However, when a method in the methods section of example-component empties and repopulates the options array, the initial options lose their animation. Only new options added trigger the animation.

It seems like the animation is tied to the creation of specific indexes in the options array.

Any insights on why this behavior occurs and how to address this issue?

PS: Sharing the (relevant) CSS:

.option {
    animation-timing-function: linear;
    animation-name: option;
    animation-duration: 2.5s;
}

@-webkit-keyframes option{
    0% { opacity: 0; }
    75% { opacity: 0; }
    100% { opacity: 1; }
}

-

EDIT 1:

Adding

animation-iteration-count: infinite
does not resolve the issue. It simply repeats the animation. (-> The 'options' disappear and reappear every 2.5s)

EDIT 2:

Experimenting with timing the deletion and repopulation of options seems to impact the animation behavior. The issue persists despite trying various methods to clear the array.

Answer №1

https://v2.vuejs.org/v2/guide/list.html#Components-and-v-for

Starting from version 2.2.0 of Vue.js, using v-for with a component now requires a unique key.

When Vue.js updates a list of elements generated using v-for, it typically employs an "in-place patch" strategy. This means that if the order of the data items has changed, Vue.js will patch each element in its current position rather than rearranging the DOM elements to match the new order.

To help Vue.js accurately track each node's identity, allowing for element reordering and reuse, a unique key attribute must be provided for each item. It is recommended to use the unique id of each item as the key value.

In essence, failing to assign a unique ID to each data object in the loop and bind it to the corresponding element will result in Vue.js simply patching the data without altering the DOM.

Answer №2

Wouldn't it make sense for the animation to re-run if the class it is tied to is toggled?

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

Server blocking access to Javascript files

Having some trouble with my code, it works fine on Localhost but not on Fasthosts server. Seems to be an access issue to a folder on the server, here are the debug messages: GET (http://reggarockaboogie.co.uk/images/gallery/fld01/) 403 (Forbidden) k.cors ...

Having trouble getting the smooth scroll-behavior to work properly in Tailwind CSS?

I've been working on my portfolio using next.js and tailwind CSS. I've added scroll-behavior: smooth in the globals.css file, and in the Navbar, I used https://i.stack.imgur.com/wqb4t.png I really want to achieve that smooth scrolling effect thr ...

Expanding CSS Grid to Occupy Remaining Area

I'm currently utilizing CSS grid to construct a basic 3 column layout. Here's a snippet of my code... .container { display:grid; grid-template-columns:1fr 1fr 1fr; } ...

JavaScript - Combining nested arrays of JSON data into a single object

I'm looking to convert a nested JSON structure into a single object with dynamic keys. I attempted the code below, which only works for one level. I need help writing a recursive function to handle n levels of nesting. Any advice would be appreciated. ...

Unable to open Vue.js and Vuetify.js Accordion

Incorporating a custom component called blog, I utilized an accordion feature from vuetify.js to display blog posts within the component using another custom component, blog-post. Initially, everything worked smoothly without nesting the components togethe ...

Ways to display text when hovering over an image link

Despite trying various techniques recommended by fellow Stackoverflow users who faced similar issues, I still encountered a problem where the text appeared below the image even after applying the suggested methods to my code. I also experimented with a me ...

Performing String formatting in JavaScript using an array

I've been utilizing the stringformat library to format strings in my node.js applications. var stringFormat = require('stringformat'); stringFormat.extendString(); In my current project, I'm attempting to pass an array of parameters a ...

Data is present in a JavaScript array, yet it is returning a value of

In my quest to create an array of User IDs for an AJAX Post Request, I encountered a strange issue. Despite successfully retrieving and displaying the User IDs individually in console.log, once I push them to the connectionData array, accessing specific in ...

Using PHP script to retrieve MySQL table values and dynamically update a live graph in a Javascript function

I've been researching similar issues for quite some time now, but to no avail. Currently, I'm utilizing Highcharts to update a graph every 3 seconds with the latest entry from a specific MySQL table. I am referring to the example Javascript code ...

JavaScript tag filtering system: Display only the div elements that contain all the specified classes in an array; otherwise, hide them

Can you provide some guidance on accomplishing this task? I am looking to toggle the visibility of a div based on its classes and an array. If the array contains the term something, then only divs with the class something will be displayed. If the array ...

How to successfully configure environment variables in Next.js and deploy them on Netlify

An issue arose after deploying my Next.js application to Netlify using Git. I utilize a .env.local file to store the backend route URL that is used across the entire app for fetch requests. However, post deployment, the process.env.NEXT_PUBLIC_BACKEND_ROUT ...

Ensuring the Correctness of URLs Using Javascript

I have a client-side URL validation code that I am currently using. I am wondering if there is a more efficient way to achieve the same result. My question pertains to the overall approach rather than the specific regex. this.validators.myUrl = function ...

Control the playback of individual HTML5 audio elements using jQuery for seamless user experience

How can I modify the code to ensure that only one HTML5 audio element plays at a time? Currently, all tracks are able to play simultaneously. I am using jQuery for play and pause functionalities. If one track is playing and another is clicked on, how can ...

Utilize range slider to refine dataset

I have implemented a jquery datatable along with the range.slider plugin. My goal is to apply the range slider to filter out data in the last column. In my attempt, I am using search.push to accomplish this filtering process. Below is an example of my i ...

Determine if a webpage is out of focus using jQuery

Is it possible for jQuery/JavaScript to detect if a user is engaged in other activities and not currently looking at this page? Precision is not necessary, just an approximate idea is fine. ...

Store various dropdown selections in an array

Questions are being generated from a database for users to answer using a drop-down menu. Upon selecting a specific option, a suggestion is added to an array triggering a JavaScript on-change event. Once all questions are answered, the array including all ...

The Vuetify v-btn within the v-bottom-navigation remains in an active state even when it has not

I am encountering an issue with a button in the bottom navigation bar of my Vuetify application. I have set up several buttons as routes in Nuxtjs, which become active when the corresponding page is visited. However, there is a button in the bottom nav bar ...

Optimal Placement of CSS and index.html Files in ReactJS with Redux

Currently, my setup for the index.html file looks like this: <!doctype html> <html class="no-js" lang=""> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Pra ...

C# - Issue with Webbrowser failing to fully load pages

I am facing an issue with loading pages completely on the web browser, likely due to heavy usage of JavaScript. To address this problem, I have integrated another browser into the project called Awesomium. I am wondering if Awesomium supports using getEle ...

Tips for effectively utilizing the display: inline property in conjunction with ng-repeat

I am struggling to create a timeline with a broken structure on my website. I suspect that the issue lies in using display:inline. When attempting to apply this to my site: https://i.stack.imgur.com/4Ur7k.png the layout gets distorted: https://i.stack.i ...