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

              
                <div id="app">
  <p class="victoryState" v-if="victory">Done! <button v-on:click="cardsShuffle" class="btn btn-primary">Refresh ?</button></p>
  <div v-for="(card, index) in cards" :key="index" :class="[{'down': card.down && !card.matched, 'up': !card.down, 'matched': card.matched}, ' card']" v-on:click="handleClick(card)">
    <i :class="'fas ' + card.icon"></i>
  </div>
</div>
              
            
!

CSS

              
                body {
  background: oldlace;
  padding: 20px;
  font-family: Helvetica, Arial, Sans-Serif;
}

#app {
  display: grid;
  grid-template-columns: repeat( auto-fit, 100px );
  grid-auto-rows: 100px;
  grid-gap: 15px;
  justify-content: center;
  perspective: 800px;
  max-width: 720px;
  margin: 0 auto;
}

i.fas {
  font-size: 40px;
  color: #333;
}

.victoryState {
   grid-column-start: 1;
   grid-column-end: -1;
   text-align: center;
   display: flex;
   justify-content: space-between;
   align-items: center;
}

.card {
  background: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
  box-shadow: 5px 5px 0 0 #333;
  cursor: pointer;
  animation: flipUp .5s forwards;
  i {
    opacity: 1;
    transition: opacity .5s;
  }
  &.down {
    animation: flipDown .5s forwards;
    i {
      opacity: 0;
    }
  }
  &.matched {
    animation: matching .3s forwards;
  }
}

@keyframes flipDown {
  0% {
    background: #fff;
    transform: rotateY(0deg);
    box-shadow: 5px 5px 0 0 #333;
  }
  100% {
    background: rgb(115, 132, 127);
    transform: rotateY(180deg);
    box-shadow: -5px 5px 0 0 #333;
  }
}

@keyframes flipUp {
  0% {
    background: rgb(115, 132, 127);
    transform: rotateY(180deg);
    box-shadow: -5px 5px 0 0 #333;
  }
  100% {
    background: #fff;
    transform: rotateY(0deg);
    box-shadow: 5px 5px 0 0 #333;
  }
}

@keyframes matching {
  0%{background: #fff;}
  100%{background: lightpink;}
}
              
            
!

JS

              
                // List of font awesome codes used as illustrations, can be modified
const icons = [
	'fa-kiwi-bird',
  'fa-chess',
  'fa-frog',
  'fa-camera-retro',
  'fa-plug',
  'fa-anchor',
  'fa-birthday-cake',
  'fa-cube',
  'fa-dice',
  'fa-bug',
  'fa-cut',
  'fa-gem'
];

// Duplicate elements of an array
const duplicate = (arr) => {
	return arr.concat(arr).sort()
};

// Check if two cards are a match
const checkMatch = (icons) => {
	if (icons[0] === icons[1]) {
  	console.log("it's a match");
  	return true;
  }
};

new Vue({
  el: "#app",
  data: {
    cards: _.range(0, icons.length * 2),
    runing: false
  },
  methods: {
  	// Create cards array based on icons, shuffle them
  	cardsShuffle() {
    	// prep objects
    	this.cards.forEach((card, index) => {
      	this.cards[index] = {
        	icon: '',
          down: true,
          matched: false
        }
      });
      // input every icon two times
    	icons.forEach((icon, index) => {
        this.cards[index].icon = icon;
        this.cards[index + icons.length].icon = icon;
      });
      this.cards = _.shuffle(this.cards);
    },
    handleClick(cardClicked) {
    	if (!this.runing) {
      	// turn card up
      	if (!cardClicked.matched && this.cardCount.cardsUp < 2) {
     	 		cardClicked.down = false;
     	 	}
        // when two cards are up, check if they match or turn them down
        if (this.cardCount.cardsUp === 2) {
        	this.runing = true;
          setTimeout(() => {
     	 			let match = checkMatch(this.cardCount.icons);
     	 			this.cards.forEach((card) => {
     	 				if (match && !card.down && !card.matched) {
     	 					card.matched = true;
     	 				} else {
     	 	 				card.down = true;
     	 	 			}
     	 			});
         		this.runing = false;
     	 		}, 1500);
        }
      }
    }
  },
  mounted() {
  	this.cardsShuffle();
  },
  computed: {
  	// make a count of cards up and cards matched, keep icons of cards to check in array
  	cardCount: function() {
    	let cardUpCount = 0;
      let cardMatchedCount = 0;
      let icons = [];
    	this.cards.forEach((card) => {
      	if (!card.down && !card.matched) {
        	cardUpCount++;
          icons.push(card.icon);
        }
        if (card.matched) {
        	cardMatchedCount++;
        }
      });
      return {
      	cardsUp: cardUpCount, 
        cardsMatched: cardMatchedCount,
        icons: icons}
    },
    // update victory state
    victory: function() {
    	if (this.cardCount.cardsMatched === this.cards.length) {
      	return true;
      }
    }
  }
})
              
            
!
999px

Console