Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="content">
	<h1 class="title">Eat Me</h1>
	<ul id="choco-base">

	</ul>
</div>
              
            
!

CSS

              
                html {
	width: 100%;
	height: 100%;
}
body {
	background-image: radial-gradient(#975d34 50%, #754a2c 100%);

	overflow: hidden;
}
.content{
	position: absolute;
	top: 50%;
	left: 50%;
	transform:translate(-50%, -50%);
}

.title {
	font-family: 'Leckerli One', cursive;
	color: #5F371D;
	font-size: 54px;
	margin: 0 auto 15px auto;
	text-align: center;
}
#choco-base{
	position: relative;
	list-style: none;
	background: url(http://designlab.innovator.jp.net/015_blog_images/bg.jpg) 0 0 no-repeat;
	-webkit-background-size: contain;
	background-size: contain;
	width: 600px;
	height: 292.55px;
	padding-left: 0;
}
.choco-item {
	position: absolute;
	width: 150px;
	height: 97.5px;
	cursor: pointer;
}
.choco-item.mode_selected .choco-img {
	opacity: 0;
}
.choco-item.mode_eaten .choco-img {
	opacity: 0;
}
.choco-item.mode_eaten .choco-card {
	opacity: 0;
}
.choco-item img {
	width: 150px;
	height: 97.5px;
	position: absolute;
	top: 0;
	left: 0;
}
              
            
!

JS

              
                // チョコの座標を決めるための配列を作成
var coordinate = [];
for(i = 0; i < 4; i++) {
	for (j = 0; j < 3; j ++) {
		coordinate.push([i+1, j]);
	}
}

// 配列をシャッフル
function shuffle(array) {
	var n = array.length, t, i;
	while (n) {
		i = Math.floor(Math.random() * n--);
		t = array[n];
		array[n] = array[i];
		array[i] = t;
	}
	return array;
}
shuffle(coordinate);

// チョコカードをランダムに並べる
var choco_width = 150;
var choco_height = 97.5;
var choco_count = 12;

for (i = 0; i < choco_count; i ++){
	var choco_base = document.querySelector("#choco-base");
	var choco = document.createElement('li');
	choco.classList.add('choco-item');

	// チョコカードのimg要素を追加
	var choco_img = document.createElement('img');
	choco_img.src= "http://designlab.innovator.jp.net/015_blog_images/choco.jpg";
	choco_img.classList.add('choco-img');

	// チョコカードの裏の柄のimg要素を追加
	var card_img = document.createElement('img');
	var img_num;
	(i + 1)%2 != 0 ? img_num = (i + 2)/2 : img_num = (i + 1)/2;
	card_img.src= 'http://designlab.innovator.jp.net/015_blog_images/' + img_num + '.jpg';
	card_img.classList.add('choco-card');

	// チョコ要素に、チョコカードのimg要素とチョコカードの裏の柄のimg要素を追加
	choco.appendChild(card_img);
	choco.appendChild(choco_img);

	// チョコの座標を指定
	choco.style.left = choco_width * (coordinate[0][0]-1) + 'px';
	choco.style.top = choco_height * coordinate[0][1] + 'px' ;

	// どのチョコか判断するためのidを指定
	choco.setAttribute('onclick', 'turn(' + coordinate[0][0] + coordinate[0][1] + ');');
	choco.setAttribute('id', 'choco_' + coordinate[0][0] + coordinate[0][1]);

	choco_base.appendChild(choco);
	coordinate.shift();
}

// チョコがクリックされた時の挙動
var first_choco;
var first_choco_name;
var second_choco;
var second_choco_name;
var select_num = 0;
// チョコをクリックしたら
function turn(choco_id) {
	// 1個目のチョコか2個目のチョコか判断
	if((select_num + 1)%2 != 0) {
		first_choco = document.querySelector('#choco_' + choco_id);
		first_choco_name = first_choco.firstChild.src;
		first_choco.classList.add('mode_selected');
	} else {
		// 選択中のチョコをクリックした場合の処理
		if('choco_' + choco_id == first_choco.getAttribute('id')) {
			console.log('same!!');
			first_choco.classList.remove('mode_selected');
		} else {
			second_choco = document.querySelector('#choco_' + choco_id);
			second_choco_name = second_choco.firstChild.src;
			second_choco.classList.add('mode_selected');
			// 1個目のチョコと2個目のチョコか同一か同じ柄か判断
			setTimeout('judgment(first_choco, first_choco_name, second_choco, second_choco_name)', 500);
		}
	}
	select_num ++;
}
function judgment(first_choco, first_choco_name, second_choco, second_choco_name) {
	if (first_choco_name == second_choco_name) {
		first_choco.classList.add('mode_eaten');
		second_choco.classList.add('mode_eaten');
	} else {
		first_choco.classList.remove('mode_selected');
		second_choco.classList.remove('mode_selected');
	}
}
              
            
!
999px

Console