What is the best way to manipulate a CSS animation using JavaScript within a Vue JS project?

I am facing some challenges with implementing a JavaScript function to access a CSS animation. My objective in this code is to make the border spin when the next or previous button is clicked, but I'm struggling to activate the CSS animation.

Here is my HTML:

<template>
<div v-on:click="prev" class="prevButton"><p>&lt;</p></div>
<div class="borderTop"></div>
<div v-on:click="next" class="nextButton"><p>&gt;</p></div> 
</template>

This is the CSS:

.border{
  position: fixed;
  height: 500px;
  width: 500px;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  border-radius: 50%;
  border: 120px solid rgba(0, 0, 0, 0.555);
  transform: rotate(45deg);
  -webkit-animation: spin 2s linear; /* Safari */
  animation: spin 2s linear;
}

@keyframes spin {
  
  0% { transform: rotate(0deg); 
        border-right: 120px solid rgba(255, 255, 255, 0.226);}
  100% { transform: rotate(360deg); }
}

This is the JavaScript:

prev(){ 
  document.querySelector(".borderTop").animate;
}
next(){ 
  document.querySelector(".borderTop").animate;
}

Answer №1

To activate the animation using Javascript, you need to incorporate your animation style within a separate class that can be applied to the div during the animation period.

.animate {
    animation: spin 2s linear;
}

If you want to reverse the animation for the previous button, create another class with animation-direction: reverse;.

prev() {
  document.querySelector('.borderTop').classList.add('animate');
  
  // Remove the animate class after the animation is completed to enable it again when the button is clicked.
  setTimeout(function() {
    document.querySelector('.borderTop').classList.remove('animate');
  }, 2000);
}

next() {
  document.querySelector('.borderTop').classList.add('animate');
  setTimeout(function() {
    document.querySelector('.borderTop').classList.remove('animate');
  }, 2000);
}

Answer №2

let borderElement = document.querySelector("#topBorder");
changeAnimation(){ 
  borderElement.style.animation = "rotate 2s linear";
  borderElement.style.WebkitAnimation = "rotate 2s linear";
}

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

The proper way to close file descriptors on a terminated HTTP connection in a Node.js environment

I am currently experimenting with Node.js, where I am attempting to stream files from the filesystem over HTTP. However, my experience on an OS X Lion machine has been hindered by a buggy Apache Bench (ab) app that prematurely aborts connections. This issu ...

Nested arrays within an array in AngularJS can be displayed using ng-repeat

I am having trouble iterating over arrays within arrays. My goal is to create a vertical menu with button-like functionality, but I'm struggling to make it work. angular.module('NavigationApp',[]).controller('NavigationController&apo ...

Learn the process of eliminating special characters from input or textarea fields with AngularJs!

My attempts to integrate an AngularJS directive to remove special characters from input and textarea fields have been unsuccessful. Despite no errors being reported in the console, I am facing issues with the character count tooltip not displaying numbers ...

What is the best way to link my PHP variable to a JavaScript variable within a While loop?

My goal is to create a website that retrieves information from a MySQL database and displays pictures upon clicking a button. echo "<img align=\"left\"src=\"".$path[$y]."\" alt=\"error\">"; echo "<img align=\"r ...

jQuery Validation for Radio Buttons and Optional Fields

I've been pondering over this issue for a couple of hours now. Currently, my focus is on validating a form using a jQuery plugin. Here's the link to the documentation: http://docs.jquery.com/Plugins/Validation Here's the code I have so fa ...

Is there a way to make the menu overlap the logo on the website?

Within my Ruby on Rails Spree application that utilizes the Twitter Bootstrap gem, I have the following code snippet for the header section: <body class="<%= body_class %>" id="<%= @body_id || 'default' %>" data-hook="body"> & ...

Align child elements in the middle horizontally within a col-lg-4 class using Bootstrap

I am currently working with Bootstrap 4.3.1 and utilizing flex for my page layout. <div class='row d-flex align-items-center'> <div class='col-lg-12'> <div class="form-group row"> ...

Python Scrapy: Extracting live data from dynamic websites

I am attempting to extract data from . The tasks I want to accomplish are as follows: - Choose "Dentist" from the dropdown menu at the top of the page - Click on the search button - Observe that the information at the bottom of the page changes dynamica ...

What is the approach of Angular 2 in managing attributes formatted in camelCase?

Recently, I've been dedicating my time to a personal project centered around web components. In this endeavor, I have been exploring the development of my own data binding library. Progress has been made in creating key functionalities akin to those f ...

Is it possible to tap into the stylus Abstract Syntax Tree (AST) for

Is there a way to access the abstract syntax tree (AST) of the CSS style generated by stylus without re-parsing it using css-parse? I'm curious if the AST of the generated styles can be accessed publicly. ...

AngularJS: comparing the differences between copying and extending

Explaining Different Object Copy Methods : When it comes to copying objects in certain situations, two common solutions are often considered: using angular.copy() or angular.extend(). Challenge I Am Currently Facing : The angular.copy(source, destinatio ...

Leverage CSS variables within the `url('data:image/svg+xml')` function

Here is a CSS snippet I use to create a dashed border around a div: div { width: 100px; height: 100px; border-radius: 16px; background-image: url('data:image/svg+xml,%3csvg stroke="black" width="100%25" height="100%25" xmlns="http://www.w ...

I possess both a minimum and maximum number; how can I effectively create an array containing n random numbers within

Given a minimum number of 10.5 and a maximum number of 29.75, the task is to generate an array within these two ranges with a specific length denoted by 'n'. While the function for generating the array is provided below, it is important to calcul ...

Express Module Paths Failing to Function Properly

When I first started building my routes, I had everything in one api.js file. However, I realized there might be a better approach, so I did some research online to see how others handle it. After following a few tutorials, I decided on a new layout with s ...

Issues with Flexbox not being rendered correctly on Safari 5.1 for Windows

My goal is to design a straightforward box for questions, with the question number on one side and the text on the other. To ensure that the texts are aligned vertically and dynamic, I am experimenting with using flexbox. I have researched both the old an ...

Unable to use console log in shorthand arrow function while working with Typescript

When debugging an arrow function in JavaScript, you can write it like this: const sum = (a, b) => console.log(a, b) || a + b; This code will first log a and b to the console and then return the actual result of the function. However, when using TypeSc ...

Selenium is throwing an ElementNotInteractableError indicating that the element is not able to be

Today I decided to dive into learning Selenium, but I've hit a roadblock while trying to click on an element that looks like this: <a rel="nofollow" style="" href="javascript:void(0);" time="" itemid="15 ...

Reactjs failing to fetch data with Axios

I am currently working on a project using Reactjs/nextjs and trying to fetch data using "Axios". The API URL is successfully fetching the data, but on my webpage, it only shows "There are no records yet". Can anyone help me find where I am going wrong? H ...

Display the matrix in a semi-spiral pattern

I am working with a 3 by 5 matrix, filled with numbers from 1, presented as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The task is to print the values in a half-spiral order, starting from the bottom left vertically: 11 6 1 5 10 15 12 7 2 4 9 ...

What is the best way to attach a function to an href attribute for creating a dynamic link?

I've been struggling to pass a function to the href attribute in my link tag, but no matter what I try, it just doesn't seem to work. It either redirects to a 404 page or is completely unclickable. What could be causing this issue? This link app ...