在记忆游戏中单击时翻转特定卡片
Flipping specific cards onClick in a memory game
所以我正在玩一个记忆游戏,我的卡片可以很好地翻转,但每当我点击翻转一张时,它都会翻转所有卡片。如果可能的话,我宁愿使用一种不给每张卡一个 ID 的方法...请帮忙。
HTML
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
</div>
CSS
.cardsContainer {
width: 230px;
height: 320px;
position: relative;
border: 1px dashed red;
-webkit-perspective: 800px;
-moz-perspective: 800px;
-o-perspective: 800px;
perspective: 800px;
margin: 20px 10px 10px 10px;
display: inline-block; } .card {
width: 100%;
height: 100%;
position: absolute;
-webkit-transition: -webkit-transform 1s;
-moz-transition: -moz-transform 1s;
-o-transition: -o-transform 1s;
transition: transform 1s;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-transform-origin: 50% 50%; } .card div {
display: block;
height: 100%;
width: 100%;
line-height: 260px;
color: white;
text-align: center;
font-weight: bold;
font-size: 140px;
position: absolute;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-o-backface-visibility: hidden;
backface-visibility: hidden; } .card .front { } .card .back {
-webkit-transform: rotateY( 180deg );
-moz-transform: rotateY( 180deg );
-o-transform: rotateY( 180deg );
transform: rotateY( 180deg ); } .card.flipped {
-webkit-transform: rotateY( 180deg );
-moz-transform: rotateY( 180deg );
-o-transform: rotateY( 180deg );
transform: rotateY( 180deg ); }
JS
function flip() {
$('.card').toggleClass('flipped');
}
您显示的函数不是JavaScript,而是JQuery。在JQuery中,$('.card')
表示"all the elements with class 'card'"。您需要做的是将一个 onclick()
函数附加到每个只翻转自身的卡片对象。
尝试将 HTML 更改为 onclick="flip(this)"
,并将 flip
函数更改为仅更改传递给它的元素:
function flip(e) { e.toggleClass('flipped'); }
所以我正在玩一个记忆游戏,我的卡片可以很好地翻转,但每当我点击翻转一张时,它都会翻转所有卡片。如果可能的话,我宁愿使用一种不给每张卡一个 ID 的方法...请帮忙。
HTML
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
<section class="cardsContainer">
<div class="card" onclick="flip()">
<div class="front"><img src="../images/backsideBlue.png"></div>
<div class="back"><img src="../images/cookie.JPG"></div>
</div>
</section>
</div>
CSS
.cardsContainer {
width: 230px;
height: 320px;
position: relative;
border: 1px dashed red;
-webkit-perspective: 800px;
-moz-perspective: 800px;
-o-perspective: 800px;
perspective: 800px;
margin: 20px 10px 10px 10px;
display: inline-block; } .card {
width: 100%;
height: 100%;
position: absolute;
-webkit-transition: -webkit-transform 1s;
-moz-transition: -moz-transform 1s;
-o-transition: -o-transform 1s;
transition: transform 1s;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-transform-origin: 50% 50%; } .card div {
display: block;
height: 100%;
width: 100%;
line-height: 260px;
color: white;
text-align: center;
font-weight: bold;
font-size: 140px;
position: absolute;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-o-backface-visibility: hidden;
backface-visibility: hidden; } .card .front { } .card .back {
-webkit-transform: rotateY( 180deg );
-moz-transform: rotateY( 180deg );
-o-transform: rotateY( 180deg );
transform: rotateY( 180deg ); } .card.flipped {
-webkit-transform: rotateY( 180deg );
-moz-transform: rotateY( 180deg );
-o-transform: rotateY( 180deg );
transform: rotateY( 180deg ); }
JS
function flip() {
$('.card').toggleClass('flipped');
}
您显示的函数不是JavaScript,而是JQuery。在JQuery中,$('.card')
表示"all the elements with class 'card'"。您需要做的是将一个 onclick()
函数附加到每个只翻转自身的卡片对象。
尝试将 HTML 更改为 onclick="flip(this)"
,并将 flip
函数更改为仅更改传递给它的元素:
function flip(e) { e.toggleClass('flipped'); }