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

              
                

<h1>iOS7 Switch</h1>
<p>Click on the toggle switch below to see the funky animation, a la iOS7. Keep your mouse button pressed down for a second for maximum fun.</p>

<div class='listItem'>
  <span>Click me!</span>
  
  <div class='switch'>
    <div class='nub-bg'></div>
    <div class='nub'></div>
  </div>
</div>
              
            
!

CSS

              
                // ---------------------------- [ General Styles ] //

* {
  // Keeps the user from being able to drag the nub off the page.
  -webkit-user-select: none;
  -moz-user-select: none;
	user-select: none;
}

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

body {
  font-family: "helvetica neue", "helvetica", "Open Sans", "Calibri", sans-serif;
  text-align: center;
  background-color: #efeff4;
  padding-top: 0px;
}
div.listItem {
  height: 43px;
  border-radius: 5px;
  background-color: #fff;
  width: 537px;
  margin: 0 auto;
  padding: 0 15px;
}
div.listItem span {
  float: left;
  line-height: 43px;
  font-size: 16px;
}

h1 {
  font-size: 65px;
  font-weight: 100;
  color: #157dfb;
  margin-bottom: 20px;
}
p {
  width: 537px;
  margin: 0 auto 40px;
  line-height: 21px;
}

// ---------------------------- [ Switch Styles ] //

// The main switch container.

div.switch {
  position: relative;
  top: 6px;
  float: right;
  
  width: 51px;
  height: 31px;
  border-radius: 31px;
  background-color: #e6e6e6;
  
  cursor: pointer;  
  
  -webkit-transition: 200ms background-color ease-in;
  -moz-transition:    200ms background-color ease-in;
  transition:         200ms background-color ease-in;
}
div.switch.active {
  background-color: #4cd964;
}

// The "back" part of the nub.

div.switch div.nub-bg {  
  position: absolute;
  top: 2px;
  left: 2px;
  
  width: 47px;
  height: 27px;

  border-radius: 27px;
  background-color: #fff;
  
  -webkit-transform: scale(1,1);
  -moz-transform:    scale(1,1);
  transform:         scale(1,1);
  transform-origin: center center;
  
  -webkit-transition: 200ms -webkit-transform ease-in;
  -moz-transition:    200ms -moz-transform ease-in;
  transition:         200ms transform ease-in;
}
div.switch div.nub-bg.active {
  width: 47px;
  height: 27px;
  
  -webkit-transform: scale(0,0);
  -moz-transform:    scale(0,0);
  transform:         scale(0,0);
}

// The nub.

div.switch div.nub {
  position: absolute;
  top: 2px;
  
  left: 2px;
  right: 22px;
  
  height: 27px;

  border-radius: 27px;
  background-color: #fff;
  box-shadow: 0 2px 3px rgba(0,0,0,0.5);
  
  -webkit-transition: 200ms width ease-in,
                      200ms left ease-in,
                      200ms right ease-in;
  
  -moz-transition:    200ms width ease-in,
                      200ms left ease-in,
                      200ms right ease-in;
  
  transition:         200ms width ease-in,
                      200ms left ease-in,
                      200ms right ease-in;
}
div.switch.active div.nub {
  left: 22px;
  right: 2px;
}

div.switch div.nub.wide {
  left: 2px;
  right: 14px;
}
div.switch.active div.nub.wide {
  left: 14px;
  right: 2px;
}
              
            
!

JS

              
                   
function iosSwitch() {
  this.switchContainer = document.querySelector("div.switch");
  this.nub             = document.querySelector("div.switch div.nub");
  this.nubBg           = document.querySelector("div.switch div.nub-bg");
  
  this.isActive = false;
  
  var self = this;

  function mouseDown() {
    self.nub.addClassName("wide");
    
    document.body.addEventListener("mouseup", mouseUp);
  };

  function mouseUp() {
    self.nub.removeClassName("wide");    
    self.toggleState();
    
    document.body.removeEventListener("mouseup", mouseUp);
  };

  this.switchContainer.addEventListener("mousedown", mouseDown);
};

iosSwitch.prototype.toggleState = function() {
  this.isActive = !this.isActive;
  
  this.renderState();
};

iosSwitch.prototype.renderState = function() {
  var self = this;

  switch(self.isActive) {
    case true:
      self.switchContainer.addClassName("active");
      self.nubBg.addClassName("active");
    break;
    case false:
      self.switchContainer.removeClassName("active");
      self.nubBg.removeClassName("active");
    break;
  }
};

// Support Methods

Element.prototype.addClassName = function(className) {
  if(this.className == 'undefined') {
    this.className = className;
  } else {
    var classes = this.className.split(" ");
    if(classes.indexOf(className) == -1) {
      classes.push(className);
    }
    
    this.className = classes.join(" ");
  }
  
  return this;
};

Element.prototype.removeClassName = function(className) {
  if(this.className == 'undefined')
    return;
  else {
    var classes = this.className.split(" ");
    var classNameIndex = classes.indexOf(className);
    
    if(classNameIndex == -1)
      return this;
    else {
      classes.splice(classNameIndex, 1);
      
      this.className = classes.join(" ");
    }
  }
}

// Instantite the switch.
var iosSwitch = new iosSwitch();
              
            
!
999px

Console