.toggleClass()方法的奇怪行为

时间:2014-06-26 19:34:24

标签: javascript jquery html css css3

我有两个div元素(卡片),我想要执行翻转效果(使用css3)。问题是当用户点击div元素(即卡)时,所有卡都被翻转。我想只翻转被点击的div(卡片),而不是所有div。有人能告诉我哪里有问题吗?

这是jsFiddle演示:

jsFiddle

以下是使用的代码:

HTML:

<div class="flip-container">
    <div class="flipper" id="flipper">
        <div class="front"> 
        </div>
    <div class="back">
            <img src="http://icons.iconarchive.com/icons/reclusekc/kulo/96/Skull-1-icon.png">
        </div>
        <div class="front"> 
        </div>
    <div class="back">
            <img src="http://img9.uploadhouse.com/fileuploads/17699/176992615db99bb0fd652a2e6041388b2839a634.png">
        </div>

    </div>
</div>

JavaScript的:

/*Jquery is incuded */
$(".flipper").click(function(){
    var id = $(this).attr("id");
    document.getElementById(id).classList.toggle('flipped');
});

CSS:

/* entire container, keeps perspective */
.flip-container{
/*perspective*/
-webkit-perspective:1000;
   -moz-perspective:1000;
    -ms-perspective:1000;
     -o-perspective:1000;
        perspective:1000;
    display: table;
        margin: 0px auto;
    width: auto;
}
/* flip the pane when hovered */
    .flipped {
/*transform*/
-webkit-transform:rotateY(180deg);
   -moz-transform:rotateY(180deg);
    -ms-transform:rotateY(180deg);
     -o-transform:rotateY(180deg);
        transform:rotateY(180deg);
}
.front, .back{
 float: left;
    width: 100px;
    height: 120px;
    margin: 5px;
    padding: 5px;
    border: 4px solid #EE872A;
    cursor: pointer;
    border-radius: 10px;
    box-shadow: 0 1px 5px rgba(0,0,0,.5);
        z-index:2;
  background: #B1B1B1;
    /*backface-visibility*/
-webkit-backface-visibility:hidden;
   -moz-backface-visibility:hidden;
    -ms-backface-visibility:hidden;
     -o-backface-visibility:hidden;
        backface-visibility:hidden;

}
/* flip speed goes here */
.flipper {
/*transition*/
-webkit-transition:0.6s;
   -moz-transition:0.6s;
     -o-transition:0.6s;
        transition:0.6s;
/*transform-style*/
-webkit-transform-style:preserve-3d;
   -moz-transform-style:preserve-3d;
    -ms-transform-style:preserve-3d;
     -o-transform-style:preserve-3d;
        transform-style:preserve-3d;
position:relative;
}
/* hide back of pane during swap */
/* front pane, placed above back */
/* back, initially hidden pane */
.back{
/*transform*/
-webkit-transform:rotateY(180deg);
   -moz-transform:rotateY(180deg);
    -ms-transform:rotateY(180deg);
     -o-transform:rotateY(180deg);
        transform:rotateY(180deg);
    z-index:3;
}

3 个答案:

答案 0 :(得分:3)

这应该是技巧http://jsfiddle.net/wvveY/61/

<div class="flip-container">
    <div class="flipper">
        <div class="front"></div>
        <div class="back">
            <img src="http://icons.iconarchive.com/icons/reclusekc/kulo/96/Skull-1-icon.png">
        </div>
    </div>
    <div class="flipper">
        <div class="front"></div>
        <div class="back">
            <img src="http://img9.uploadhouse.com/fileuploads/17699/176992615db99bb0fd652a2e6041388b2839a634.png">
        </div>
    </div>
</div>


$(".flipper").click(function () {
    $(this).toggleClass('flipped');
});

答案 1 :(得分:0)

尝试在JS中使用它

$(".flipper").click(function(){
    $(this).toggleClass('flipped');
});

<强>更新 和您的HTML如下:

<div class="flipper">
    <div class="front"> 
    </div>
    <div class="back">
       <img src="http://icons.iconarchive.com/icons/reclusekc/kulo/96/Skull-1-icon.png">
    </div>
</div>

<div class="flipper">
    <div class="front"> 
    </div>
    <div class="back">
        <img src="http://img9.uploadhouse.com/fileuploads/17699/176992615db99bb0fd652a2e6041388b2839a634.png">
    </div>
</div>

答案 2 :(得分:0)

也许你想要那样http://jsfiddle.net/wvveY/67/

您需要将卡片分开,如下所示:

<div class="flip-container">
    <div class="flipper" >
        <div class="front"> 
        </div>
    <div class="back">
            <img src="http://icons.iconarchive.com/icons/reclusekc/kulo/96/Skull-1-icon.png">
        </div>
        </div>
        <div class="flipper" >
        <div class="front"> 
        </div>
    <div class="back">
            <img src="http://img9.uploadhouse.com/fileuploads/17699/176992615db99bb0fd652a2e6041388b2839a634.png">
        </div>