19

I want to make an indeterminate progress bar as in material design (the second one) in CSS3 in a web page. Can anyone share the necessary CSS magic? I want it to behave exactly the same as on the video, so one end is accelerating while the other is slowing down until they switch.

I could not find any existing example that would work this way.

Michał Grzejszczak
  • 2,599
  • 1
  • 23
  • 28

3 Answers3

44

I found a good example by Stefano on CodePen:

// Android Material Loading animation with pure css. 
// Author: Stefano Ferrara http://androidpc.it/
// Forked from https://codepen.io/chofoo/pen/Abril
// Author: Simon Clavey https://simonclavey.co.uk/
body{
  background:#ffffff;
  margin:50px 300px;
}

.slider{
  position:absolute;
  width:1000px;
  height:5px;
  overflow-x: hidden;
}

.line{
  position:absolute;
  opacity: 0.4;
  background:#4a8df8;
  width:150%;
  height:5px;
}

.subline{
  position:absolute;
  background:#4a8df8;
  height:5px; 
}
.inc{
  animation: increase 2s infinite;
}
.dec{
  animation: decrease 2s 0.5s infinite;
}

@keyframes increase {
   from { left: -5%; width: 5%; }
   to { left: 130%; width: 100%;}
}
@keyframes decrease {
   from { left: -80%; width: 80%; }
   to { left: 110%; width: 10%;}
}
<div class="slider">
  <div class="line"></div>
  <div class="subline inc"></div>
  <div class="subline dec"></div>
</div>

License:

Copyright (c) 2016 by Stefano (https://codepen.io/shalimano/pen/wBmNGJ)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Rooster212
  • 589
  • 10
  • 17
  • Any ideas on how achieve the same using only `transforms`? Like translate and scale? That way the animation won't stop if the main thread gets stalled, since this animation freezes when the main thread is busy with other stuff. – Diosney May 11 '18 at 23:09
  • Well, tried it with `translate` and `scale` and it still freezes whenever I perform any heavy task. Any ideas? I've a simple refresh icon with `rotate` that don't stops and that is the same thing I want to do with the progress bar. – Diosney May 11 '18 at 23:11
  • My first thought would be to move heavy tasks off the main thread. You could offload to a server (if that's possible with your use case) or use a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). I've not used transforms in that way before and not sure it's possible. Everything I've seen still uses CSS animation with transform keyframes. – Rooster212 Jun 06 '18 at 20:33
7

Super simple CSS loading bar:

main {
  margin: 100px auto;
  /* Parent should have position set to relative for not disturbing the height when the loading bar is hidden */
  position: relative; 
  /* Parent width will be the width of the loading bar */
  width: 400px; 
}

.loading-bar-container {
  height: 2px;
  width: 100%;
  background-color: #cfe0f0;
  position: absolute;
  overflow: hidden;
}

.loading-bar {
  height: 100%;
  width: 50%;
  background-color: #0000ff;
  position: absolute;
  left: -50%;
  animation: loading 2s ease-in 0.5s infinite;
}

@keyframes loading {
 0% {
  transform:translateX(0)
 }
 to {
  transform:translateX(400%)
 }
}
<main>
  <div class="loading-bar-container">
    <div class="loading-bar" />
  </div>
</main>
4

For those interested this is how it looks the same CSS code of the accepted answer above but using CSS transforms instead of left/width.

body{
  background:#ffffff;
  margin:50px 300px;
}

.slider{
  position:absolute;
  width:1000px;
  height:5px;
  overflow-x: hidden;
}

.line{
  position:absolute;
  opacity: 0.4;
  background:#4a8df8;
  width:150%;
  height:5px;
}

.subline{
  position:absolute;
  background:#4a8df8;
  width:100%;
  height:5px; 
}
.inc{
  animation: increase 2s infinite;
}
.dec{
  animation: decrease 2s 0.5s infinite;
}

@keyframes increase {
   from {transform:translateX(-5%) scaleX(.05);}
   to {transform:translateX(130%) scaleX(1);}
}
@keyframes decrease {
   from {transform:translateX(-80%) scaleX(.8);}
   to {transform:translateX(110%) scaleX(.1);}
}
<div class="slider">
  <div class="line"></div>
  <div class="subline inc"></div>
  <div class="subline dec"></div>
</div>
sibbl
  • 3,203
  • 26
  • 38