Files
30-seconds-of-code/snippets/pulse-loader.md
2019-10-05 16:59:36 +05:30

2.0 KiB

title, tags
title tags
Pulse loader animation

Creates a pulse effect loader animation using animation-delay property.

<div class="ripple-loader">
    <div></div>
    <div></div>
</div>
    .ripple-loader {
        position: relative;
        width: 64px;
        height: 64px;
    }
    .ripple-loader div {
        position: absolute;
        border: 4px solid #76ff03;
        border-radius: 50%;
        animation: ripple-loader 1s ease-out infinite;
    }
    .ripple-loader div:nth-child(2) {
        animation-delay: -0.5s;
    }
    @keyframes ripple-loader {
        0% {
            top: 32px;
            left: 32px;
            width: 0;
            height: 0;
            opacity: 1;
        }
        100% {
            top: 0;
            left: 0;
            width: 64px;
            height: 64px;
            opacity: 0;
        }
    }

Explanation

Note: 1rem is usually 16px.

  1. @keyframes defines an animation at two points in the cycle, Start(0%), where the divs neither have width nor height and are positioned at the center & End(100%), where the divs have increased height & width but position is reset to 0.
  2. The divs seem to disappear as they expand. This is achieved using opacity property, which transitions from 1 to 0 in the animation.
  3. .ripple-loader is the parent container of the ripples and has a definite width and height. It uses position: relative to position child divs.
  4. .ripple > div, targets the two child divs of the parent to be styled. The divs are given a border of 4px & border-radius: 50% to turn them from squares to circles.
  5. animation is a shorthand property for the various animation properties: animation-name, animation-duration, animation-direction, animation-iteration-count are used.
  6. nth-child(n) targets the element which is the nth child of its parent.
  7. animation-delay is used on the second div, so that each element does not start the animation at the same time.

Browser support