Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 id="box-1" class="box">1</div>
<div id="box-2" class="box">2</div>
<div id="box-3" class="box">3</div>
<div id="box-4" class="box">4</div>
<div id="box-4" class="box">5</div>
              
            
!

CSS

              
                .box {
  width: 75px;
  height: 75px;
  background: green;
  margin: 10px;
  color: #fff;
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  line-height: 75px;
  text-shadow: 0px 0px 1px rgba(0,0,0,1);
}
              
            
!

JS

              
                console.clear();

var offset = 25;
var $boxes = $(".box");

$boxes.each(function(i) {
  var color = "hsl(" + i / $boxes.length * 360 + ", 100%, 50%)";
  TweenLite.set(this, { x: 0, backgroundColor: color });
});

//
// BOX 1: Constructor using class declaration
// ===========================================================================
class Box1 {  
  constructor() {
    this.element = $boxes[0];
  }
  
  get x() { return this.element._gsTransform.x; }
  set x(x) {
    x = Math.round(x / offset) * offset;
    if (this.x !== x) {
      TweenLite.set(this.element, { x: x });
    }
  }
}

//
// BOX 2: Constructor without class declaration
// ===========================================================================
function Box2() {
  this.element = $boxes[1];
}

Object.defineProperty(Box2.prototype, "x", {
    get: function () { return this.element._gsTransform.x; },
    set: function (x) {
      x = Math.round(x / offset) * offset;
      if (this.x !== x) {
        TweenLite.set(this.element, { x: x });
      }
    }
});

//
// BOX 3: Object literal
// ===========================================================================
var box3 = {
  element: $boxes[2],
  get x() { return this.element._gsTransform.x; },
  set x(x) {
    x = Math.round(x / offset) * offset;
    if (this.x !== x) {
      TweenLite.set(this.element, { x: x });
    }
  }
};


//
// BOX 4: Using a single function based getter/setter
// ===========================================================================
var box4 = {
  element: $boxes[3],
  x: function(x) {
    
    var lastX = this.element._gsTransform.x;
    
    if (!arguments.length) {
      return lastX;
    }
    
    x = Math.round(x / offset) * offset;
    if (lastX !== x) {
      TweenLite.set(this.element, { x: x });
    }
  }
};

//
// BOX 5: Using functions prefixed with "get" and "set"
// ===========================================================================
var box5 = {
  element: $boxes[4],
  getX: function() {
    return this.element._gsTransform.x;
  },
  setX: function(x) {
    x = Math.round(x / offset) * offset;
    if (this.getX() !== x) {
      TweenLite.set(this.element, { x: x });
    }
  }
};

// Box 1 and 2 use a constructor
var box1 = new Box1();
var box2 = new Box2();

var tl = new TimelineMax({ repeat: -1, repeatDelay: 0.5, yoyo: true });

tl.to([box1, box2, box3, box4], 2, { x: 500 }, 0)
  .to(box5, 2, { setX: 500 }, 0);








              
            
!
999px

Console