What is the best way to incorporate a color-changing animation into a countdown border, resembling a spinner?

I am looking to implement a timer using jQuery that will start once this circle on the page is reached. The idea is for the border color (currently white) to progressively change to red, resembling a loading spinner effect commonly seen online. After reaching 10 seconds, the animation should reset, possibly pausing when hovered over. Since I am new to JQuery and animations, I am unsure where to even begin with this task.

Below is the code snippet I have been working with:

<div id="circle-wrapper">
    <div id="circle">
         <h2>Some Text</h2>
    </div>
</div>

#circle-wrapper {
position: absolute;
left: 0;
width: 100%;
margin-top: 80px;
}
#circle {
border-radius: 50%;
border: 5px solid #fff;
max-width: 550px;
height: 550px;
margin: auto;

Answer №1

Forget about jQuery, there's a CSS-only solution for creating spinners and other animations known as css animations

for example

    .loader {
      margin: 6em auto;
      font-size: 10px;
      position: relative;
      text-indent: -9999em;
      border-top: 1.1em solid rgba(255,0,0,0.2);
      border-right: 1.1em solid rgba(255,0,0, 0.2);
      border-bottom: 1.1em solid rgba(255,0,0, 0.2);
      border-left: 1.1em solid #ffffff;
      -webkit-animation: load8 1.1s infinite linear;
      animation: load8 1.1s infinite linear;
    }
    .loader,
    .loader:after {
      border-radius: 50%;
      width: 10em;
      height: 10em;
    }
    @-webkit-keyframes load8 {
      0% {
        -webkit-transform: rotate(0deg);
        transform: rotate(0deg);
      }
      100% {
        -webkit-transform: rotate(360deg);
        transform: rotate(360deg);
      }
    }
    @keyframes load8 {
      0% {
        -webkit-transform: rotate(0deg);
        transform: rotate(0deg);
      }
      100% {
        -webkit-transform: rotate(360deg);
        transform: rotate(360deg);
      }
    }
<div class="loader"></div>

For more information on animations and keyframes, visit: w3school

Answer №2

I am thrilled with the outcome of this project, as it required a significant amount of effort to achieve using only CSS. It should be noted that I have utilized the webkit vendor prefix, so the result may not be compatible with browsers other than webkit.

Click here to view the demo

Below is the HTML code:

<div class = "cont">
    <div id="circle"></div>
    <div class = "q q1-cover"></div>
    <div class = "q q1"></div>
    <div class = "q q2"></div>
    <div class = "q q3"></div>
    <div class = "q q4"></div>
    <div class = "cutout"></div>
</div>

And here is the accompanying CSS styling:

* {
    box-sizing:border-box;
}

body {
    margin:0;
}

.cont {
    position:relative;
    width:100px;
    height:100px;
    margin:100px 0 0 100px;
    background:pink;
}

.cutout {
    background:white;
    border-radius:100%;
    width:80px;
    height:80px;
    position:absolute;
    top:0;
    left:0;
    bottom:0;
    right:0;
    margin:auto;
    z-index:100;
}

#circle {
    border-radius: 100%;
    background:red;
    width: 100px;
    height: 100px;
    position:absolute;
    top:0;
    left:0;
    bottom:0;
    right:0;
    margin:auto;
}

.q {
    position:absolute;
    width:50%;
    height:50%;
    background:white;
    -webkit-animation-timing-function:linear;
    -webkit-animation-iteration-count:1;
    -webkit-animation-duration:1s;
    -webkit-animation-fill-mode:forwards;
}

.q1-cover {
    background:red;
    bottom:50%;
    left:50%;
    border-radius:0 100% 0 0;
    -webkit-animation-name:q1cover;
    -webkit-animation-delay:1s;
}

.q1 {
    border-radius:0 100% 0 0;
    top:0;
    right:0;
    transform-origin:0% 100%;
    -webkit-animation-name:q;
}

.q2 {
    border-radius:0 0 100% 0;
    bottom:0;
    right:0;
    transform-origin:0% 0%;
    -webkit-animation-name:q;
    -webkit-animation-delay:1s;
}

.q3 {
    border-radius:0 0 0 100%;
    bottom:0;
    left:0;
    transform-origin:100% 0%;
    -webkit-animation-name:q;
    -webkit-animation-delay:2s;
}

.q4 {
    border-radius:100% 0 0 0;
    left:0;
    top:0;
    transform-origin:100% 100%;
    -webkit-animation-name:q;
    -webkit-animation-delay:3s;
}

@-webkit-keyframes q1cover {
    0% {  }
    100% { z-index:10; }
}

@-webkit-keyframes q {
    0% { transform:rotate(0deg); }
    99% { opacity:1; }
    100% { transform:rotate(90deg); opacity:0; }
}

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

Incorporate the {{ }} syntax to implement the Function

Can a function, such as toLocaleLowerCase(), be used inside {{ }}? If not, is there an alternative method for achieving this? <div *ngFor="let item of elements| keyvalue :originalOrder" class="row mt-3"> <label class=" ...

Please refer to the image located in the directory that is one level above the current location

My current setup includes an image located at images\a.jpg and a script in js\a.js. Both the images and js directories are under the www document root directory. Within the script a.js, I have the following code snippet: var provider_img = $(th ...

Ways to retrieve ng-options values and trigger a function upon change of the value within?

In the code I am working on, there is a need to load ajax data by making a call to a third-party API with a URL containing a variable parameter. This parameter should be sourced from the ng-options of a select drop-down, but for some reason it is not worki ...

Guide to navigating through a webpage to locate the specified element with Selenium

Is there a way to ensure that when scrolling to an element in selenium, it appears at the very top of the browser window rather than just being visible on the page? What steps can be taken to make sure that the element being scrolled to is positioned at t ...

Is there a way to replicate this exact toggle selection functionality from Angular Material using just HTML and CSS?

I'm attempting to incorporate this functionality into my Angular project ( link ), but I'm facing challenges with styling it using CSS. Could this be due to limitations in overriding the default settings? I came across this helpful resource: sour ...

What could have caused this issue to appear when I tried running ng build --prod?

Issue encountered while trying to build the ng2-pdf-viewer module: An error occurred in the webpack loader (from @angular-devkit/build-optimizer) with the message: TypeError: Cannot read property 'kind' of undefined. This error is related to A ...

Dynamic bullseye displayed on a Vis.js network node

I am currently utilizing Vis.js to create network diagrams. I am interested in adding a notification feature where when an AJAX update is received for a specific node, the canvas will move that node to the center (which Vis.js can already do). Following th ...

The CSS formatting is not being properly applied within the innerHTML

I have a scenario where I am trying to display a Bootstrap card using innerHTML in my TS file, but the styles are not being applied to this content. I suspect that the issue might be because the styles are loaded before the component displays the card, cau ...

jQuery: Remember to re-bind the form when it is returned successfully

Just a quick question: I'm currently using the jQuery.forms.js plugin. My situation involves a form that posts to a PHP page and receives data in JSON format. The returned data contains code for a new form, which replaces the original form used to s ...

Unable to delete a segment of text within the description of each individual article division using jQuery

In an effort to clean up the article descriptions on my website's homepage, I am seeking a solution to remove the {AC} text that appears at the beginning of each description. While attempting to use the jQuery code provided below, I encountered an iss ...

Utilize AJAX to update the quantity

I have attempted to update the rows in a table using AJAX, and it works for the first row but not for subsequent rows. Can someone assist me in implementing this functionality with AJAX for multiple rows? Here is an example code snippet from cart.php: &l ...

Adaptable placement of background across screen widths

I am facing an issue with three buttons that have the same height and width, text, and background icons on the right. On small screens or when there is only a single word in the button, I want to move the icons to the bottom center of the button. Is there ...

Upon page refresh, products are automatically added to the cart

While developing a shopping cart code, I encountered an issue where refreshing the page automatically added a product to my cart without any action from me. For instance, my website displays three products: Apple, Banana, Orange. When I click on Apple, it ...

Creating a personalized v-for loop with v-if to apply a specific class to a div element

How can I correctly utilize v-if when using v-for inside? I am aiming to implement a condition where if the index is 0 or it's the first data, then add an active class. <div class="item active" v-for="(item, key, index) in slideItem" :key="item._ ...

Click on the links to view various captions for a single image, one at a time

My goal is to create an interactive image similar to what can be found at . (Click Play and navigate to page 5 to see the interactive physical exam). While this example seems to use Flash, I am interested in achieving a similar effect using javascript/jQue ...

Maintaining the layout of two columns in Bootstrap 4, responsive design turns them into a single stacked

Currently using Bootstrap v4 with a two-column layout that splits the container in half. On larger screens, the items in each column display correctly. However, on smaller screens, Bootstrap shuffles the items from column 2 into column 1. I am looking to ...

Web GUI for insert, update, and delete functionalities

After repeatedly creating Add/Edit/Delete/List GUI's, I have grown weary and frustrated with the process. Surely, there must be a free package available that can simplify this tedious task. I envision something like the following code snippet: { ...

Place the HTML onto a fresh page

After rendering a control in asp.net code and converting it to HTML on a button click, the next step is to transfer the HTML to a new page in order to display all the information it contains. Is there a specific control in asp.net that can be utilized in ...

Arranging divs for dynamic movement with window resizing

Seeking a solution to have words on my background image function as links, I planned to place transparent divs over the words in the background. However, struggling with positioning these divs accurately as the window is resized. Code Snippet: <div id ...

Long pressing on text in iOS fails to display the 'copy' button on jQueryMobile Dialogs if ajax is being employed

One of the Dialog boxes that suddenly appears is the subject of my discussion: http://jquerymobile.com/test/docs/pages/page-dialogs.html (shortened URL for mobile use ) If you click 'Open Dialog,' you will notice that nothing can be copied in i ...