Vue: update data and trigger function upon completion of animation transformation, utilizing animation transformation completion listener

Check out this straightforward Vue example: https://codesandbox.io/s/sleepy-haze-cstnc2?file=/src/App.vue

https://i.stack.imgur.com/zboCb.png

Whenever I click on the square, it not only changes color but also expands to 200% of its original size with a 3-second transition period for scaling.

https://i.stack.imgur.com/ignib.png

I'm looking to figure out how to turn the square green once the scaling animation is finished using Vue.

<template>
  <div id="container">
    <div
      id="box"
      v-on:click.stop="this.clickAction"
      :class="this.isClicked ? ' changed' : ''"
    ></div>
  </div>
</template>

<script>
export default {
  name: "App",
  components: {},

  data() {
    return {
      isClicked: false,
    };
  },

  methods: {
    clickAction() {
      console.log("click");
      this.isClicked = true;
    },
  },
};
</script>

<style>
#container {
  height: 100vh;
  width: 100vw;

  display: flex;
  justify-content: center;
  align-items: center;
}

#box {
  background-color: brown;
  padding: 2em;
  margin: 2em;

  width: 100px;
  height: 100px;
}

#box:hover {
  cursor: pointer;
  opacity: 80%;
}

.changed {
  background-color: cyan !important;

  transform-origin: center;
  transition: transform 3s;

  transform: scale(2);
}
</style>

Answer №1

You have the option to assign names of classes instead of using a boolean value in the isClicked property and incorporate the setTimeout function to delay for 3 seconds:

new Vue({
  el: "#demo",
  data() {
    return {
      isClicked: '',
    };
  },
  methods: {
    clickAction() {
      this.isClicked = 'changed';
      setTimeout(() => {
        this.isClicked = 'stopped';
      }, 3000)
    },
  },
})
#container {
  height: 100vh;
  width: 100vw;

  display: flex;
  justify-content: center;
  align-items: center;
}

#box {
  background-color: brown;
  padding: 2em;
  margin: 2em;

  width: 100px;
  height: 100px;
}

#box:hover {
  cursor: pointer;
  opacity: 80%;
}

.changed {
  background-color: cyan !important;

  transform-origin: center;
  transition: transform 3s;

  transform: scale(2);
}
.stopped {
  background-color: green !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div id="container">
    <div
      id="box"
      v-on:click.stop="clickAction"
      :class="isClicked"
    ></div>
  </div>
</div>

Alternatively, you can utilize ref and listen for the transitioned event:

new Vue({
  el: "#demo",
  data() {
    return {
      isClicked: '',
    };
  },
  methods: {
    clickAction() {
      this.isClicked = 'changed';
      const elem = this.$refs.box;
      elem.addEventListener('transitionend', event => {
        if (event.target === event.currentTarget) {
          this.isClicked = 'stopped';
        }
      });
    },
  },
})
#container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}
#box {
  background-color: brown;
  padding: 2em;
  margin: 2em;
  width: 100px;
  height: 100px;
}
#box:hover {
  cursor: pointer;
  opacity: 80%;
}
.changed {
  background-color: cyan !important;
  transform-origin: center;
  transition: transform 3s;
  transform: scale(2);
}
.stopped {
  background-color: green !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div id="container">
    <div
      ref="box"
      id="box"
      @click="clickAction"
      :class="isClicked"
    ></div>
  </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

I'm encountering an error with the *ngIf directive in my Angular reactive form - any ideas on

Despite my reputation, I have a question that is causing me some confusion. I am trying to create a reactive form using an example on Stackblitz. While everything seems fine in Stackblitz and the code works correctly there, I encounter an error in VS Code ...

What is the most effective way to dynamically incorporate external input into a SlimerJS script?

Could use some assistance with SlimerJS. My current program requires intermittent input from stdin to proceed with the next task. The code below functions effectively when using PhantomJS+CasperJS for reading external input, but encounters difficulties wi ...

Is it possible to incorporate an editable text feature within react-moveable? (Allowing for both dragging and editing capabilities)

In React Movable box, the text can be dragged with a left click and edited with a right click using the "contentEditable" attribute. However, on smartphones, editing seems to be disabled and only dragging is possible. <div className="move ...

Upon initiating npm start in my React application, an error was encountered: internal/modules/cjs/loader.js:834

Upon downloading my React course project, I proceeded to install dependencies and run npm start. To my dismay, I encountered the following error: PS C:\Users\Marcin & Joanna\Desktop\react-frontend-01-starting-setup> npm start &g ...

Guide on sending two values from a form using Ajax and the POST method

How can I send two values, A and B, from a form to check.php using Ajax with the POST method? In the code snippet below, I managed to send two input values to check.php and assign them to variables $A and $B. However, I want to accomplish this without ref ...

Can you identify the language of this HTML include code snippet?

Recently, I came across some .html files that include tags in a different format: [% INCLUDE '/path/to/footer.html' %] This bracket-and-percent-sign tag is unfamiliar to me. Does anyone know what it is used for? ...

Stop users from signing up if the chosen username is already in use

I have a script that successfully checks if a username is available, but even if the username is already taken, the user can still register. I am looking for a way to prevent registration if the username is not free. Here is the code snippet: index.php $ ...

File remains visible after deletion upon refreshing the page, but disappears after using the Ctrl + F5

After deleting a file on the server, I noticed that it still appeared when I refreshed the page. However, when I tried to do a hard refresh by pressing ctrl+F5, it resulted in a 404 error. This behavior is puzzling to me - shouldn't a simple refresh s ...

Attributes in HTML for EditorFor() within ASP.NET MVC

Is there a way to include HTML attributes in EditorFor()? For example: <%= Html.EditorFor(model => model.Control.PeriodType, new { disabled = "disabled", readonly = "readonly" }) %> I prefer not to rely on metadata for this. Update: I fou ...

The slider thumb is not showing up properly on Microsoft Edge

Hey there! I'm experiencing an issue with the range slider's thumb display in Microsoft Edge. It appears to be positioned inside the track rather than outside as intended. Take a look at this snapshot for clarification: https://i.stack.imgur.co ...

Utilizing one JavaScript file and consistent classes for multiple modals

Is it possible to have multiple modals using the same JS file, classes, and IDs? I created this code: <button id='myBtn'>Order/Discount</button> <div id='myModal' class='modal'> <div clas ...

Using jQuery Datepicker, showcase two distinct datepickers on a single page

Currently, I am utilizing the jQuery [Datepicker][1] on a website, but I am facing challenges when attempting to display two datepickers on the same page in different manners. The first one should only appear once you click the text box, and once a date i ...

I'm looking for a way to retrieve an object from a select element using AngularJS. Can

I am working with a select HTML element that looks like this: <select required="" ng-model="studentGroup"> <option value="" selected="">--select group--</option> <option value="1">java 15-1</option> <option value="2">ja ...

Maintain parent hover effect while child is being hovered over

After exploring various solutions to fix an issue, I've hit a roadblock. Adding CSS and jQuery code seemed promising at first, but upon refreshing the browser, a new problem emerged. The parent element retained its hover state background color, but th ...

Utilizing Local Storage in Vuex Store with Vue.js

I have been working with localStorage for storing and retrieving items in my JavaScript code housed within a .vue file. However, I am now looking to find a way to transfer this stored data into my Vuex store, specifically within the mutations section locat ...

Autocomplete failing to provide a valid response, returning a null value instead

Utilizing an Autocomplete feature for employee search, users can input a name and select from the list of results. However, the current onChange function logs the index value instead of the selected employee's name. Is there a way to pass the employee ...

Troubleshooting: Select2 Search Functionality Error

After working with the select2 plugin for some time, everything seemed perfect until now. I have a page with 3 selects that load data and function properly, as well as multiple selects inside a popup. The appearance is good, but the search field is not al ...

The placeholder text is not displaying with bullets in Internet Explorer. However, it is functioning correctly in Chrome

I need assistance displaying the placeholder text in IE8 as shown below: "Any relevant reference numbers, such as Direct Debits: - Name of the Branch (if applicable) - What was the original problem - Date the problem occurred " While it appears correct ...

How can I alter the columnWidth setting in Masonry when detecting a different screen width or window width?

I am currently working on adjusting the layout of my masonry script to respond accordingly when the browser window or screen width drops below a certain threshold. However, I am running into issues as it seems to break whenever I try to make this adjustmen ...

Using the concept of method chaining in JavaScript, you can easily add multiple methods from

Hey there! I'm looking for some assistance with dynamically building a method chain. It seems like it should be pretty straightforward if you're familiar with how to do it... Currently, I am using mongoose and node.js to query a mongo database. ...