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

              
                <nav id="main_nav"></nav>

<h3>Menu modeled in Kontent.ai</h3>
<p>No part of the navigation is hardcoded, it's all dynamically generated based on dedicated content items retrieved from your Kontent project.</p>
<p>Hover individual menu items to display the sub-menu drop-down.</p>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
}

body {
  font: sans-serif;
  font-size: 12px;
  background: #eee;
  margin: 1em;
  padding: 1.5em;
}

h1 {
  display: block;
  font-size: 2em;
  font-weight: 400;

}

a {
  color: #06c;
}

nav {
  min-height: 35px;
}

nav ul {
  background: #ccc;
  float: left;
  -webkit-transition: .5s;
  transition: .5s;
}

nav li {
  float: left;
  position: relative;
  width: 150px;
  list-style: none;
  -webkit-transition: .5s;
  transition: .5s;
  border-bottom: 1px solid #999;
}

nav > ul > li > a, h1 {
  text-transform: uppercase;
}

nav a {
  display: block;
  text-decoration: none;
  padding: 5px 15px;
  color: #000;
}

nav ul ul {
  position: absolute;
  left: 0;
  top: 100%;
  visibility: hidden;
  opacity: 0;
}

nav ul ul ul {
  left: 100%;
  top: 0;
}

nav li:hover, nav li:hover li {
  background: #ddd;
}

nav li li:hover, nav li li:hover li {
  background: #bbb;
}

nav li li li:hover {
  background: #999;
}

nav li:hover > ul {
  visibility: visible;
  opacity: 1;
}
              
            
!

JS

              
                // Use jQuery to get the root navigation item via Kontent.ai Delivery API.
$.ajax({
  type: "GET",
  url: "https://deliver.kontent.ai/8d20758c-d74c-4f59-ae04-ee928c0816b7/items/root_navigation_item?depth=5",
  dataType: "json",
  success: processData,
  error: function() {
    alert("failed");
  }
});

// Process the retrieved data using the itemsToList function and display it.
function processData(data) {
  var rootMenuItems = data.item.elements.subitems.value;
  var allLinkedContent = data.modular_content;
  
  var menuHtml = itemsToList(rootMenuItems, allLinkedContent, 0);
  $("#main_nav").append(menuHtml);
}

// Limit recursion depth in case of cyclic references between content items. 
// (Otherwise your menu would be infinitely large and your site would crash.)
const DEPTH_LIMIT = 5;

// Generate menu HTML by recursively transforming content items to list elements.
function itemsToList(itemCodenames, linkedContent, currentDepth) {
    if(currentDepth >= DEPTH_LIMIT) {
      return "";
    }
    var result = "<ul>";
    for(let itemCodename of itemCodenames){
      let title = linkedContent[itemCodename].elements.title.value;
      let url = linkedContent[itemCodename].elements.url.value;
      console.log(linkedContent[itemCodename])
      if (linkedContent[itemCodename].elements.subitems) {
        let subitems = linkedContent[itemCodename].elements.subitems.value;
        // console.log(title, url);
        result += "<li><a href='#/"+ url +"' title='/"+ url +"'>" + title + "</a>"
               + itemsToList(subitems, linkedContent, currentDepth+1) +"</li>";  
      }
      else {
        result += "<li><a href='#/"+ url +"' title='/"+ url +"'>" + title + "</a></li>";  
      }
    }
    result += "</ul>";
    return result;
  }
              
            
!
999px

Console