css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
  <a class="navbar-brand" href="#">Navbar</a>

  <div class="collapse navbar-collapse" id="navbarsExampleDefault">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#bio" id="bio-menu">Bio</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#join-us" id="join">Join Us</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#about">About</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#science">Science</a>
      </li>
    </ul>
  </div>
</nav>

<div class="container-fluid">
  <div class="row header" id="featured">
  </div>
</div>
<div class="secondary-menu">
  <div id="secondary-menu-parent"></div>
  <ul id="sub-menu-js"></ul>
</div>
<div class="container">
  <div class="row">
    <div class="col-md-8 main">
      <div id="theContent"></div>
    </div>
  </div>
</div>
<div id="hidden-data"></div>
            
          
!
            
              .navbar {
  background-color: #000;
}

.header {
  height: 30em;
  background-color: red;
  margin-bottom: 5em;
}

a:target {
  background-color: red;
}

.page-title {
  font-size: 3em;
}

#hidden-data {
  display: none;
}
            
          
!
            
              //GET WORDPRESS JSON CONTENT
fetch(
  "https://rampages.us/kirkwarrenbrown/wp-json/wp/v2/pages?per_page=90&_embed"
)
  .then(function(response) {
  // Convert to JSON
  return response.json();
})
  .then(function(data) {
  // GOOD
  for (i = 0; i < data.length; i++) {
    writePages(data[i]);
    if (data[i].slug == urlHash()) {
      var slug = urlHash();
      setPage(slug);
    }
  }
  if (urlHash() === "" || document.getElementById(slug) === null) {
    setPage("home");
  }
  buildMenu(slug);
});

function setPage(slug) {
  var getPage = document.getElementById(slug).innerHTML;
  var getDestination = document.getElementById("theContent");
  getDestination.innerHTML = getPage;

  //set header image
  var getImage = document.getElementById(slug).dataset.img;
  var getFeatured = document.getElementById("featured");
  getFeatured.style.backgroundImage = "url('" + getImage + "')";
}

//writes the pages to a hidden div for later reference
function writePages(data) {
  var post = jQuery("#hidden-data").append(
    jQuery(
      '<div id="' +
      data.slug +
      '" class="post"' +
      getImage(data) +
      getParent(data) +
      getId(data) +
      '><h2 class="page-title">' +
      data.title.rendered +
      '</h2><div class="page-content">' +
      data.content.rendered +
      "</div></div>"
    )
  );
}

//gets featured image if exists, else default image
function getImage(data) {
  if (data.better_featured_image != null) {
    var img = data.better_featured_image.source_url;
    return 'data-img="' + img + '"';
  } else {
    return 'data-img="https://c1.staticflickr.com/5/4458/37554432650_de6121d93d_o.jpg"';
  }
}

function getParent(data) {
  if (parseInt(data.parent) > 0) {
    var parent = data.parent;
    return 'data-parent="' + parent + '"';
  } else {
    return ' data-parent="0"';
  }
}

function getId(data) {
  return ' data-id="' + data.id + '"';
}

function buildMenu(slug) {
  //get id etc
  var j = 0;
  var theId = parseInt(document.getElementById(slug).dataset.id);
  var secondMenu = document.getElementById("sub-menu-js");

  // var theParent = "";
  if (document.getElementById(slug).dataset.parent != 0) {
    var theParent = parseInt(document.getElementById(slug).dataset.parent); //get parent ID if exists
  }

  var posts = document.getElementsByClassName("post");
  document.getElementById("sub-menu-js").innerHTML = "";

  //default parent category in sub menu
  addParent(theId, theParent);

  for (i = 0; i < posts.length; i++) {
    //MENU BUILDING LOGIC
    if (
      theId == posts[i].dataset.parent ||
      theParent == posts[i].dataset.parent
    ) {
      var urlParam = posts[i].id;
      var menuName = posts[i].querySelector("h2").innerHTML;
      var a = document.createElement("a");
      var newItem = document.createElement("li");

      a.textContent = menuName;
      a.setAttribute("href", "#" + urlParam);
      newItem.appendChild(a);
      secondMenu.appendChild(newItem);
      j++;
    }
  }
  if (j === 0) {
    document.getElementById("sub-menu-js").innerHTML = "";
  }
}

//lets you do the back and forward properly --
$(window).on("hashchange", function() {
  setPage(urlHash());
  buildMenu(urlHash());
});

//GET THE HASH VARIABLE FROM URL
function urlHash() {
  if (window.location.hash) {
    var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
    //console.log(hash);
    return hash;
    // hash found
  } else {
    // No hash found
    return "about";
  }
}

function addParent(theId, theParent) {
  var secondParent = document.getElementById("sub-menu-js");
  if (55 === theId || 55 === theParent) {
    secondParent.innerHTML = '<li class="parent">About</li>';
  } else if (68 === theId || 68 === theParent) {
    secondParent.innerHTML = '<li class="parent">Science</li>';
  } else {
    secondParent.innerHTML = "";
  }
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console