css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

โš ๏ธ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <header>
    <h1>SCSS Animation Timeline Mixins </h1>
    <p>Create time based CSS animations (not percentages)</p>
    <div class="hearts">
        <span>
            <i>๐Ÿ’œ๐Ÿ’œ๐Ÿ’œ</i>
            <i>โค๏ธโค๏ธโค๏ธ</i>
            <i>๐Ÿงก๐Ÿงก๐Ÿงก</i>
            <i>๐Ÿ’›๐Ÿ’›๐Ÿ’›</i>
            <i>๐Ÿ’š๐Ÿ’š๐Ÿ’š</i>
            <i>๐Ÿ’™๐Ÿ’™๐Ÿ’™</i>
            <i>๐Ÿ’œ๐Ÿ’œ๐Ÿ’œ</i> 
        </span>
    </div>     
</header>
<pre>
<strong>SCSS</strong>
body {
    @include animation-timeline {
        animation-iteration-count: infinite;
        animation-fill-mode: forwards;
        
        @include tween(0s, (background-color: blueviolet, color: blueviolet));        
        @include tween(3s, (background-color: red, color: red));
        @include tween(3s, (background-color: orange, color: orange));
        @include tween(3s, (background-color: gold, color: gold));
        @include tween(3s, (background-color: green, color: green));
        @include tween(3s, (background-color: blue, color: blue));
        @include tween(3s, (background-color: blueviolet, color: blueviolet));
    }
}
</pre>
<pre>
<strong>CSS</strong>
body {
    animation-iteration-count: infinite;
    animation-fill-mode: forwards;
    animation-name: u33q65pnv;
    animation-duration: 21s;
}

@keyframes u33q65pnv {
  0% { background-color: blueviolet; color: blueviolet; }
  16.6666666667% { background-color: red; color: red; }
  33.3333333333% { background-color: orange; color: orange; }
  50% { background-color: gold; color: gold; }
  66.6666666667% { background-color: green; color: green; }
  83.3333333333% { background-color: blue; color: blue; }
  100% { background-color: blueviolet; color: blueviolet; }
}
</pre>
<pre>
<strong>The Mixins</strong>
<span>//  global context flags using wierd โ˜ ๏ธ names to minimize 
//  chance of naming conflicts with other scss variables</span>
$โ˜ ๏ธ--animation-timeline-duration: null;
$โ˜ ๏ธ--animation-timeline-tweens  : null;

<span>//  mixin to create an animation context for nested tweens
//  used to calculate the total duration of the animation
//  converting each tween delay into percentages </span>
@mixin animation-timeline($name: unique-id()) {

    <span>//  global context flag to sum up duration</span>
    $โ˜ ๏ธ--animation-timeline-duration: 0s !global;    

    <span>//  global context map to hold animation tweens</span>
    $โ˜ ๏ธ--animation-timeline-tweens  : () !global;

    <span>//  mixin content (the included tweens)</span>
    @content;

    <span>//  animation name and duration 
    //  note! if no name is provided a unique id will be used
    //  this allows you to create one-time-use animations without
    //  having to deal with animation naming conflicts :-)</span>     
    animation-name:     $name;
    animation-duration: $โ˜ ๏ธ--animation-timeline-duration;

    <span>//  create keyframes if duration is more than 0s</$span>
    @if $โ˜ ๏ธ--animation-timeline-duration > 0s {
        @keyframes #{$name} {
            <span>//  loop through the included tweens </span>
            @each $time, $props in $โ˜ ๏ธ--animation-timeline-tweens {
                <span>//  calculate percentage based on total duration</span>
                #{percentage($time/$โ˜ ๏ธ--animation-timeline-duration)}{ 
                    <span>//  print out the tween properties</span>
                    @each $prop, $value in $props {
                        #{$prop}:$value;
                    }
                }
            }
        }
    }

    <span>//  reset global context flags</span>
    $โ˜ ๏ธ--animation-timeline-duration: null !global;
    $โ˜ ๏ธ--animation-timeline-tweens  : null !global;
}


<span>//  mixin to create tweens based on a delay and a map
//  containing the the tween properties*
//
//  * using a map is not optimal โ€“ but for now you are not 
//  able to save @content to variables :( 
//</span>  
@mixin tween($delay: 0s, $props: null){
    <span>//  only do stuff if we are in a animation-timeline context</span>
    @if $โ˜ ๏ธ--animation-timeline-tweens {
        <span>//  increment the total animation duration by the the tween delay </span>
        $โ˜ ๏ธ--animation-timeline-duration: $โ˜ ๏ธ--animation-timeline-duration + $delay !global;
        <span>//  save current duration and tween props to the global tween map</span>
        $โ˜ ๏ธ--animation-timeline-tweens: map-merge($โ˜ ๏ธ--animation-timeline-tweens, ($โ˜ ๏ธ--animation-timeline-duration: $props)) !global;
    }
}
</pre>

            
          
!
            
              //  global context flags using wierd โ˜ ๏ธ names to minimize 
//  chance of naming conflicts with other scss variables
$โ˜ ๏ธ--animation-timeline-duration: null;
$โ˜ ๏ธ--animation-timeline-tweens  : null;

//  mixin to create an animation context for nested tweens
//  used to calculate the total duration of the animation
//  converting each tween delay into percentages 
@mixin animation-timeline($name: unique-id()) {

    //  global context flag to sum up duration
    $โ˜ ๏ธ--animation-timeline-duration: 0s !global;    

    //  global context map to hold animation tweens
    $โ˜ ๏ธ--animation-timeline-tweens  : () !global;

    //  mixin content (the included tweens)
    @content;

    //  animation name and duration 
    //  note! if no name is provided a unique id will be used
    //  this allows you to create one-time-use animations without
    //  having to deal with animation naming conflicts :-)     
    animation-name:     $name;
    animation-duration: $โ˜ ๏ธ--animation-timeline-duration;

    //  create keyframes
    @if $โ˜ ๏ธ--animation-timeline-duration > 0s {
        @keyframes #{$name} {
            //  loop through the included tweens 
            @each $time, $props in $โ˜ ๏ธ--animation-timeline-tweens {
                //  calculate percentage based on total duration
                #{percentage($time/$โ˜ ๏ธ--animation-timeline-duration)}{ 
                    //  print out the tween properties
                    @each $prop, $value in $props {
                        #{$prop}:$value;
                    }
                }
            }
        }
    }

    //  reset global context flags
    $โ˜ ๏ธ--animation-timeline-duration: null !global;
    $โ˜ ๏ธ--animation-timeline-tweens  : null !global;
}


//  mixin to create tweens based on a delay and a map
//  containing the the tween properties*
//
//  * using a map is not optimal โ€“ but for now you are not 
//  able to save @content to variables :( 
//  
@mixin tween($delay: 0s, $props: null){
    //  only do stuff if we are in a animation-timeline context
    @if $โ˜ ๏ธ--animation-timeline-tweens {
        //  increment the total animation duration by the the tween delay 
        $โ˜ ๏ธ--animation-timeline-duration: $โ˜ ๏ธ--animation-timeline-duration + $delay !global;
        //  save current duration and tween props to the global tween map
        $โ˜ ๏ธ--animation-timeline-tweens: map-merge($โ˜ ๏ธ--animation-timeline-tweens, ($โ˜ ๏ธ--animation-timeline-duration: $props)) !global;
    }
}

//  TODO ideas
//  - tween(3s)     no map => reuses the previous map to create pause
//  - tween(start)  start  => adds a 0.01ms delay and reuses first map to jump to start   


body {
    @include animation-timeline {
        animation-iteration-count: infinite;
        animation-fill-mode: forwards;
        @include tween(0s, (background-color: blueviolet, color: blueviolet));        
        @include tween(3s, (background-color: red, color: red));
        @include tween(3s, (background-color: orange, color: orange));
        @include tween(3s, (background-color: gold, color: gold));
        @include tween(3s, (background-color: green, color: green));
        @include tween(3s, (background-color: blue, color: blue));
        @include tween(3s, (background-color: blueviolet, color: blueviolet));
    }
}




.hearts {
    overflow: hidden; 
    height: 6em;
    i { font-size: 4em; display: block; line-height: 1.5; margin: 0; font-style: normal; text-align: center; }
    span {
        position: relative; 
        display: block; 
        @include animation-timeline(hearts) {
            animation-fill-mode: forwards;
            animation-iteration-count: infinite;
            @include tween(600ms, (transform: translatey(-100%/7 * 1)));   //  slide to โค๏ธ
            @include tween(2.4s,  (transform: translatey(-100%/7 * 1)));   //  pause 
            @include tween(600ms, (transform: translatey(-100%/7 * 2)));   //  slide to ๐Ÿงก 
            @include tween(2.4s,  (transform: translatey(-100%/7 * 2)));   //  pause 
            @include tween(600ms, (transform: translatey(-100%/7 * 3)));   //  slide to ๐Ÿ’›
            @include tween(2.4s,  (transform: translatey(-100%/7 * 3)));   //  pause 
            @include tween(600ms, (transform: translatey(-100%/7 * 4)));   //  slide to ๐Ÿ’š
            @include tween(2.4s,  (transform: translatey(-100%/7 * 4)));   //  pause 
            @include tween(600ms, (transform: translatey(-100%/7 * 5)));   //  slide to ๐Ÿ’™
            @include tween(2.4s,  (transform: translatey(-100%/7 * 5)));   //  pause 
            @include tween(600ms, (transform: translatey(-100%/7 * 6)));   //  slide to ๐Ÿ’œ
            @include tween(2.4s,  (transform: translatey(-100%/7 * 6)));   //  pause 
            @include tween(0.1ms, (transform: translatey(-100%/7 * 0)));   //  jump to  ๐Ÿ’œ
        }
    }
    
}



html { box-sizing: border-box; } *,*::before,*::after { box-sizing: inherit; }
header { background: rgba(white, 0.9); padding: 1em; color: inherit; overflow: hidden; transform: translate3d(0,0,0); }
h1   { font: 400 24px sans-serif; margin-bottom: 0; }
p    { font: 400 16px sans-serif; }
pre  { 
    font-size: 11px; background: rgba(white, 0.7); padding: 1em; color: black; overflow: scroll;
    -webkit-overflow-scrolling: touch; 
    span { opacity: 0.7; }
}


            
          
!
999px
๐Ÿ•‘ One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console