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

              
                <section id="menu" class="tab-menu">
  <div>
    <b>Javascript Tabbed Menu</b>
    <a href="#menu" data-click-switch="menu about selected" class="menu about tab-index selected">about</a>
    <a href="#menu" data-click-switch="menu city-menu selected" class="menu city-menu tab-index">cities</a>
    <a href="#menu" data-click-switch="menu fruit-menu selected" class="menu fruit-menu tab-index">fruit</a>
    <a href="#menu" data-click-switch="menu animal-menu selected" class="menu animal-menu tab-index">animals</a>
  </div>
</section>
<section id="cities" class="menu city-menu tab-content">
  <div class="tab-menu">
    <b>Cities:</b>
    <a href="#cities" data-click-switch="cities atl selected" class="cities atl tab-index selected">atlanta*</a>
    <a href="#cities" data-click-switch="cities nyc selected" class="cities nyc tab-index">new york</a>
    <a href="#cities" data-click-switch="cities la selected" class="cities la tab-index">los angeles</a>
  </div>  
  <div data-hover-switch="atl-hover selected" class="cities atl tab-content selected">
    <h2>Atlanta</h2>
    <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRamIa4IbpbIEho_cgrp2kU0a7IBY3sguNxUULzBd5IDY9tbIzwcg">
    <div class="atl-hover tab-content"><h3>Atlanta is the best!</h3></div>
  </div>
  <div class="cities nyc tab-content">
    <h2>New York</h2>
    <img src="http://www.nyhabitat.com/images-layout/temp_newyork.jpg">
  </div>
  <div class="cities la tab-content">
    <h2>Las Angeles</h2>
    <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQLSk4-uA8IpWWhT3te-MfGGFYAJ2zcKER9_63zMs6FIO2_MPlN">
  </div>
  
</section>
<section id="fruit" class="menu fruit-menu tab-content">
  <div class="tab-menu">
    <b>Fruit:</b>
    <a href="#fruit" data-click-switch="fruit apple selected" class="fruit apple tab-index selected">apple</a>
    <a href="#fruit" data-click-switch="fruit banana selected" class="fruit banana tab-index">banana*</a>
    <a href="#fruit" data-click-switch="fruit pear selected" class="fruit pear tab-index">pear</a>
  </div>
  <div class="fruit apple tab-content selected">
    <h2>Apples</h2>
    <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRC3wZDxYZG1euB7Xgp7rY88MxPsCvs2s9UfDb67ncFcUMjt7a6" />
  </div>
  <div data-hover-switch="banana-hover selected" class="fruit banana tab-content">
    <h2>Bananas</h2>
    <img src="https://www.organicfacts.net/wp-content/uploads/2013/05/Banana21.jpg" />
    <div class="banana-hover tab-content"><h3>Bananas are bananas!</h3></div>
  </div>
  <div class="fruit pear tab-content">
    <h2>Pears</h2>
    <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTKXCHA7WHLj8oITo_0Rgb4Cy_4P8bH1DXSFJ52EBSTlXZ7H6RZ4LEnsmkyuw" />
  </div>
</section>
<section id="animals" class="menu animal-menu tab-content">
  <div class="tab-menu">
    <b>Animals:</b>
    <a href="#animals" data-click-switch="animals cat selected" class="animals cat tab-index selected">cat*</a>
    <a href="#animals" data-click-switch="animals dog selected" class="animals dog tab-index">dog</a>
  </div>
  <div data-hover-switch="cat-hover selected" class="animals cat tab-content selected">
    <h2>Cats</h2>
    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSn8YEWhbyMuoOIv-hkDABdhEqFU565HVuElyIhgjZ0seS85pRJExcWDns" />
    <div class="cat-hover tab-content"><h3>Meows!</h3></div>
  </div>
  <div class="animals dog tab-content">
    <h2>Dogs</h2>
    <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRTZc2-aCFM0MTmm9v-vF20ZJn8rT2tOmH9uaB5h5yOyOokeRMZ" />
  </div>
</section>
<section id="about" class="menu about tab-content selected">
  <p>
    A quick tabbing system created via javascript.
    Use the attribute <code>data-switch="fruit apple selected"</code> to select the apple fruit tab.
    <br><br>
    When clicking on the switch for apple, the script will:
    <ol>
      <li>remove the class-name <code>selected</code> from all elements containing the class <code>fruit</code>.</li>
      <li>Then, the script will add the class-name <code>selected</code> to all elements containing the class <code>apple</code>.</li>
      <li>Finally, with proper use of CSS, this tabbed menu system is functional</li>
    </ol>
    The events are attached to the document, so additional tabs can be created on the fly without "rewiring" events.
  </p>  
</section>

              
            
!

CSS

              
                * {box-sizing: border-box;}

body {font-family: sans-serif; margin: 2em auto; width: 90%; min-width: 320px;}

code {background-color: lightgrey; color: green}
#menu {margin-bottom: 2em;}
#menu div {margin-bottom: 0em; display: inline-block; width: 584px;}

.tab-menu {padding-bottom: 0.25em; border-bottom: solid 2px lightgrey; text-align: left;}
.menu b {margin-right: 2em;}

.tab-index {
  border-bottom: solid 2px clear;
  text-decoration: none;
  color: lightgrey;
  padding: 0.25em 1em;
  letter-spacing: -1px;
  transition: letter-spacing 0.25s ease, color 1s ease, border-color 2s ease;
  text-transform: lowercase;
}
.tab-index:hover {
  opacity: 0.25;
  color: green;
  /* border-bottom: solid 2px green; */
  /* text-shadow: 0px 1px 1px rgba(0,0,0,0.25); */
}
.tab-index.selected {
  text-shadow: 0px 1px 1px rgba(0,0,0,0.1);
  border-bottom: solid 2px green; 
  color: green; 
  font-weight: bold; 
  letter-spacing: 2px;
  text-transform: capitalize;
}


.tab-content {padding: 0em; margin: 0em auto 3em auto; text-align: center;}
.tab-content:not(.selected) {display: none;}

img {height: 200px; border-radius: 50%;}

section#about {text-align: left;}
              
            
!

JS

              
                // Helper Functions
util = {
  on: function(source, action, target, callback) {
    var fn;

    // target is given. Attach listener to source and look for target.   
    if (arguments.length > 3) {
      fn = function(e)  {
        // Ensure correct event parameter for older browsers.
        e = e ? e : window.event;

        // Create list of possible targets.
        var targets = source.querySelectorAll(target);

        // Only execute function if the event target matches the target query.
        for (var i = 0, len = targets.length; i < len; i++) if (e.target === targets[i]) return callback(e, source);

        return null;
      }
    }

    // no target is given.
    else fn = function(e) {return target(e, source)};    

    // attach events
    var actions = action.split(' ');
    for (var i = 0, len = actions.length; i < len; i++) {
      // Mozilla, Netscape, Firefox
      if (window.addEventListener) source.addEventListener(actions[i], fn, false);
      // IE
      else if (window.attachEvent) source.attachEvent('on' + actions[i], fn);
      // Fail Safe
      else source['on' + actions[i]] = fn;
    }
    return fn;    
  },
  
  separate: function(text, delimiter, spaces) {return text.trim().split(new RegExp('[' + ((delimiter || ',;\\s') + (delimiter && spaces ? '\\s' : '')) + ']+'))},
  
  hasClass: function(e, c) {return !!e.className.match(new RegExp('(\\s|^)' + c + '(\\s|$)'))},

  addClass: function(e, c) {if (!util.hasClass(e, c)) e.className += ' ' + c},

  removeClass: function(e, c) {
    if (util.hasClass(e, c)) {
      var reg = new RegExp('(\\s|^)' + c + '(\\s|$)');
      e.className = e.className.replace(reg, ' ');
    }
  }
}

function initSwitches() {
  
  // Event handler for clicking a switch.
  function clickHandler(evt) {
    var attr = util.separate(evt.target.getAttribute('data-click-switch')); // group, target, class
    if (attr.length > 2) {  
      // Turn off all in group.
      var group = document.getElementsByClassName(attr[0]);
      for (var i = 0, len = group.length; i < len; i++) util.removeClass(group[i], attr[2]);
      // Turn on all targets.
      var target = document.getElementsByClassName(attr[1]);
      for (var i = 0, len = target.length; i < len; i++) util.addClass(target[i], attr[2]);
      // Do not follow links.
      evt.preventDefault();
    }
  }
  
  // Event handler for hovering over a switch.
  function hoverHandler(evt) {
    var attr = util.separate(evt.target.getAttribute('data-hover-switch')); // target, class
    if (attr.length > 1) {
      var targets = document.getElementsByClassName(attr[0]);
      // Turn on all in group
      if (evt.type == 'mouseenter') for (var i = 0, len = targets.length; i < len; i++) util.addClass(targets[i], attr[1]);      
      // Turn off all in group
      if (evt.type == 'mouseleave') for (var i = 0, len = targets.length; i < len; i++) util.removeClass(targets[i], attr[1]);
    }
  }
  
  // Add an event to all switches.
  var switches;
  switches = document.querySelectorAll('[data-click-switch]');
  if (switches) for (var i = 0, len = switches.length; i < len; i++) util.on(switches[i], 'click', clickHandler);
  switches = document.querySelectorAll('[data-hover-switch]');
  if (switches) for (var i = 0, len = switches.length; i < len; i++) util.on(switches[i], 'mouseenter mouseleave', hoverHandler);
  
}

initSwitches();

// data-switch="click fruit apple selected; hover fruit-hover apple-hover selected"
              
            
!
999px

Console