Ways to Loop the Animation Endlessly for a Loading Spinner

Looking to animate a line infinitely on my website, here is the code snippet:

<body>
    <div class="line_1"></div>
    <div class="line_2"></div>
    <div class="line_3"></div>
</body>

The CSS applied for styling the lines:

body {
padding:50px;
}

.line_1 {
position:relative;
width:50px;
height:5px;
opacity:0.3;
background-color:#ef4646;
transform:rotate(-45deg);
transform-origin:left;
top: -10px;
left: 10px;
}

.line_2 {
position:relative;
width:50px;
height:5px;
opacity:0.3;
background-color:#86b4fc;
transform:rotate(45deg);
transform-origin:left;
top: 10px;
left: 10px;
}

.line_3 {
position:relative;
top:60px;
left: -40px;
width:50px;
height:5px;
opacity:0.3;
background-color:#f7e551;
transform:rotate(-45deg);
transform-origin:right;

The script used for animation:

<script type="text/javascript">
        $(document).ready(function () {
            $('.line_1').animate({ opacity:"1",left:"0px",top:"0px" });
            setInterval(function () { $('.line_2').animate({ opacity: "1", left: "0px", top: "0px" }); },400);
            setInterval(function () { $('.line_3').animate({ opacity: "1", left: "-10px", top: "30px" }); }, 600);

        });
    </script>

The current issue is that the animation only plays once when the page loads. Any suggestions on how to make it repeat continuously would be greatly appreciated. Thank you!

Answer №1

One way to streamline this code is by encapsulating it within a function and utilizing jQuery's animate method along with callbacks. To ensure a smooth transition, removing any inline styles is essential, achieved by eliminating the style attribute from the elements. Additionally, introducing a delay using jQuery's delay method allows for animation timing control.

The CSS has also been optimized by consolidating common attributes into shared rules, simplifying maintenance.

function RunAnimation(){
  $('.line_1').removeAttr("style").animate({ 
    opacity:"1",
    left:"0px",
    top:"0px" 
  });
  $('.line_2').removeAttr("style").delay(400).animate({ 
    opacity: "1", 
    left: "0px", 
    top: "0px"
  });
  $('.line_3').removeAttr("style").delay(600).animate({ 
    opacity: "1", 
    left: "-10px", 
    top: "30px" 
  }, RunAnimation);
}
$(document).ready(RunAnimation);
body {
  padding:50px;
}

.line_1, .line_2, .line_3 {
  position: relative;
  width: 50px;
  height: 5px;
  opacity: 0.3;
  transform-origin:left;
}

.line_1 {
  background-color: #ef4646;
  transform: rotate(-45deg);
  top: -10px;
  left: 10px;
}

.line_2 {
  background-color: #86b4fc;
  transform: rotate(45deg);
  top: 10px;
  left: 10px;
}

.line_3 {
  top: 60px;
  left: -40px;
  background-color: #f7e551;
  transform: rotate(-45deg);
  transform-origin: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<body>
    <div class="line_1"></div>
    <div class="line_2"></div>
    <div class="line_3"></div>
</body>

Answer №2

--edited--

Utilizing CSS can be a more efficient approach than using jQuery in my opinion. I have implemented keyframes to enable animation, and setting

animation-iteration-count: infinite;
ensures the animation runs continuously. If you wish to adjust the speed, simply edit the animation-duration to your desired timeframe.

body {
  padding:50px;
}

.line_1, .line_2, .line_3 {
  position: relative;
  width: 50px;
  height: 5px;
  opacity: 0.3;
  transform-origin:left;
}

.line_1 {
  background-color: #ef4646;
  transform: rotate(-45deg);
  top: -10px;
  left: 10px;
animation-name: line-1;
-webkit-animation-name: line-1;
animation-duration: 1.5s;
-webkit-animation-duration: 1.5s;
animation-iteration-count: infinite;
-webkit-animation-iteration-count: infinite;
}

@keyframes line-1 {
  0%   {opacity:0.3; }
  30%  {opacity:0.3; left: 10px; top: -10px;}
  50%  {opacity:1; left: 0; top: 0;}
  100% {opacity:1; left: 0; top: 0;}
}

@-webkit-keyframes line-1 {
  0%   {opacity:0.3; }
  30%  {opacity:0.3; left: 10px; top: -10px;}
  50%  {opacity:1; left: 0; top: 0;}
  100% {opacity:1; left: 0; top: 0;}
} 

.line_2 {
  background-color: #86b4fc;
  transform: rotate(45deg);
  top: 10px;
  left: 10px;
animation-name: line-2;
-webkit-animation-name: line-2;
animation-duration: 1.5s;
-webkit-animation-duration: 1.5s;
animation-iteration-count: infinite;
-webkit-animation-iteration-count: infinite;
}

@keyframes line-2 {
  0%   {opacity:0.3; }
  40%  {opacity:0.3; left: 10px; top: 10px;}
  60%  {opacity:1; left: 0; top: 0;}
  100% {opacity:1; left: 0; top: 0;}
}

@-webkit-keyframes line-2 {
  0%   {opacity:0.3; }
  40%  {opacity:0.3; left: 10px; top: 10px;}
  60%  {opacity:1; left: 0; top: 0;}
  100% {opacity:1; left: 0; top: 0;}
}

.line_3 {
  top: 60px;
  left: -40px;
  background-color: #f7e551;
  transform: rotate(-45deg);
  transform-origin: right;
animation-name: line-3;
-webkit-animation-name: line-3;
animation-duration: 1.5s;
-webkit-animation-duration: 1.5s;
animation-iteration-count: infinite;
-webkit-animation-iteration-count: infinite;
}

@keyframes line-3 {
  0%   {opacity:0.3; }
  50%  {opacity:0.3; left: -40px; top: 60px;}
  70%  {opacity:1; left: -10px; top: 30px;}
  100% {opacity:1; left: -10px; top: 30px;}
}

@-webkit-keyframes line-3 {
  0%   {opacity:0.3; }
  50%  {opacity:0.3; left: -40px; top: 60px;}
  70%  {opacity:1; left: -10px; top: 30px;}
  100% {opacity:1; left: -10px; top: 30px;}
}
<div class="line_1"></div>
<div class="line_2"></div>
<div class="line_3"></div>
    

Answer №3

If you're looking for a solution, my recommendation is to encapsulate all the code within a function and then invoke it at regular intervals.

Here is an example of how you can achieve this:

$(document).ready(function() {
  function startAnimation() {
    $('.line_1').animate({
      opacity: "1",
      left: "0px",
      top: "0px"
    });
    setTimeout(function() {
      $('.line_2').animate({
        opacity: "1",
        left: "0px",
        top: "0px"
      });
    }, 400);
    setTimeout(function() {
      $('.line_3').animate({
        opacity: "1",
        left: "-10px",
        top: "30px"
      });
    }, 600);
    setTimeout(function() {
      $('.line_1, .line_2, .line_3').removeAttr("style");
    }, 1600);

  }
  
  startAnimation();
  setInterval(startAnimation, 1650);
});
body {
  padding: 50px;
}

.line_1 {
  position: relative;
  width: 50px;
  height: 5px;
  opacity: 0.3;
  background-color: #ef4646;
  transform: rotate(-45deg);
  transform-origin: left;
  top: -10px;
  left: 10px;
}

.line_2 {
  position: relative;
  width: 50px;
  height: 5px;
  opacity: 0.3;
  background-color: #86b4fc;
  transform: rotate(45deg);
  transform-origin: left;
  top: 10px;
  left: 10px;
}

.line_3 {
  position: relative;
  top: 60px;
  left: -40px;
  width: 50px;
  height: 5px;
  opacity: 0.3;
  background-color: #f7e551;
  transform: rotate(-45deg);
  transform-origin: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
  <div class="line_1"></div>
  <div class="line_2"></div>
  <div class="line_3"></div>
</body>

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

What is the process for creating a rollover effect with png images and jquery?

I managed to reverse engineer a script that I came across online and it seems to be functioning well. The only issue I'm facing is that the hover image appears with full visibility before any hover action is taken place. Here's the script snippet ...

extracting information from a database with the help of ajax and jquery

I need to fetch data from the database based on a date range using ajax and jquery. I am facing an issue passing the id to calender.php for dates d1 and d2. <form action="" method="post" class="getcalender" id="form1"> From : <input type="text" n ...

Update an object within an array in MongoDB, ensuring that only the first object is modified

Currently, I am following a tutorial on the node.js framework. You can check it out here: https://www.w3schools.com/nodejs/nodejs_mongodb.asp I've encountered a peculiar issue that I can't seem to figure out. To give you a better idea, I've ...

Troubleshooting: jQuery UI draggable issue on Chrome

Having issues with jQuery draggable feature not working properly on Google Chrome browser Here is the code snippet causing the problem: <script src="js/jquery-1.6.2.min.js" type="text/javascript"></script> <script src="js/jquery-ui-1.8.16. ...

Prevent automatic scrolling to the top of the page after submitting a form. Remain at the current position on the

After submitting a form, I want to prevent the page from automatically scrolling back up. How can I keep the same position on the page after form submission? <script type="text/javascript"> var frm = $('#form'); frm.submit(function ...

Steps for selecting the data-link of a div using Selenium:

Take a look at this code: <div class="results"> <div class="search-results" data-link="some url1">result1</div> <div class="search-results" data-link="some url2">result2</div> ...

Ways to verify form data for accuracy of responses

Here's what I have: Index.php: <?php include "phpscripts/databaseconn.php"; $conn = new mysqli('127.0.0.1', 'root', '', 'lidl'); if($conn->connect_error) { die('Could not connect: ' . mysql ...

Edit in bulk: Execute a JavaScript function to remove a row, then delete based on a specific condition

Assignment on developing an Asp.net web page application. Check out the demo here Click to add a new record The "Delete" link will show up in the new row - it would be great if there is an X icon for consistency I need to click the "Delete" link, trigg ...

Maintaining the integrity of Jquery Tab even after refreshing the page is essential

I recently started using Jquery and encountered an issue with tab implementation. Whenever I refresh the page, it automatically directs me back to the initial tab setting. $(function() { var indicator = $('#indicator'), i ...

Enhance the <div> with interactive content through dynamic updates on click within the same element

I am currently developing an Editor-Menu for a website administration. When I open admin.php, the Editor-Menu should be loaded into the #ausgabe div using the code $('#ausgabe').load('./admin-ajax/ajax2.php'). This functionality is work ...

Determine the width of invisible images using JavaScript/jQuery

Please take a look at the jsFiddle Every time I run it, I am trying to retrieve the width of the images, but the values I get are as follows (check in the JS console) 400 0 575 533 0 ... Some values are zero, and I can't figure out why. It seem ...

Updating PHP database with a countdown timer

I am looking to create a JavaScript function that will count down from one hundred seconds. After writing the countdown code, I want to store the remaining time value in a database every second. Here is an example of what I hope to achieve: For each secon ...

"The issue with the jQuery form is that it is not functioning as intended. It seems that the ajax

When attempting to utilize jQuery form, I encountered an error stating that ajaxForm is not a function in the console. The jquery.form.js file is correctly included and the code is within a document ready function... Here is the provided script: $("#appl ...

I am facing an issue with the clearTimeout function in my JavaScript code. Can anyone help

I am encountering some issues with the clearTimeout() function. The setTimeout() function is working as expected, but I want it to stop running when I close my notification. I'm not sure what is causing the problem in my function. After closing the ...

Express bodyParser may encounter difficulties in parsing data sent from Internet Explorer

After creating a server app with Node and Express 4, I utilized jQuery for the front-end. An Ajax call was set up to send data via POST to the server: $.ajax({ cache: false, type: 'POST', url: Config.API_ENDPOINT_REGISTRATION, ...

Tips for retrieving input values when they are not available in HTML documents

In this code snippet, I am creating an input element with the file type without adding it to the DOM. My goal is to retrieve the value of this input once the user selects a file. Even though the input is not part of the HTML template, I still want to acces ...

What is the best way to extract the values associated with keys using a data variable?

I need help accessing the values of the keys under the containertransport in my nuxt.js project using the data variable url. If I try to access it like {{item.containertransport}}, it works fine. <template> <div> <p class="p_1" v-f ...

Adaptable Semantic UI form design

Welcome, internet friends! If anyone out there has a moment to spare and is familiar with Semantic UI, I could really use some assistance... Currently, I am working on a form that looks great on larger screens like this: https://i.stack.imgur.com/cafc5.j ...

The integration of the Vue.js jQuery datatable plugin is producing unexpected issues

After successfully installing dataTables with npm i dataTables --save, I encountered an issue where the search bar and pagination features were not functioning correctly within my Vue component. Please refer to the screenshot for more details: https://i.ss ...

Increase initial zoom for React Three Fiber OrbitControls to provide a wider view

I've been working on a project in React Three Fiber using this codesandbox for practice. My query regarding the demo is about setting a wider initial zoom in OrbitControls to view more small stars. Can anyone help with this? Below is the code snippe ...