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 class="content">
	<h1 class="title">Eat Me</h1>
	<ul id="choco-base">

	</ul>
</div>
            
          
!
            
              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;
}
            
          
!
            
              // チョコの座標を決めるための配列を作成
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
🕑 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