Pressing the mouse on the div causes it to vanish until an animation is triggered

I am working with two objects: object1 and object2.

object1 is set to have an animation delay of 0s

object2 has an animation delay of 0.5s

When you click on 1., both objects are assigned the class .animation, which works as expected. However, object2 waits for 0.5s before animating, which is the desired behavior.

The issue arises when you click on 2.. Both objects are assigned the class .animation-back, object1 behaves correctly, but object2 disappears for 0.5s before animating.

I understand that this happens because the .animation class is being removed at the same time as adding the .animation-back class, causing a delay. Removing .removeClass('animation-back') triggers a loop or unexpected behavior.

$(".text1").click(function(){
  $(".object1").addClass('animation').removeClass('animation-back');
  $(".object2").addClass('animation').removeClass('animation-back');
});
$(".text2").click(function(){
  $(".object1").addClass('animation-back').removeClass('animation');
  $(".object2").addClass('animation-back').removeClass('animation');
});
a{
  font-size:150%;
}
.text1{
  cursor:pointer;
  margin-left:42px;
  font-size:150%;
}
.text2{
  cursor:pointer;
  margin-left:42px;
  font-size:150%;
}
.object1{
  width: 100px;
  height: 100px;
  background: red;
  margin:2em;
  margin-left: 150px;
  position: relative;
  opacity: 0;
}
.object1.animation{
  animation: animation1 0.7s ease-in-out 0s 1 forwards;
}
.object1.animation-back{
  animation: animation2 0.7s ease-in-out 0s 1 forwards;
}
.object2{
  width: 100px;
  height: 100px;
  background: red;
  margin:2em;
  margin-left: 150px;
  position: relative;
  opacity: 0;
}
.object2.animation{
  animation: animation1 0.7s ease-in-out 0.5s 1 forwards;
}
.object2.animation-back{
  animation: animation2 0.7s ease-in-out 0.5s 1 forwards;
}

@keyframes animation1 {
  from { margin-left: 150px; opacity: 0; }
  to { margin-left: 0px; opacity: 1; }
}
@keyframes animation2 {
  from { margin-left: 0px; opacity: 1; }
  to { margin-left: 150px; opacity: 0; }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text1"><a>1.</a></div>
<div class="text2"><a>2.</a></div>
<div class="object1"></div>
<div class="object2"></div>

Any advice would be greatly appreciated. Thank you and have a wonderful day!

Answer №1

It seems that the issue lies in your default class making the box opacity 0 and the animation having a delay of .5s, causing a half-second delay before the opacity returns to 1. To resolve this, simply update the object 2 class to include the default state of the object.

.object2.animation-back {
    opacity: 1;
    margin-left: 0px;
    animation: animation2 0.7s ease-in-out 0.5s 1 forwards;
}

Answer №2

By setting the animation-fill-mode to forwards, you are instructing it to maintain only the final animation state after completion. If you prefer to display the initial state as soon as it is applied to an element, set animation-fill-mode to backwards (or both for both forwards and backwards).

.object2.animation-back {
    animation: animation2 0.7s ease-in-out 0.5s 1 both;
}

https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode


On a related note, consider using just one set of keyframes and adjusting the animation-direction to reverse if you want to reverse the animation.

.object2.animation {
    animation: animation1 0.7s ease-in-out 0.5s 1 both;
}
.object2.animation-back {
    animation: animation1 0.7s ease-in-out 0.5s 1 reverse both;
}

https://developer.mozilla.org/en-US/docs/Web/CSS/animation-direction

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

Adding key/value pairs to an array of objects in RxJS Observables can be easily

I currently have an Angular app with state management that retrieves data from a database in observables. Here is an example of the interfaces I am working with: interface Service { id: number, name: string, category_id: number, } interface Category ...

JavaScript/CSS manipulation: The power of overriding animation-fill-mode

When animating text using CSS keyframes, I use animation-fill-mode: forwards to maintain the final look of the animation. For example: #my-text { opacity: 0; } .show-me { animation-name: show-me; animation-duration: 2s; animation-fill-mod ...

Arranging the letters in sequence through regular expressions in PHP

I attempted to use a regular expression in php/javascript to match the order of letters. My task was to find a 4 letter word where the first two letters are in order and the second two letters are also in order, like BCEF. I wanted to accomplish this usin ...

Tips for activating text selection in PDF.js

Struggling to enable text selection in PDF.js after successfully displaying a PDF? Seeking a simple example to guide you through the process? Despite trying various approaches, I haven't achieved the desired outcome yet. My current code snippet is a ...

Guide to enabling picklist editing in the specified component within an Angular application

Currently utilizing the p-picklist component and looking to customize the displayed data in the target list span. You can find the source code at: Is there a way to enable editing of the product name within the target list? <p-pickList [source]=&quo ...

Is it possible that the JSON is formatted correctly but there is an issue with parsing it in JavaScript?

const plantDisease={ "apple_scab": { "symptoms": "Leaves covered in a dark velvet layer, showing velvety olive-green to black spots", "cause": "Venturia inaequalis", "natural_control": "Utilize resistant varieties like Prima, Priscilla, Sir P ...

Customized Pagination Text for DataTable

In an effort to customize the display options for jQuery DataTables, I implemented the following code snippet. It allows me to modify the default selection of showing 12, 24, 36, or 48 records per page instead of the usual preset values of 10, 25, 50, and ...

Receive characteristics in component specification

Is there a way to pass the native attributes of an img element to a custom image component without explicitly defining each one, like alt, and have them all be applied to the img tag? Here is an example of what I mean: @Component({ selector: 'image ...

What is the best way to add data to a URL in an ActionResult Method using window.location.href?

I am having trouble understanding how to retrieve data using window.location.href = '/Product/Success/'+data.OrderTrackNo+'';. I am able to get data using ajax, but it seems different when retrieving data with window.location.href, whic ...

Adjusting the visible options in ngOptions causes a disruption in the selected value of the dropdown menu

I have successfully implemented a feature that allows users to convert temperature values displayed in a drop-down menu to either Celsius or Fahrenheit. For this functionality, I am using a select input with ng-options as shown below: <select ng-model ...

Automatically open Bootstrap dropdown upon loading the page

Displayed below is my dropdown menu styled using bootstrap: <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="posuvnik">15min <strong class="caret"></strong></a> <ul class="dropd ...

Is there a way to retrieve the id of an input field using PHP?

I am faced with a situation where I have an HTML input field that is specified with a unique id. My goal is to use this input field id to identify the field effectively. For example: <input type='text' id='float' name='attribu ...

Submitting form data from an HTML page to a specific Skype account is a straightforward process that can be easily accomplished

[Situation] A new feature is being considered for a feedback form that would allow users to input their name, email address, subject, and message. A unique option being proposed is the ability for users to submit their messages via Skype. If this option is ...

Restrict the number of choices in a drop-down menu using Angular

Is it possible to limit the number of results displayed in a dropdown select tag and add a scrollbar if there are more options? Check out my code below: <select class="form-control" name="selectedOption" [(ngModel)]='selectedOption'> ...

Is it possible to bind browser keyboard scrolling to a div without explicit instructions?

Here is a question that closely relates to this one: Enable div to scroll by keyboard without clicking I am aware that explicitly focusing on the element will enable the desired behavior. My inquiry is whether there is a way to achieve this implicitly. ...

When using server-side rendering in React with AJAX, an error may occur if trying to call setState

In order to display data to the user, I rely on the response from the AJAX request as I store all necessary information in the database. My component GenericPage.jsx: export default class GenericPage extends React.Component { componentWillMount() { ...

Guide to creating jQuery event handlers for active elements

After encountering a frustrating issue with Internet Explorer where an input cursor remained visible behind a div with z-index, I came across a solution here. However, I wanted to convert the solution into jQuery code. Original JavaScript: document.query ...

How can we map a promise that resolves to foo to a new promise that resolves to bar?

I am working on a function that uses an XMLHttpRequest to retrieve data and returns a promise with the response. But now I want to modify it so that the promise only contains a specific string from the response. Instead of resolving to response = {status ...

The use of Ajax post results in the retrieval of multiple arrays containing objects that possess various values

I have a PHP file (ajax.php) that retrieves messages from a database and a JavaScript file (main.js) that sends an AJAX request to this PHP file. My goal is to create a table row in the JS file for each message returned by the PHP file. Main.js: functio ...

Looking to integrate Genemu FormBundle autocomplete functionality with querybuilder functionality?

Can I modify this code to work with Genemy FormByndle in order to enable autocomplete in my form: $builder->add('users', 'entity', array( 'class' => 'AcmeHelloBundle:User', 'query_builder' =& ...