cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <section id="intro">
  <p class="logo"><img src="http://static1.squarespace.com/static/535d6ce2e4b0faf2733ec3fe/t/54d951d7e4b05a1e7369644b/1445862036999/?format=1500w" alt="Crown Town Compost" /></p>
  <section>
    <h1>We're three guys with specially-equipped bikes</h1>
    <p>We prepared a short game to teach you more about composting.</p>
  </section>
  <button onclick="location.href='#game';">Start game</button>
</section>
<section id="game">
  <p id="score">Get 5 right to win</p>
  <h2 id="food"></h2>
  <p id="answer">Select what bin it goes in</p>
  <div class="button-group">
    <button onclick="submitAnswer(event);">Trash</button>
    <button onclick="submitAnswer(event);">Recycle</button>
    <button onclick="submitAnswer(event);">Compost</button>
  </div>
</section>
<section id="facts">
  <h2>Complete the game to unlock hard-hitting facts</h2>
</section>
<section id="cta">
  <p class="logo"><img src="http://static1.squarespace.com/static/535d6ce2e4b0faf2733ec3fe/t/54d951d7e4b05a1e7369644b/1445862036999/?format=1500w" alt="Crown Town Compost" /></p>
  <section>
    <h1>Let's make a difference.</h1>
    <p>Together we can divert large amounts of food waste from landfills.</p>
  </section>
  <button onclick="location.href='http://crowntowncompost.com'">Join us</button>
</section>
            
          
!
            
              * {
  box-sizing: border-box;
}
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
body {
  background-color: #2c3e50;
  color: white;
  font-family: sans-serif;
}
h1 {
  font-size: 2.2em;
  line-height: 1.2;
}
#game h2 {
  font-size: 3em;
  font-weight: normal;
}
#game section {
  display: flex;
  justify-content: stretch;
  align-items: stretch;
  flex: 3;
}
section {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}
section > * {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
}
section > section {
  flex-grow: 3;
  text-align: left;
}
#game p {
  padding: 10px;
}
#intro > section,
#cta > section {
  padding: 20px;
  line-height: 1.4;
}
p {
  font-size: 1.2em;
}
#score {
  flex: 0 0 50px;
  color: white;
  font-size: 1.4em;
  background: #e94c3c;
  padding: 0;
  text-transform: uppercase;
  letter-spacing: 2px;
}
#instructions {
  display: flex;
  flex-direction: column;
  line-height: 2;
}
#instructions:before {
  content: "\21E7";
  color: white;
}
#instructions:after {
  content: "\21E9";
  color: #e94c3c;
}
#food {
  flex: 2;
}
#bin {
  color: #e94c3c;
}
.logo {
  flex: 0 0 120px;
}
.logo img {
  width: 80px;
}
#facts > * {
  flex-direction: column;
}
.button-group {
  display: flex;
  align-items: stretch;
  margin-top: auto;
  flex: 1;
  justify-content: space-between;
}
.button-group button {
  flex: 1;
  font-size: 1em;
}
button {
  color: white;
  text-shadow: 2px 2px 0px black;
  background-color: #111;
  border: none;
  border-bottom: 5px solid #e94c3c;
  font-size: 2em;
  text-transform: uppercase;
}
button:hover {
  background-color: black;
}
button:active {
  background-color: #e94c3c;
}
#answer.success {
  background-color: rgba(255, 255, 255, .2);
}
#answer.failure {
  background-color: rgba(0, 0, 0, .2);
}
#answer {
  flex: 1 0 auto;
  font-size: 1em;
  text-transform: uppercase;
  letter-spacing: 1px;
}
#facts ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  flex: 5;
}
#facts li {
  font-size: 2em;
  padding: 10px;
}
#facts li:before {
  content: 'fact';
  display: block;
  text-transform: uppercase;
  color: #e94c3c;
  font-size: .8em;
  border-bottom: 2px solid #e94c3c;
  border-bottom-width: 50%;
  margin: 0 100px;
  margin-bottom: 10px;
}
            
          
!
            
              var foodList = [
  {
    name: "Vegetable",
    bin: "Compost"
  },
  {
    name: "Fruit",
    bin: "Compost"
  },
  {
    name: "Egg shells",
    bin: "Compost"
  },
  {
    name: "Meat",
    bin: "Trash"
  },
  {
    name: "Dairy",
    bin: "Trash"
  },
  {
    name: "Charcoal",
    bin: "Trash"
  },
  {
    name: "Newspaper",
    bin: "Recycle"
  },
  {
    name: "Pizza boxes",
    bin: "Recycle"
  },
  {
    name: "Aluminum",
    bin: "Recycle"
  },

];

var factList = [
  "100,000 pounds of waste are generated each day in Uptown Charlotte",
  "253 pounds of food waste are produced each year by the average US consumer.",
  "2-5 percent of food waste is diverted from landfills each year by home composting."
]
var factSection = document.getElementById('facts');
var factH2 = factSection.getElementsByTagName('h2')[0];
var food = document.getElementById('food');
var answer = document.getElementById('answer');
var randomItem;
var buttonGroup = document.getElementsByClassName('button-group')[0];
var gameArea = document.getElementById('game');
var buttons = gameArea.getElementsByTagName('button');
var score = document.getElementById('score');
var counter = 0;
var numQuestions = 5;

function loadGame(array) {
  randomItem = Math.floor(Math.random() * array.length);
  food.textContent = array[randomItem].name;
}

function submitAnswer(event) {
  resetStyles();
  checkAnswer(event);
  reloadGame(foodList);
}

function resetStyles() {
  answer.classList.remove("success");
  answer.classList.remove("failure");
  var buttons = document.getElementById('game').getElementsByTagName('button');
  for (var i = 0; i < buttons.length; i++) {
    buttons[i].blur();
  }
}

function checkAnswer(event) {
  foodList[randomItem].bin == event.target.textContent ? showResult(true) : showResult(false);
}

function reloadGame(array) {
  if (array.length > 1 && !isGameOver()) {
    removeItem(foodList, randomItem);
    loadGame(foodList);
  } else {
    endGame();
  }
}

function endGame() {
  if(isGameOver()) {
    food.textContent = "You won!";
  } else {
    food.textContent = "That's all.";
  }
  var factButton = document.createElement('button');
  factButton.textContent = "See the facts";
  factButton.setAttribute('onclick', "location.href=\"#facts\"");
  gameArea.removeChild(buttonGroup);
  gameArea.appendChild(factButton);
  showFact();
}

function removeItem(array, position) {
    array.splice(position,1);
}

function showResult(boolean) {
  switch(boolean) {
    case true:
      answer.innerHTML = "Great job!<br/> " + foodList[randomItem].name + " = " + foodList[randomItem].bin;
      answer.classList.add("success");
      score.textContent = (++counter) + "/" + numQuestions;
      break;
    case false:
      answer.innerHTML = "Nice try.<br/> " + foodList[randomItem].name + " = " + foodList[randomItem].bin;
      answer.classList.add("failure");
      score.textContent = (counter) + "/" + numQuestions;
      break;
  }
}

function isGameOver() {
  if (counter == numQuestions) {
    return true;
  }
}

function showFact() {
  factSection.removeChild(factH2);
  var newFactUl = document.createElement('ul');
  var newFactLi = document.createElement('li');
  newFactLi.textContent = factList[0];
  newFactUl.appendChild(newFactLi);
  factSection.insertBefore(newFactUl, factSection.firstChild);
  
  var button = document.createElement('button');
  button.addEventListener('click', nextFact, false);
  button.textContent = "Next fact";
  factSection.appendChild(button);
}

function nextFact() {
  var factLi = factSection.getElementsByTagName('li')[0];
  var button = factSection.getElementsByTagName('button')[0];
  switch (factLi.textContent) {
    case factList[0]:
      factLi.textContent = factList[1];
      break;
    case factList[1]:
      factLi.textContent = factList[2];
      button.textContent = "Finish";
      button.removeEventListener('click', nextFact, false);
      button.setAttribute('onclick', "location.href='#cta'");
      break;
  }
}

loadGame(foodList);
            
          
!
999px
Loading ..................

Console