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

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

              
                <h1>Grid Using GreenSock <a href="https://greensock.com/timelinelite" target="_blank">TimelineLite</a></h1>
<button id="shuffle">Shuffle</button>
<button id="reorder">Reorder</button>
<button id="prepend">Prepend</button>
<button id="append">Append</button>
<button class="spacer"></button>
<button id="reset">Reset</button>  
<div id="container"></div>
              
            
!

CSS

              
                $box-size = 100px
$box-color = #9acd32
$button-color = #555
$background = #3a3a4d
$color = #efefef

body
  margin 20px
  background $background
  font-family RobotoDraft, 'Helvetica Neue', Helvetica, Arial
  
h1
  color $color
  font-weight: normal
  font-weight 300
  margin-bottom 30px
  a
    color $color
    border-bottom 2px solid $color
    text-decoration none
    padding-bottom 3px
    &:hover
      color $tile-color
      border-color $box-color
  

#container
  overflow hidden
  clear both
  width 720px
  position relative
  .box
    display inline-block  
    float left
    width $box-size
    height $box-size
    margin 5px
    background $box-color
    text-align center
    font-size 20px
    line-height 100px
    position relative
    border 1px solid rgba(0,0,0,0.5)

button
  float left
  width $box-size
  margin 0px 5px 20px 5px
  padding 10px 0px
  font-size 16px
  background $color
  color $333
  border none
  &:hover
    background darken($color, 10)
  &.spacer
    background $background



              
            
!

JS

              
                console.clear();

var boxes = [];
var numBoxes = 18;
var numBoxesToAdd = 6;

// Animation settings
var duration = 0.5;   // length of time in secods for box to fade in
var delay = 0.03;     // delay in secods before each new box appears

var tiles = [];
var ease  = Back.easeOut.config(0.5);
var container = $('#container')[0];

$(document).ready(function () {
  
  init();

  $('#shuffle').click(function () {
    reorderTiles(true);
  });
  
  $('#reorder').click(function () {
    reorderTiles();
  });
  
  $('#prepend').click(function () {
   
    prependBoxes(addBoxes(numBoxesToAdd));
  });   
  
  $('#append').click(function () {

    appendBoxes(addBoxes(numBoxesToAdd));
  });
  
  $('#reset').click(function () {

    init();
  });  
});

function init () {
  
  tiles = [];
  boxes = [];
  $('#container').empty();
  createBoxes(numBoxes);
  appendBoxes(boxes);  
}

//
// REORDER TILES
// ====================================================================
function reorderTiles(shuffled) {
    
  var total = tiles.length;
  
  var i = total;
  
  while (i--) {
    
    var tile = tiles[i];
        
    tile.x = tile.element.offsetLeft;
    tile.y = tile.element.offsetTop;
        
    container.removeChild(tile.element);
  }
  
  shuffled ? shuffle(tiles) : tiles.sort(sortOrder);
    
  for (var i = 0; i < total; i++) {
    
    var tile = tiles[i];
        
    var lastX = tile.x;
    var lastY = tile.y;
    
    container.appendChild(tile.element);
    
    tile.x = tile.element.offsetLeft;
    tile.y = tile.element.offsetTop;
    
    var dx = tile.transform.x + lastX - tile.x;
    var dy = tile.transform.y + lastY - tile.y;   
    
    TweenLite.fromTo(tile.element, 1, { x: dx, y: dy }, { 
      x: 0, 
      y: 0, 
      ease: ease, 
      immediateRender: true
    });    
  }  
}

//
// CREATE TILE
// ====================================================================
function createTile(num, prepend) {
    
  var add = prepend ? ["prependTo", "unshift"] : ["appendTo", "push"];  
  var tile = $("<div class='box'/>").text(num)[add[0]](container)[0];
  
  TweenLite.set(tile, { x: "+=0" });
     
  tiles[add[1]]({      
    transform: tile._gsTransform,
    element: tile,
    num: num,
    x: tile.offsetLeft,
    y: tile.offsetTop
  });
  
  return tile;
}

//
// SORT
// ====================================================================
function sortOrder(a, b) {
  return a.num - b.num;
}

function createBoxes (num) {
  
  for (var i = 0; i < num; i += 1) {
    boxes.push(i + 1);
  }  
}

function addBoxes (num) {
  
  var numBoxes = boxes.length;
  var newBoxes = [];
  
  for (var i = 0; i < num; i += 1) {
    newBoxes.push(numBoxes + i + 1);
    boxes.push(numBoxes + i + 1);
  }

  return newBoxes;
}

function appendBoxes (collection, isShuffle) {  
    
  var tl = new TimelineLite();
  
  collection.forEach(function(num, i) {
        
    var tile = createTile(num);
    
    tl.from(tile, duration, {
      opacity: 0, 
      scale: 0,
      ease: Sine.easeIn      
    }, '-=' + (duration - delay));        
  });
}

function prependBoxes (collection) {  
  
  var tl = new TimelineLite();
  
  collection.reverse().forEach(function (num, index) {
      
    var tile = createTile(num, true);
    
    tl.from(tile, duration, {
      opacity: 0, 
      scale: 0,
      ease: Sine.easeIn,
      delay: -(delay * (index + 1))
    }, '-=' + (duration - delay));
  });
}

function shuffle (array) {
  
  var currentIndex = array.length;
  var temporaryValue;
  var randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}
              
            
!
999px

Console