Having trouble getting CSS3 transitions to work properly with v-for in Vue2

Utilizing transitions in Vue2, I incorporated a transition in the CSS. The Vue template displays two boxes - one utilizing v-for and an array, while the other uses a variable approach. It seems that btn2 is effective, but btn1 is not working as expected.

<style lang="sass">
        .item
            width: 120px
            height: 120px
            background-color: bisque
            transition: margin-left 500ms
    </style>
    <template>
        <div>
            <div class="item" v-for="item in list" :key="item.index" :style="{marginLeft: item.index + 'px'}">{{ item.value }}</div>
            <div class="item" :style="{marginLeft: left + 'px'}">123</div>
            <button @click="addone">btn1</button>
            <button @click="addtwo">btn2</button>
        </div>
    </template>
    <script>
        export default {
            name: 'Heap',
            data() {
                return {
                    left: 100,
                    list: [
                        {
                            value: 12,
                            index: 10
                        }
                    ]
                }
            },
            methods: {
                addone() {
                    this.list[0]['index']+=10
                },
                addtwo() {
                    this.left+=10
                }
            }
        }
    </script>
    

Answer №1

Your implementation includes the code snippet :key="item.index" within the first div element. This code modifies the index value within the same element, triggering a re-render when the key's value changes.

The absence of animation in your case is due to the fact that instead of incrementing the CSS dynamically, you are essentially causing the element to be re-rendered with the updated CSS.

Key attributes in Vue.js assist in identifying nodes within a list efficiently. They aid Vue in determining which nodes can be retained and patched, and which ones require re-rendering.

To optimize performance, consider using a static, unchanging value as a key. In this context, I have introduced an id property to your object and utilized it as the key value for demonstration purposes.

<style lang="sass">
.item
  width: 120px
  height: 120px
  background-color: bisque
  transition: margin-left 500ms
</style>
<template>
  <div>
    <div
      v-for="item in list"
      :key="item.id"
      class="item"
      :style="{marginLeft: item.index.toString() + 'px'}"
    >
      {{ item.value }}
    </div>
    <div class="item" :style="{marginLeft: left.toString() + 'px'}">123</div>
    <button @click="addone">btn1</button>
    <button @click="addtwo">btn2</button>
  </div>
</template>
<script>
export default {
  name: 'Example',
  data() {
    return {
      left: 100,
      list: [
        {
          id: '1',
          value: 12,
          index: 10,
        },
      ],
    };
  },
  methods: {
    addone() {
      this.list[0].index += 10;
    },
    addtwo() {
      this.left += 10;
    },
  },
};
</script>

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

Problem selecting a specific item within a dropdown menu

Currently, I am exploring Element UI, a visually appealing VueJS framework for user interfaces. One issue I have encountered is when placing a select element inside a dropdown menu, as selecting an item in the select element automatically closes the dropdo ...

Seeking guidance on how to perfectly align my collapsible navigation bar for a university assignment

Currently, I am tackling a project for my college course, and the navigation bar is posing some challenges. Despite conducting various research attempts to resolve the issue, the site remains quite basic as it stands as just a template. Primarily, my objec ...

When hovering, the :after element will transition to the current div

How can I make the pink color div move vertically in front of the currently hovered div when we hover on it? Any suggestions on how to achieve this effect? https://i.sstatic.net/yfwgf.png $(document).ready(function() { $(".timeline-entry").hover(fu ...

Struggling to align content properly in Vue-Typed-JS (Nuxt, Vuetify) app

Currently in the process of building a PWA using Nuxt and Vuetify, with a typing animation from vue-typed-js. https://github.com/Orlandster/vue-typed-js#default-import Facing some challenges trying to center the typing content on the page. <template&g ...

Thick labels in Chart.js are shortened with three dots

Is there a way to show the entire label without it getting truncated with 3 dots? I have experimented with different options from https://www.chartjs.org/docs/master/axes/index, including padding, but so far I haven't been successful. The full date da ...

Arranging pagination elements for table stacking on smaller screens

Below is a code snippet showcasing the table pagination elements and styles I'm using. The layout looks great on medium and larger screens, but when viewed on smaller screens, the pagination elements wrap to the next line while the counts remain float ...

The text on the video background does not appear to be showing up when the video background

I'm currently working on a WordPress theme and encountering an issue that I can't seem to resolve. I've integrated Syd Lawrence's background video plugin from https://github.com/sydlawrence/jquery.videoBG to add a video background, but ...

Retrieving all buttons from a webpage and removing those with a specific background-image using JavaScript

Hey everyone, I'm still learning about javascript and other web development languages. My current task is to locate all the buttons on a webpage and remove the ones that have a specific background image. I know about using getElementsByTagName but n ...

Utilize the method of the existing Vue component as the default value for a prop

Passing a function as a property to a Vue component can be done by using the @click directive. However, if you want to maintain the default behavior of the component, which relies on data from its state (such as props, data, or other methods), you may need ...

Enhancing Accessibility of the 'Return to Top' Link

Currently, I am designing a web page that requires users to scroll extensively. To enhance the user experience, I have included a back-to-top link at the bottom of the page for easy navigation back to the top. This is the HTML markup I have implemented: ...

I'm encountering an issue configuring the API and front-end on the same domain with nginx

I recently set up a virtual machine on Azure and tried to configure a front-end in Vue along with a back-end in Django. However, I encountered an issue where both of them cannot seem to work under the same domain. Below is my nginx configuration for Vue: ...

Obtaining a specific CSS value access

Looking for some help on how to access and use a string value ("#E0ED44") as a background color in my div. Can't seem to find any answers on gg. ...

Is there a way to instruct the code to select between two variables and align their values?

I have encountered a challenge while working on a unit test. The website I am testing is dynamic, causing the CSS Selector and xpath to frequently change. As a solution, I came up with the idea of using both CSS and xpath to retrieve the same text. Curren ...

What is the best way to add an image underneath a partially see-through layer?

I've searched high and low but can't seem to find a solution that works for my issue! On my website, there is a green background with a form in the center of it. You can take a look at the jsFiddle example here. My goal is to have the image loca ...

I require a browse button on my JSP page that allows users to select folders, not individual files

In search of ideas for my JSP page, I am looking to browse a folder rather than just a file. Can anyone assist with this requirement? ...

Eliminating padding or margin for figures in Bootstrap on smaller screens

I've been struggling to create a fullscreen image on small devices without any margin or padding. Unfortunately, the solutions suggested in this thread didn't work for me. Below is the code I'm currently using (tested on https://jsfiddle.n ...

What is the interaction between CSS and URL fragments (the #stuff)?

Can URL fragments affect CSS behavior? For instance, let's consider a sample page at: http://example.boom/is_this_a_bug.html. You can view the code for this page on https://gist.github.com/3777018 Upon loading the page with the given URL, the element ...

Vue's unshift method only appends new items to the beginning of an

Could you help me with a problem similar to this one: The array I am working with looks like this: remate: {id:1, lotes: [{id:1, nombre: lalala}, {id:2, nombre: lololo}]} My issue arises when attempting to add new items to remate.lotes. While the push m ...

Mysterious Gap between an <img> Tag and a div

My <img> is positioned next to an inline block div, causing some design issues. I attempted to set the <img> to display:block, but that solution breaks the overall layout. Here is the snippet of HTML code: ...

Leveraging Vue.js with Bootstrap4 modal components

I've been attempting to implement a modal that appears with a simple change in my data using a vue @click event on an element, but for some reason, the modal isn't showing up. I could really use some guidance on how to make this work. Just to cl ...