123

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.

            
              <div id="app">
  <div id="response">
    <!-- the transition will be changed based on truthiness of answer -->
    <transition :name="currentTransition">
      <!-- response is a boolean, so we have toString it for the class name -->
      <!-- true/false class name controls background color -->
      <!-- we have a separate boolean, answerChecked, just to know if an answer was attempted -->
      <div id="notification" :class="response.toString()" v-if="answerChecked">{{ response }}</div>
    </transition>
  </div>

  <div id="problem">
    <!-- very basic form, nothing special -->
    <input v-model="a" readonly /> +
    <input v-model="b" readonly /> =
    <input v-model="answer">
    <button v-if="!answerChecked" @click="check">check</button>
    <button v-else @click="reset">reset</button>
  </div>
  
  <h2 id="instructions">Answer the equation, click check.<br />Click reset to try again.</h2>
</div>

            
          
!
            
              body {
  background-color: #707070;
  font-family: sans-serif;
  height: 100vh;
  overflow: hidden;
  width: 100vw;
}

#app {
  align-items: center;
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: center;
  position: relative;
  width: 100%;
}

#response {
  height: 100px;
}

#notification {
  background-color: gray;
  border: {
    color: black;
    radius: 10px;
    style: solid;
    width: 2px;
  }
  color: white;
  margin: 20px;
  padding: 20px;
  
  // using boolean makes for easy classes
  &.false {
    background-color: #C11826;
  }
  &.true {
    background-color: #307B21;
  }
}

#problem {
  align-items: center;
  background-color: white;
  border: {
    color: black;
    radius: 16px;
    style: solid;
    width: 2px;
  }
  box-sizing: border-box;
  font-size: 24px;
  padding: 16px;
  text-align: center;
  
  input {
    font-size: 24px;
    text-align: center;
    width: 2em;
  }
  button {
    background-color: white;
    border: {
      color: black;
      radius: 10px;
      style: solid;
      width: 2px;
    }
    cursor: pointer;
    font-size: 24px;
    margin-top: 16px;
    width: 100%;
  }
}

#instructions {
  color: white;
  text-align: center;
}

// we are using two animations, based on whether the answer is postive/negative
.positive-enter-active {
  animation: positive 1s;
}
@keyframes positive {
  0% {
    transform: translate3d(0, 0, 0);
  }
  25% {
    transform: translate3d(0, -20px, 0);
  }
  50% {
    transform: translate3d(0, 20px, 0);
  }
  75% {
    transform: translate3d(0, -20px, 0);
  }
  100% {
    transform: translate3d(0, 0, 0);
  }
}

.negative-enter-active {
  animation: negative 1s;
}
@keyframes negative {
  0% {
    transform: translate3d(0, 0, 0);
  }
  25% {
    transform: translate3d(-20px, 0, 0);
  }
  50% {
    transform: translate3d(20px, 0, 0);
  }
  75% {
    transform: translate3d(-20px, 0, 0);
  }
  100% {
    transform: translate3d(0, 0, 0);
  }
}
            
          
!
            
              Vue.component('notification', {
  template: '<div id="notification"><slot></slot></div>'
})

new Vue({
  el: "#app",
  data: {
    // variables for our random equation
    a: 0,
    b: 0,
    // v-model for our answer attempt
    answer: null,
    // is our answer true/false?
    response: false,
    // was answer attempted true/false?
    answerChecked: false,
    // which transition to use
    currentTransition: ''
  },
  methods: {
    // two random numbers for our equation
  	randomProblem: function () {
    	this.a = Math.floor(Math.random() * Math.floor(10));
      this.b = Math.floor(Math.random() * Math.floor(10));
    },
    check: function () {
      // is our answer true/false?
      this.response = this.a + this.b === parseInt(this.answer);
      // an answer was attempted, so true
      this.answerChecked = true;
      // decide which transition we need based on true/false answer attempt
      this.currentTransition = this.response ? 'positive' : 'negative';
    },
    // just reset everything
    reset: function () {
    	this.answer = null;
      this.answerChecked = false;
      this.randomProblem();
    }
  },
  // random equation when we start
  mounted () {
  	this.randomProblem();
  }
})
            
          
!
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.

Console