Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">
    
<head>
  <meta charset="utf-8">
</head>
    
<body>

<div class="wrapper">  
   
 <p>circle.timeline = </p>
    
    <textarea id="input_field" oninput="setTimelineInput(this.value)" onchange="setTimelineInput(this.value)">
[{"key":0   , "x": 0  , "y":0   },
 {"key":0.20, "x": 100, "y":100 },
 {"key":0.40, "x": 200, "y":0   },
 {"key":0.60, "x": 300, "y":200 },
 {"key":0.80, "x": 300, "y":100 },
 {"key":1.00, "x": 200, "y":100}]
    </textarea> 

    <div class="box">
        <div id="circle"><span></span></div>
    </div>
    
    <p id="output_field">0</p>
    
    <input id="range" type="range" min="0" max="1000" value="0" oninput="update(this.value)" onchange="update(this.value)">

</div> 
    
</body>
</html>
              
            
!

CSS

              
                body{
    background-color:#fff;
    margin:0;
    padding:0;
}

.wrapper{
    font-family:"Tahoma";
    max-width:300px;
    margin:0 auto;
    padding:30px 5px 30px 5px;
}
p{
    margin:5px 0 5px 0;
    font-size:14px;
}
textarea{
    width:100%;
    padding:5px;
    box-sizing: border-box;
    min-height:140px;
    line-height:16px;
    border:1px solid #ccc;
    border-radius:3px;
    background-color:#f4f4f4;
    margin-bottom:10px;
}

.box{
    width:100%;
    height:200px;
    background-image:url(https://www.isotopic.com.br/wordpress/wp-content/uploads/2015/11/zIYXCZv.png);
    background-repeat:no-repeat;
    margin:0 auto;
}

#circle{
    width:10px;height:10px;
    transform:translate(0px,0px);
}
#circle span{
    display:block;
    width:10px;height:10px;
    border-radius:50%;
    border:4px solid #6699EE;
    background-color:#fff;
    transform:translate(-8px,-8px);
}
#range{
    width:90%;
    display:block;
    margin: 0 auto;
}

#output_field{
    text-align:center;
}
              
            
!

JS

              
                /**
* Author: isotopic.com.br
* Scroll based animation following a 'keyframed timeline' approach.
* All animation keyframes are stored in a json file.
* Movement is triggered by any continuous input (like scroll, slider, etc).
* In a real use case it'd have a lot more verifications. This is just to show the concept.
*/

// Sample object to be animated
var circle = document.getElementById("circle");
circle.timeline = {};

// Input
var input_field = document.getElementById("input_field");

// Output
var output_field = document.getElementById("output_field");

// In this example, json data comes from the input field
function setTimelineInput(json_data){ 
   circle.timeline = JSON.parse(json_data);
}
setTimelineInput(input_field.value);

// In this example, update is fired by the slider event - usually it'd be triggered by scrolling the page.
function update(range_value){

    // 'ratio' corresponds to the animation state, from 0 (start) to 1 (end).
    var ratio = range_value / 1000;
    var keyframes = [];
    var local_ratio = 0;
    output_field.innerHTML = ratio;
    
    // Loop through all keyframes available for this object
    for(i=0;i<circle.timeline.length-1;i++){

        // When ratio's value is between two keyframes, it must affect the object's properties
        if(ratio >= circle.timeline[i].key && ratio <= circle.timeline[i+1].key){
            keyframes[0] = circle.timeline[i];
            keyframes[1] = circle.timeline[i+1];
        }
        
        if(keyframes.length==2){

            // Normalizes the global ratio to a local ratio (the ratio between two keyframes)
            local_ratio = (ratio - keyframes[0].key) / (keyframes[1].key - keyframes[0].key);
            
            // A little easing
            local_ratio = easeInOutQuad(local_ratio);

            // The calculated position will be the initial position, plus the percent of the possible variation between frames
            x = (keyframes[0].x + local_ratio*(keyframes[1].x -
            keyframes[0].x)); y = (keyframes[0].y + local_ratio*(keyframes[1].y
            - keyframes[0].y));

            // What a mess
            circle.style['-webkit-transform'] = "translate( "+x+"px, "+y+"px)";
            circle.style['-ms-transform']     = "translate( "+x+"px, "+y+"px)";
            circle.style['transform']         = "translate( "+x+"px, "+y+"px)";

        }
        
    }
    
}


//Robert Penner's simplified to just one input
function easeInOutQuad(t) {
    t *= 2;
    if (t < 1) return t*t /2;
    t--;
    return - ( t*(t-2) - 1 ) /2;
}
              
            
!
999px

Console