Creating a sleek line animation loading graphic

I have been working on creating a loader animation that currently looks like this:

My goal is to make the black line move left to right and then right to left continuously. However, at the moment, it is only moving in one direction.

.loader {
  background: #ccc;
  width: 400px;
  height: 10px;
  border-radius: 10px;
  position: relative;
}
.loader .blue-line {
  background: #000;
  border-radius: 10px;
  position: absolute;
  left: 0;
  z-index: 1;
  width: 100px;
  height: 10px;
  animation: line-bounce 1s infinite;
}
@keyframes line-bounce {
  from {
    left: 300px;
  }
  to {
    left: 0;
  }
}
<div class="loader">
  <div class="blue-line"></div>
</div>

Answer №1

Try utilizing @keyframes alongside % values of 0/50/100 to achieve the desired effect using 100%{left: 300px;}

.loader {
  background: #ccc;
  width: 400px;
  height: 10px;
  border-radius: 10px;
  position: relative;
}
.loader .blue-line {
  background: #000;
  border-radius: 10px;
  position: absolute;
  left: 0;
  z-index: 1;
  width: 100px;
  height: 10px;
  animation: line-bounce 1s infinite;
}
@keyframes line-bounce {
  0%{
    left: 300px;
  }
  50%{
    left: 0;
  }
  100%{
    left: 300px;
  }
}
<div class="loader">
  <div class="blue-line"></div>
</div>

Answer №2

Alternatively, you have the option to utilize

 50% {
    left: 300px;
 }

.loader {
  background: #ccc;
  width: 400px;
  height: 10px;
  border-radius: 10px;
  position: relative;
}

.loader .blue-line {
  background: #000;
  border-radius: 10px;
  position: absolute;
  left: 0;
  z-index: 1;
  width: 100px;
  height: 10px;
  animation: line-bounce 1.6s infinite;
}

@keyframes line-bounce {
  
  50% {
    left: 300px;
  }
  
}
<div class="loader">
  <div class="blue-line"></div>
</div>

Answer №3

If you're looking to learn more about keyframes, please check out the following link:

https://www.w3schools.com/cssref/css3_pr_animation-keyframes.asp

.loader {
  background: #ccc;
  width: 400px;
  height: 10px;
  border-radius: 10px;
  position: relative;
}
.loader .blue-line {
  background: #000;
  border-radius: 10px;
  position: absolute;
  left: 0;
  z-index: 1;
  width: 100px;
  height: 10px;
  animation: line-bounce 1s infinite;
}
@keyframes line-bounce {
    0%   {left: 0px;}
    50%  {left: 300px;}
    100% {left: 0px;}
 
}
<div class="loader">
  <div class="blue-line"></div>
</div>

Answer №4

A simpler approach involves adding the alternate property to the animation and making adjustments for avoiding the use of pixel values as shown below:

.loader {
  background: #ccc;
  width: 400px;
  height: 10px;
  border-radius: 10px;
  margin:10px 0;
  position: relative;
}
.loader .blue-line {
  background: #000;
  border-radius: 10px;
  position: absolute;
  left: 0;
  z-index: 1;
  width: 100px;
  height: 10px;
  animation: line-bounce 1s infinite alternate;
}
@keyframes line-bounce {
  from {
    left: 100%;
    transform:translateX(-100%);
  }
  to {
    left: 0;
    transform:translateX(0);
  }
}
<div class="loader">
  <div class="blue-line"></div>
</div>
<div class="loader" style="width:500px">
  <div class="blue-line"></div>
</div>

<div class="loader" style="width:200px">
  <div class="blue-line"></div>
</div>

Answer №5

Appreciate the feedback @לבני מלכה, I've made some adjustments to enhance its fluidity.

.loader {
  background: #ccc;
  width: 400px;
  height: 10px;
  border-radius: 10px;
  position: relative;
}

.loader .blue-line {
  background: #000;
  border-radius: 10px;
  position: absolute;
  left: 0;
  z-index: 1;
  width: 100px;
  height: 10px;
  animation: line-bounce 1.6s infinite;
}

@keyframes line-bounce {
  0% {
    left: 300px;
  }
  50% {
    left: 0;
  }
  100% {
    left: 300px;
  }
}
<div class="loader">
  <div class="blue-line"></div>
</div>

Answer №6

Give it a go: test it out

HTML:

<div class="loading_track">
  <div class="loading_bar">

  </div>
</div>

CSS:

.loading_track {
      position: relative;
      height: 10px;
      background-color: #ccc;
      width: 250px;
    }

    .loading_bar {
      background-color: #333;
      position: absolute;
      height: 100%;
      width: 80px;
      animation: line-bounce 1s infinite;
    }
    @keyframes line-bounce {
      0% {
        left: 0;
      }
      50% {
        left: calc(100% - 80px);
      }
      100%{
        left: 0;
      }
    }

Answer №7

Feel free to utilize the code snippet below:

<html>
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

  <style type="text/css">
    .loader {
      background: #ccc;
      width: 400px;
      height: 10px;
      border-radius: 10px;
      position: relative;
    }
    
    .loader .blue-line {
      background: #000;
      border-radius: 10px;
      position: absolute;
      left: 0;
      z-index: 1;
      width: 100px;
      height: 10px;
      animation: line-bounce 1s infinite;
    }
    
    @keyframes line-bounce {
      0% {
        left: 0px;
      }
      50% {
        left: 300px;
      }
      100% {
        left: 0px;
      }
    }
  </style>
</head>

<body>

  <div class="container-fluid">
    <div class="row">
      <div class="loader">
        <div class="blue-line"></div>
      </div>
    </div>
  </div>

</body>

</html>

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

Initiate automatic submission

After receiving some valuable assistance from users on stackoverflow, I've successfully implemented the following code. However, I've now realized that it needs to be triggered when the page loads instead of on a trigger. Since I am new to client ...

Whenever a JavaScript Ajax Call is made, it consistently returns readyState=1 and status=0

I've been using AJAX to send a post request, but I'm running into some issues. Every time I check the xmlhttp.readyState, it's always coming back as 1 and the xmlhttp.status is always 0. Also, the xmlhttp.responseText is consistently empty. ...

The Chrome application does not retain cookies

Why is the cookie url_user not being stored? Below is the content of index.html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial- ...

The amazing property of AngularJS - Scope

I have saved this HTML code in a file. Here is the HTML : <!-- checkbox 1 --> <input type="checkbox" name="checkbox1" id="checkbox-group1" ng-model="checkbox1" value="group1"> <input type="checkbox" name="checkbox1" id="checkbox-group2" ng ...

I am looking to input and store the Kannada text used in my Angular project

I am facing an issue with my html form where I call an API to save text fields in a database. The problem arises when I type Kannada words in the text field, as when rendering the list, the Kannada fonts are displayed as ??. I am seeking a solution to thi ...

Issue with JQM popup functionality in Firefox browser

I am currently utilizing JQM 1.3.1 and have a webpage featuring various popups located at the bottom of the page: <div data-role="page" data-title="Strategic Plans"> <div data-role="content" id="capbPlans" data-bind="cafeLiveScroll: { callbac ...

Maintaining fixed position for cursor events in CSS

Here is a simple popup I created using tailwindcss to display project descriptions. Take a look at the code snippet below: // Code snippet here... I am facing an issue where both the container (outer div) and the first inner div have the same click event. ...

Customize the CSS styling of third-party components in different pages using NextJS

When working with third-party components, you can include their styles by importing their stylesheet into your component or _app.tsx file. For detailed instructions on how to do this, you can refer to Next.js documentation. Another option is to add the sty ...

Transforming current application into a responsive Angular platform

I need help making my existing Angular application, which uses Bootstrap, mobile responsive. Do I have to rewrite the entire CSS and HTML files, or is there another way to achieve this? As a newcomer, any suggestions or guidance would be greatly apprecia ...

Adjusting the width of images using jQuery Mobile or jqTouch

I am looking to incorporate a static footer image with 5 navigation buttons into my mobile website. You can view the image here. The default color for the icons should be blue, but I want the yellow icon to appear only when hovered over or in an active sta ...

Using jQuery to modify the text content within an element

Two li elements exist, each containing an icon and text. The goal is to modify the icon and text of the first element upon button click without affecting the entire anchor tag's content when using .text(). $(document).ready(function(){ $("#change ...

Is it possible to design a div with a curved lower edge?

As I am developing a website, I have a question regarding the styling of a div element. Is it achievable using just HTML5, CSS3 (and possibly JavaScript) to create a div with a curved bottom like the one shown below: https://i.sstatic.net/ZfM0a.png Or wo ...

Preventing links from functioning on dynamically generated web pages

I have implemented the following code to deactivate all links on a preview page: var disableLink = function(){ return false;}; $('a').bind('click', disableLink); While this successfully disables all static links, any anchor tags loade ...

Attempting to retrieve data from an HTML response using Node.js

My goal is to extract the Email ([email protected]) from the HTML response using cheerio and puppeteer modules. However, I am retrieving unnecessary information which I do not need at all. It is located within the Class p2 in td/tr. when passing tr a ...

Check if the browser is not Google Chrome before running the code in JavaScript

Related Search: Finding a secure way to detect Google Chrome using Javascript? Using Javascript to identify Google Chrome and apply CSS changes Is there a JavaScript statement that can be used to execute code only when the user is not using Google ...

How can I prevent the interpolation of "${variable}" in HTML when using AngularJS?

I need to display the string ${date} in a div element: <div>please substitute the ${date} as the tag for the name</div> The displayed content should be: please use the ${date} as the substitute tag to the name. However, the browser interpre ...

The box is not aligning properly

How can I adjust the positioning of my table with the ID "table" to fit higher within the containing "box"? There seems to be a margin above the header that reads "Kalkulatory macierzowe". What steps should I take to resolve this issue? HTML <div id = ...

Learn how to send dynamic data to another HTML element using Ajax's success function

I received a successful ajax response from one HTML file, but I'm struggling to dynamically set the data from this response to another HTML file. Can anyone help me with this issue? Here is the code snippet from my ajax report: success: function( ...

I'm trying to use Javascript to dynamically remove rows, but my code seems to be causing some issues

When a user enters text into a form, it is stored in a variable called 'userInput'. I have an HTML table with the ID "myTable". My goal is to use JavaScript to iterate through the table and remove all rows where the value of the 3rd column does n ...

Using CSS to Create Text Wrapping

I have a massive string of randomly generated numbers that I am trying to display in a div block. Since the string is quite long, it's currently being shown in one single line. For instance: String str="13,7,5,1,10,7,18,11,17,10,9,16,17,9,6,19,6,13, ...