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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div></div>
<button>Animate</button>
              
            
!

CSS

              
                // -----------------------------------------

// Reset

*
  margin: 0
  padding: 0

html
  box-sizing: border-box

*, *:before, *:after
  box-sizing: inherit

body, h1,h2,h3,h4,p
  border: 0
 
body
  height: 500vh
  
button
  position: fixed
  top: 20px
  right: 20px
  padding: 10px

div
  position: fixed
  top: 0
  left: 0
  background: black
  width: 100px
  height: 100px
  
              
            
!

JS

              
                console.clear();

// bind es6 methods to context
let es6BindAll = function(context, methodNames) {
    if (!Array.isArray(methodNames)) {
      methodNames = [methodNames];
    }
    methodNames.map(function (method) {
      try {
        context[method] = context[method].bind(context);
      } catch (e) {
        var cName = context.name ? ", " + context.name : "";
        var mName = typeof method === "function" ? method.name : method;
        console.log("Error: " + e);
        throw new Error("Cannot bind method " + mName + " to the supplied context" + cName);
      }
      return null;
    });
 }

/**
 * Store Viewport, Scroll Position & Cells
*/

let html = document.documentElement,
    body = document.body;

let app = {
  viewport: {
    w: Math.max(window.innerWidth || html.clientWidth || body.clientWidth || screen.availWidth),
    h: Math.max(window.innerHeight || html.clientHeight || body.clientHeight || screen.availHeight)
  },
  scroll: {
    top: 0
  }
}

// listen for scroll event
window.addEventListener('scroll', ()=> {
  app.scroll.top = document.getElementsByTagName('html')[0].scrollTop;
  PubSub.publish('viewport.scroll');
});

/**
 * Layout
*/

class Layout {
   
  constructor() {
    this.elem = document.querySelector('div');
    this.button = document.querySelector('button');
    this.tl;
    this.newX = app.viewport.w - 100;
    this.newY = 0;
    this.time = 6;
    this.obj = {
      x: 0,
      y: 0
    };
    
    es6BindAll(this, [
      "init",
      "addEventListeners",
      "layout",
      "onScroll"
    ]);
    this.init();
  }

  init(){
    this.addEventListeners();
  }

  addEventListeners(){
    PubSub.subscribe('viewport.scroll', this.onScroll);
    this.button.addEventListener('click', this.layout);
  }
  
  onScroll(){
    this.newY = app.scroll.top;
    if(this.tl){
      this.tl.updateTo({y: this.newY}, false);
    }
  }

  layout(){
    this.tl = new TweenMax(this.obj, this.time, {
      x: this.newX,
      y: this.newY,
      onUpdate: ()=> {
        TweenLite.set(this.elem, {x: this.obj.x, y: this.obj.y});
      },
      ease: Power1.easeOut
    });
  }
}

new Layout();
              
            
!
999px

Console