Issues have been observed with the keyframe animation involving a JavaScript timer, specifically in the rotation of the outer circle around the inner circle

I seem to be encountering an issue with an animation. My goal is to have three arrows in a circle that rotate around an inner circle and pause every 3 seconds for another 3 seconds before continuing to rotate. However, instead of rotating smoothly, the animation seems to skip at times, making it look disjointed. Any help on this matter would be greatly appreciated, and I hope my explanation is clear enough.

HTML

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container animation-banner-container">
    <div class="row">
        <div class="col-md-6">
            <div class="outCircle">
                <div class="rotate">
                    <div class="counterrotate">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6"></div>
    </div>
</div>

CSS

.animation-banner-container {
    height: 450px;
    margin-top: -5%;
    overflow: hidden !important;
}

.outCircle  {
    width: 400px;
    height: 400px;
    background: url('https://gdxdesigns.com/wp-content/uploads/2020/12/Outer-Text.png');
    background-size: 400px 400px;
    left: 175px;
    position: absolute;
    top: 50px;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;
    border-radius: 100px;
}

.rotate {
    width: 400px;
    height: 400px;
    padding: 100px !important;  
    background: url('https://gdxdesigns.com/wp-content/uploads/2020/12/inner-wheel.png');
    background-size: 400px 400px;
}

.counterrotate {
    width: 400px;
    height: 400px;
    background: url('https://gdxdesigns.com/wp-content/uploads/2020/12/temp-img.png');
    background-size: 400px 400px;
    padding: 150px !important;
    margin: -50%;
}

@keyframes circle-one {
    from { transform: rotateZ(0deg); }
    to { transform: rotateZ(120deg); }
}

@keyframes circle-two {
    from { transform: rotateZ(120deg); }
    to { transform: rotateZ(240deg); }
}

@keyframes circle-three {
    from { transform: rotateZ(240deg); }
    to { transform: rotateZ(360deg); }
}

@keyframes ccircle-one {
    from { transform: rotateZ(360deg); }
    to { transform: rotateZ(240deg); }
}

@keyframes ccircle-two {
    from { transform: rotateZ(240deg); }
    to { transform: rotateZ(120deg); }
}

@keyframes ccircle-three {
    from { transform: rotateZ(120deg); }
    to { transform: rotateZ(0deg); }
}

JS

 var seconds = 3000;
    var tid = setInterval(mycode, seconds);

    var start = 1;
    var slides = 1;

    function mycode(val) {
    if(!val) {
    
        /* Controls the Stop and Start */
        if(start == 1) {
            $(".rotate").css({"animation-play-state": "paused"});
            $(".counterrotate").css({"animation-play-state": "paused"});
            start = 0;

        } else {
            $(".rotate").css({"animation-play-state": "running"});
            $(".counterrotate").css({"animation-play-state": "running"});
         
            if(slides == 1) {
                console.log("Check Slides 1: " + slides);
                $(".rotate").css({"animation": "circle-one 3s infinite linear"});
                $(".counterrotate").css({"animation": "ccircle-one 3s infinite linear"});
                slides = slides + 1;
            } 
            
            else if (slides == 2) {
                console.log("Check Slides 2: " + slides);
                $(".rotate").css({"animation": "circle-two 3s infinite linear"});
                $(".counterrotate").css({"animation": "ccircle-two 3s infinite linear"});
                slides = slides + 1;
            }

            else if (slides == 3) {
                console.log("Check Slides 3: " + slides);
                $(".rotate").css({"animation": "circle-three 3s infinite linear"});
                $(".counterrotate").css({"animation": "ccircle-three 3s infinite linear"});
                slides = 1;
            }
            start = 1;
            
        } // Close else
        /* Close Controls the Stop and Start */

    } else {
        abortTimer();
            tid = setInterval(mycode, seconds);
        }
    }

    function abortTimer() { // to be called when you want to stop the timer
        clearInterval(tid);
    }

JSFiddle

Answer №1

Creating timed animations using JavaScript can be quite challenging due to unpredictable latency when the script is preoccupied with other tasks. This makes it unreliable for precise timing.

Instead, I recommend utilizing pure CSS for smoother and more dependable results!

Check out a sample test page at:

In the example, I've demonstrated how to replace an image with a CSS-only div, eliminating the need for a temporary image file and reducing loading time significantly.

For further information on background animations, visit: CSS Keyframe Animation with Delay Between Iterations on css-tricks.com.

Answer №2

After getting a fresh perspective on the problem, I was able to identify the issue (shoutout to Conor). The solution involved simply changing "infinite" to "forwards" in the animation code:

Previous Code Snippet

            if(slides == 1) {
                console.log("Check Slides 1: " + slides);
                $(".rotate").css({"animation": "circle-one 3s infinite linear"});
                $(".counterrotate").css({"animation": "ccircle-one 3s infinite linear"});
                slides = slides + 1;
            } 
            
            else if (slides == 2) {
                console.log("Check Slides 2: " + slides);
                $(".rotate").css({"animation": "circle-two 3s infinite linear"});
                $(".counterrotate").css({"animation": "ccircle-two 3s infinite linear"});
                slides = slides + 1;
            }

            else if (slides == 3) {
                console.log("Check Slides 3: " + slides);
                $(".rotate").css({"animation": "circle-three 3s infinite linear"});
                $(".counterrotate").css({"animation": "ccircle-three 3s infinite linear"});
                slides = 1;
            }

Updated Code Snippet

            if(slides == 1) {
                console.log("Check Slides 1: " + slides);
                $(".rotate").css({"animation": "circle-one 3s forwards linear"});
                $(".counterrotate").css({"animation": "ccircle-one 3s forwards linear"});
                slides = slides + 1;
            } 

            else if (slides == 2) {
                console.log("Check Slides 2: " + slides);
                $(".rotate").css({"animation": "circle-two 3s forwards linear"});
                $(".counterrotate").css({"animation": "ccircle-two 3s forwards linear"});
                slides = slides + 1;
            }

            else if (slides == 3) {
                console.log("Check Slides 3: " + slides);
                $(".rotate").css({"animation": "circle-three 3s forwards linear"});
                $(".counterrotate").css({"animation": "ccircle-three 3s forwards linear"});
                slides = 1;
            }

I have also made updates to my JSFiddle demo.

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

Simplify an array for utilization in D3

Currently working on extracting data from mongoDB using javascript and React. In order to utilize D3.js effectively, I need to flatten the array. Presently, my data looks like this: [{ "location": [ { "createdAt": "2023-04 ...

The Gear S device is experiencing issues transmitting data through a POST request, and no error message is being

Currently, I am in the process of creating a Gear S application (and eventually for S2) that gathers data and forwards it to my server using a POST request. Below is snippet of the code responsible for sending the data: $.post(URL_POST_DATA, { ...

HTML dropdown menu to trigger an action with the submitted URL

I need assistance creating a menu of options with corresponding URL actions. Here is the structure I have in mind: <form> <select> <option value="1">1</option> <option value="2">2</option> <option value="3 ...

Adjust the size of the bootstrap container's width

I'm looking to modify the width of the .container div in Bootstrap 4. The original CSS code is as follows: @media (min-width: 1200px) .container { max-width: 1160px; } If I were to include the following code in my file, would it potentially creat ...

Discover the process of attaching an event to the keyboard display within a Cordova application

I've exhausted my efforts trying to figure out how to assign an event for when the virtual keyboard appears on my hybrid cordova app. I'm looking to trigger a specific action whenever the keyboard shows up in my app consistently. ...

Removing div elements containing specific text using jQuery

Hello everyone, I am trying to remove all divs that contain a specific part of a string. Does anyone know how I can do this? This is what my original HTML looks like: <div class="article-options_4"></div> And this is the new HTML structure, ...

Comparative analysis within the JSON object

I have a sample JSON object with the following format: var data = [{"value":"S900_Aru","family":"S400"}, {"value":"S500_Aru","family":"S400"}, {"value":"2610_H","family":"A650"}] The first two values are related to the same f ...

Creating a tiny arrow using CSS

Can anyone advise on the best way to create a small arrow design using CSS? I have tried using a regular closing > arrow, but it's not quite what I'm looking for. https://i.stack.imgur.com/7uLMG.png ...

Modifying an HTML list item to become 'active' in a navigation bar

One way I've been implementing my navbar on each page is by using the following code at the bottom of the page within script tags: $("#navbar-partial").load("navbar.html). The code for the navbar list looks like this: <ul id="main-nav" class="nav ...

Is there a way to eliminate the bottom border on the active navigation tab?

Here's the information I've gathered: Below is a snippet of code that I have: .nav-tabs { border-bottom: 3px solid #3FA2F7 !important; } .nav-tabs .nav-link { color: #02194A; font-size: 13px; padding: 12.5px !important; } ...

Node.js Project Using a Specific URL and Parameter

Two things are on my mind: 1. I'm trying to set up my project's URL as 127.0.0.1:8080/param=id, but I've been unsuccessful so far despite attempting the following: app.get('/param=id', function(req, res) { console.log(req.param ...

In angularJS, setting the route provider is done by using the .config method

I have implemented AngularJS in my code. The main HTML file is called "mainhtml.html". <!DOCTYPE html> <html data-ng-app="demoapp"> <head> <script src="scripts/angular.min.js"></script> <title>Angular js</ti ...

unable to locate the font file I recently downloaded in the Windows terminal

Interested in customizing your Windows terminal? I recently decided to change my font style and downloaded the desired one. However, despite seeing the font in control panel and trying the "downloading for every user" option, my terminal still can't l ...

Unable to retrieve value 'includes' from null object

Currently, I am utilizing Vue.js along with JavaScript. In my code, there is an array of objects named products, each containing a special property called smallest_unit_barcode. My goal is to filter out only those products that have a barcode similar to a ...

Enhance the Error class in Typescript

I have been attempting to create a custom error using my "CustomError" class to be displayed in the console instead of the generic "Error", without any success: class CustomError extends Error { constructor(message: string) { super(`Lorem "${me ...

Cease the continuous scrolling of the display element now

I'm having trouble with my progressbar animation when scrolling. It seems to run multiple times instead of just once. I apologize if there's an error in my code. Could someone please assist me? This is the code I am using: <div class="demo- ...

Upon redirection, my JavaScript codes cease to function properly and require a page refresh to resolve

Upon redirecting to another page with included JS codes, my main.html does not function properly until I refresh the page. What steps can be taken to resolve this issue? 0.html <body data-theme="b"> <div class="p2" data-role="page" id="p2"> ...

Modify css using jquery for multiple ids assigned to the same class

I have a set of 3 divs all sharing the same class. <div id="qq1" class="cl">sentence</div> <div id="qq2" class="cl">sentence</div> <div id="qq3" class="cl">sentence</div> I am interested in finding out how to properly ...

JavaScript is unable to identify the operating system that is running underneath

Even though I am on a Windows system, the browser console is showing that I am using Linux. function detectOS() { const userAgent = navigator.userAgent.toLowerCase(); if (userAgent.includes('win')) { return 'Windows' ...

Angular service provided by MetronicApp

I've been working on integrating Angular services with the Metronic App. This is how I defined my service: angular.module('MetronicApp').service('shaperService', ['$http', function ($http) { this.shapers = function(param ...