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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 id="container"></div>
<div id="nav">
  <a id="link-a" href="#">AAAAA</a>
  <a id="link-b" href="#">BBBBB</a>
  <a id="link-c" href="#">CCCCC</a>
</div>
<div id="a" class="box">
  <h1>View A</h1>
  <p>In this example each view is managed in an array. The order in the array matches the order of the buttons at the top. The transition between views is determined by the position of the new view in the array relative to current view. If the view is to the left, for example the new view is A, and current view is B. The transition slides left to right. </p>
</div>
<div id="b" class="box">
  <h1>View B</h1>
</div>
<div id="c" class="box">
  <h1>View C</h1>
</div>
              
            
!

CSS

              
                body, html {
  margin: 0;
  height: 100%;
}

#container {
  width: 100%;
  height: 100%;
  background: lightgray;
  position: relative;
}

#nav {
  position: fixed;
  left: 20px;
  top: 20px;
}

#link-a, #link-b, #link-c {
  display: inline-block;
  padding: 1em;
  color: white;
  background: black;
}

.box {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding: 60px 2em 0 2em;
  position: absolute;
  border: 3px solid red;
}

#a {
  background: cyan;
}

#b {
  background: yellow;
}

#c {
  background: magenta;
}



              
            
!

JS

              
                // Define some view elements to work with
var a = $("#a");
var b = $("#b");
var c = $("#c");

// Add an array to hold the views
var viewsArray = [a, b, c];

// Remove these views from the DOM
a.remove();
b.remove();
c.remove();

// This var will hold a reference to the current view
var currentView;

// Define some transitions 
  var offLeft = {
    left: "-100%"
  };
  var onHorizontal = {
    left: 0
  };

  var offRight = {
    left: "100%"
  };
  
  var offDown = {
    top: "100%"
  };
  var offUp = {
    top: "-100%"
  };
  
  var onVertical = {
    top: 0
  };
  
  // These combinations create transitions in four directions
  // Slide right: offRight, offLeft, onHorizontal
  // Slide left: offLeft, offRight, onHorizontal
  // Slide down: offDown, offUp, onVertical
  // Slide up: offUp, offDown, onVertical

// Some helper functions to create transitions in four directions
function pushLeft(newView) {
  showView(newView, offRight, offLeft, onHorizontal)
}

function pushRight(newView) {
  showView(newView, offLeft, offRight, onHorizontal)
}

function pushDown(newView) {
  showView(newView, offDown, offUp, onVertical);
}

function pushUp(newView) {
  showView(newView, offUp, offDown, onVertical);
}

function showView(newView, exit, start, enter) {
  // Hide Current View
  if (currentView) {
    currentView.animate(exit, 400, function() {
      this.remove();
    });
  }
  // Show New View
  currentView = newView;
  $("#container").append(currentView);
  currentView.css(start).animate(enter, 400, function() {

  });
}



function showViewInArray(newView) {
  // Don't load the current view again
  if (currentView == newView) {
    return;
  }
  // Find the index of the current view
  var indexOfCurrentView = viewsArray.indexOf(currentView);
  // Find the index of the new view
  var indexOfNewView = viewsArray.indexOf(newView);
  if (indexOfCurrentView == -1) {
    // There is no current, so do a pushLeft
    pushRight(newView);
  } else {
    if (indexOfNewView < indexOfCurrentView) {
      // The new view is to the left in the array
      pushLeft(newView);
    } else {
      // The new view is to the right in the array
      pushRight(newView);
    }
  }
}



// These functions make use of the new transition helper functions
$("#link-a").click(function(event) {
  showViewInArray(a);
});

$("#link-b").click(function(event) {
  showViewInArray(b);
});

$("#link-c").click(function(event) {
  showViewInArray(c);
});
              
            
!
999px

Console