Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write CSS keyframes to indeterminate material design progress bar

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.

like image 547
Michał Grzejszczak Avatar asked Jan 13 '16 17:01

Michał Grzejszczak


3 Answers

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.

like image 172
Rooster212 Avatar answered Oct 31 '22 15:10

Rooster212


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>
like image 25
Prasanna Venkatesh Avatar answered Oct 31 '22 14:10

Prasanna Venkatesh


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>
like image 26
Daniel Fernández Espinoza Avatar answered Oct 31 '22 15:10

Daniel Fernández Espinoza