Divs animated with jQuery keep on moving even after the animation has finished

My current project involves animating a single circle that scales, collides with two nearby circles, and then causes those circles to animate to specific positions. While everything works as expected, there is an issue where the two circles involved in the collision will continue to move slowly away from the main circle even after the animation has finished. I attempted to use .stop(true,true) on the animate function for the middle circle (referred to as 'boss' in my code), but this only prevents the circle from growing without solving the movement issue of the other circles. Additionally, using .finish() on the boss growth animation did not resolve the problem of the other circles continuing to move post-animation.

FIDDLE: http://jsfiddle.net/direlelephant/fMLKZ/2/

UPDATE: This issue persists regardless of whether the position of the divs is set to fixed or absolute.

UPDATE: I also experimented with using .clearQueue(), .stop(true,false), and stop(true) to address the ongoing movement problem. However, .clearQueue() had no effect, while both .stop(true,false) and stop(true) prevented the middle circle from animating altogether.

Answer №1

The issue at hand is the creation of animations within a loop structure. It would be beneficial to modify your code snippet with the following:

 if ( distanceformula( xcoord3, xboss, ycoord3, yboss ) < ((boss.outerWidth() / 2) + (objectify3.outerWidth() / 2)) ) {
              console.log(1)
              $( objectify3 ).animate( {
                left: xmove3 + "px",
                top: ymove3 + "px"
              }, {
                duration:3000,
                step: function() {
                  console.log(2)                  
                }
              });

            }

After making this adjustment, check the console output. You will notice that approximately 1000 animations are being created as indicated by the repeated execution of console.log(1). This results in jQuery sequentially running 1000 animations one after another, leading to a total duration of ~1000 * 3000 seconds for all animations to complete. To address this, introducing a flag mechanism can ensure that animations only run once upon the initial intersection.

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

Guidelines on creating actionable functions using JavaScript

Having trouble connecting the form to the database and looking for a solution. This is the HTML form that I've been working on, attempting both POST and GET methods. <form id="register" action="script/register.js" method="post"> <label for= ...

How can I send a form without having the page reload using a combination of AJAX, PHP

I am struggling to submit a form without refreshing the page. I have tried using ajax as mentioned in some resources, but it's not working for me. What could be the issue? When I use the following code, everything works fine with PHP: document.getEl ...

When designing responsive websites in Bootstrap 4, what is the preferred choice between using the class "container" or "container-fluid"?

When creating responsive designs, which is more effective to use - the .container class or the .container-fluid class? ...

Dynamically loading an iFrame source and automatically populating input forms using Javascript explained

My current challenge involves retrieving URL parameters successfully and using them to decide which iframe src to populate. Additionally, I need to autofill the form created via the form src with other parameters. However, I am faced with two main issues. ...

Could you assist me in retrieving information from an API request?

I can't seem to pinpoint my mistake, but I know it's there. After the user provides their state and city information and submits it, a fetch request is supposed to retrieve latitude and longitude values. These values are then used in another API ...

Need help aligning content with flexbox? Having trouble with align-content and justify-content not working correctly?

First and foremost, I want to express my gratitude in advance for any assistance you can provide. My goal was to neatly align three pieces of content within each row, similar to the image displayed here: https://i.sstatic.net/vtsRj.png To achieve this lay ...

AngularJS - Multi-controller Data Calculation

I am currently in the process of developing an Angularjs application. The project is quite extensive and requires segmentation into multiple Controllers to manage effectively. One challenge I am facing is performing calculations across these controllers. ...

Transmitting data from the front end to the server in React/Shopify for updating API information using a PUT request

After successfully retrieving my API data, I am now tasked with updating it. Within my component, I have the ability to log the data using the following code snippet. In my application, there is an input field where I can enter a new name for my product an ...

What is the best practice for incorporating CSS and JavaScript files into a PHP template?

I've created a basic template for my PHP website, but I'm struggling to find the best way to include my CSS and JavaScript files. Take a look at my index.php file below: <?php include'core/template.php'; $temp=new Template(); $sett ...

The AJAX email submission form is not functioning properly

Recently, I have upgraded my email sign-up form on the website to include validation. However, after adding the validation, the form no longer sends and an error message is not displayed. Upon inspecting, I found the following error: TypeError: null is ...

What is the most efficient and hygienic method for storing text content in JavaScript/DOM?

Typically, I encounter version 1 in most cases. However, some of the open source projects I am involved with utilize version 2, and I have also utilized version 3 previously. Does anyone have a more sophisticated solution that is possibly more scalable? V ...

Having trouble persisting drop down values to the database in Symfony2 when utilizing ajax

I designed a registration page that requires users to input their country details. Users can select the country from a dropdown menu, which dynamically populates the states dropdown list using Ajax. The same process applies for the cities list. I was able ...

Increase the margin for the following item if it has a specific class using CSS

Here is the HTML code that I currently have: <div class="post"> <h2 class="title">TITLE: Kausalya</h2> <p class="content">CONTENT: Shaunaka Shakha</p> </div> <div class="post"> <h2 class="title"> ...

Cross-Origin Resource Sharing (CORS): The preflight request response does not satisfy the access control check

I've been facing an issue with a simple POST method to my API through the browser. The request fails, but when I try the same on Postman, it works fine. The response includes a JSON string and two cookies. In an attempt to resolve this, I set the hea ...

Troubles with proper functionality of antd's DatePicker.RangePicker within a React Function Component

I am currently utilizing React and the antd library, attempting to incorporate the DatePicker.RangePicker into my project. Initially, when I directly embed the RangePicker within the component, everything works smoothly. However, for better code organizat ...

How to Use AJAX to Read a Text File in JavaScript

Having trouble with AJAX! I have successfully set up a marquee on my website, but now I want it to dynamically fetch the text from a text file. The goal is to read the text from the file (which contains only one line) and assign it to a global variable nam ...

Passing a variable to a template in Node.js and express with hbs is causing an unexpected identifier error

I’m working on a Node.js Express app, and I’m trying to pass a variable when rendering my index.hbs file, like this: <!DOCTYPE html> <html> <body> Hello. <a href="/auth/facebook">Login with Facebook</a> {{ ...

Utilizing Material UI Pickers and Formik to efficiently handle Date, Time, and Second components

Currently, I am developing a React application with Formik that requires the utilization of date and time pickers. The app is being constructed using Material-UI and I have been exploring the possibilities provided by Material-UI Pickers at Given my relat ...

Transferring data from JavaScript to PHP using the $.ajax method for storing information in a MySQL database

I am attempting to make a POST call from a JavaScript file to a PHP file in order to insert a variable into a MySQL database. Here are the basic files I am working with: 1° PHP file for sending the call <html> <head> <script ...

fixed divs that remain in place while scrolling

In the past, I have implemented a method similar to this: However, I am not a fan of the "flicker" effect that occurs when the page is scrolled and the div needs to move along with it. Recently, I have come across websites where certain elements (such as ...