Using JavaScript to manipulate CSS Keyframes

Can I control these animations with JS/JQuery instead of CSS hover action?

.button {
  width: 350px;
  height: 300px;
  margin: 100px auto;
  position: relative;
  border: solid 2px #cbd4d9;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}

.button:hover .hoverBtn:before, .button:hover .hoverBtn:after {
  opacity: 1;
  -webkit-animation: open 0.8s;
  animation: open 0.8s;
  animation-fill-mode: forwards;
  animation-timing-function: cubic-bezier(0.39, 0.575, 0.565, 1);
  animation-direction: normal;
}
.button:hover .hoverBtn-bottom:before, .button:hover .hoverBtn-bottom:after {
  opacity: 1;
  -webkit-animation: openB 0.8s;
  animation: openB 0.8s;
  animation-delay: 0.8s;
  animation-fill-mode: forwards;
  animation-timing-function: cubic-bezier(0.39, 0.575, 0.565, 1);
  animation-direction: normal;
}

.hoverBtn {
  width: 100%;
  height: 300px;
  position: absolute;
  top: -1px;
}
.hoverBtn:before {
  position: absolute;
  content: '';
  ...
<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>Square border animation</title>
  <link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>

<body>



<div id="target">
  Click here
</div>


  <div id="myButton" class="button">
    <a href="#">
        <div class="hoverBtn"></div>
        <div class="hoverBtn-bottom"></div>
     </a>
  </div>
  <script>
$( "#target" ).click(function() {
  alert( "Handler for .click() called." );
});
</script>
  
</body>
</html>

All code has been included.

I want to trigger the animation using JS/jQuery instead of on hover.

I've attempted some solutions but need help with implementing class changes.

Sincerely,

Jonathan.

Answer №1

Just to clarify, I decided to write this code in vanilla JavaScript as opposed to using jQuery since my familiarity with jQuery is limited. However, if you prefer, I can look up the jQuery syntax for you. In the meantime, try to understand the concept behind the code.

The key idea here is to implement the following structure:

var button = document.querySelector('#myButton');
if(button.classList.contains('animBorder')){
    button.classList.remove('animBorder');
}else{ 
    button.classList.add('animBorder');
}

You'll need to switch from using :hover directly to applying a class (in this case, '.animBorder'). Then, on click events, check whether the class is active and add or remove it accordingly.

If you need further clarification, feel free to ask, and I'll provide more detailed explanations.

Below is a working example showcasing the code in action:

function _$(str){ return document.querySelector(str); }

_$('#target').addEventListener('click', doClick);

function doClick(e){
  var bcl = _$('#myButton').classList
  bcl[bcl.contains('animBorder')?'remove':'add']('animBorder');
}
.button {
  width: 350px;
  height: 300px;
  margin: 100px auto;
  position: relative;
  border: solid 2px #cbd4d9;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.button.animBorder .hoverBtn:before, 
.button.animBorder .hoverBtn:after {
  opacity: 1;
  -webkit-animation: open 0.8s;
  animation: open 0.8s;
  animation-fill-mode: forwards;
  animation-timing-function: cubic-bezier(0.39, 0.575, 0.565, 1);
  animation-direction: normal;
}
...
@keyframes openB {
...  
}
<div id="target">Click here</div>
<div id="myButton" class="button">
  <a href="#">
      <div class="hoverBtn"></div>
      <div class="hoverBtn-bottom"></div>
   </a>
</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

Production environment experiences issues with Angular animations

In my MEAN stack application, I started with Sails.js. Everything was working smoothly during development, especially with angular-animate. However, once I changed the Sails environment to production, I encountered issues. Grunt is set up to concatenate a ...

Trigger the opening of a bootstrap modal from an external source on the current page

One common question is how to load a modal from another page onto the current page, or how to open a modal on the current page when it loads. My idea is a little different: I want to click on an image hotspot (like a person in a team photo) on the /home p ...

Guide on displaying Solr Documents returned using the Jquery Autocomplete plugin

In my Spring MVC 4.X project with Solr integration for data indexing and querying, I am diving into the world of JQuery and JavaScript. While exploring, I came across a code snippet that directly queries the Solr URL from a JSP page. However, I believe tha ...

The map function will produce a result of -1 when utilizing the $stateParams parameter

When attempting to pass a variable to a page, the URL I'm using is: http://localhost/project/#/app/intro/1 After routing to my controller, everything seems fine until I try to use the map function on an array in my controller - that's where the ...

When a page is being redirected using requestdispatcher in a filter, the CSS contents do not seem to be applying correctly

Upon evaluation of the condition, if it fails, I am utilizing a request dispatcher to redirect to another page. The redirection is successful, however, the CSS is not being applied to the new page. What could be causing this issue? public class RolePrivil ...

What is the best way to remove quotation marks from a string?

I am struggling to remove any quotes (") within <a href> and </a> tags when manipulating a string. I have tried various methods without success. The string resembles HTML but is actually in string form, and I need to eliminate quotes from eleme ...

Eliminate Elements from Array - Angular Four

I am currently developing a basic to-do app using Angular4. The setup of the app is structured as follows: Form Component: Responsible for adding items to the to-do list Item Component: Represents individual to-do items App Component: Contains a *ngFo ...

What is the best way to arrange map items using justify-content-around for optimal display?

I'm currently iterating through products using the map function, but I'm having trouble getting the justify-content-around property to apply. I am working on a React project with Bootstrap 4 integrated. Below is the code snippet: {props.product ...

What causes the unusual behavior of `overflow-x: auto;` when used within a parent element with `flex-direction: row;` styling

I have gone through numerous other queries regarding horizontally scrolling tables, but none seem to address this particular problem. When using a responsive table in the default Blazor Server template of a new project built with Bootstrap 4, the browser ...

Deleting a row from the table with a single click on the X icon

Is there a way to delete individual rows in a table by clicking on a close icon instead of removing all rows at once? Additionally, I am looking for a better method to display rows in a table. You can view my current implementation here: link -> jsfiddl ...

Obtain the details of a selected item in a list by tapping on it using Natives

I am wondering how to retrieve the context of a tapped item in a listview. For example, if there are three items in the list, how can I determine that the second item was clicked and access the data related to that item in the observable array? For instan ...

Having trouble with the functionality of a simple jQuery toggle menu on mobile?

I am experiencing an issue with a simple toggle menu that utilizes jQuery's on: tap feature, but it is not functioning as expected: <nav id="mobile-nav"> <ul> <li>Item 1</li> <li>Item 2</li> ...

I have successfully established a new channel, but I am having difficulty retrieving the unique identifier for it

After using the provided code to create a channel, I'm having trouble locating the channel ID needed for the next step in my function. This function is meant to move to a specific category and send a message to it. const buyid = generateID message. ...

The npm package for google-spreadsheet.js is experiencing issues and is not functioning correctly when trying to replicate the GitHub

After attempting to implement the basic example code provided at https://github.com/theoephraim/node-google-spreadsheet, I encountered an issue. For instance: const { GoogleSpreadsheet } = require('google-spreadsheet') const creds = require(&apo ...

Mongo automatically generates an index with a null key/value pair

After successfully creating a new user using mongoose at a certain route, I noticed that the validation appears in the console and the user is stored in the mongo database. However, when attempting to create a second user with a different username, an erro ...

Looking to prevent printing and saving a webpage directly from the file menu using JQUERY or JavaScript

I am in need of assistance to find a code that can disable the ability to print and save a webpage using JQUERY or JAVASCRIPT ...

Is there a way to determine which radio button has been chosen using jQuery?

I'm trying to retrieve the value of the selected radio button using jQuery. Can anyone help with this? Currently, I am able to target all radio buttons like so: $("form :radio") But how can I determine which one is actually selected? ...

Sorting and dividing an Array using Angular

Forgive me in advance if this sounds like a naive question, as Angular and Typescript are not my strong suits. I am assisting a friend with an issue that I can't seem to overcome. I have an array of players that includes details such as first name an ...

"Using Selenium in Python to navigate Google's search results and view the

Hello, I am new to using selenium and I have a specific task in mind. I would like to scrape the address search results from Google that are displayed on the sidebar column. I have successfully managed to use selenium to conduct searches on Google and land ...

Is it possible to change the title of a QGroupBox using HTML expressions in Python?

Does anyone know how to set the title of a QGroupBox with HTML expressions in a Python program? For example, using ABC for subscript. If you have any insights or solutions, please share! Thank you. ...