只有第一张卡会翻转

时间:2016-05-02 15:03:34

标签: jquery html css css-transforms

我想创建一个照片库,在点击或标签上翻转图片,当翻转时,用户将获得有关图片本身和链接的更多信息。我发布了我的整个CSS,JS和HTML,所以我希望任何人都能发现我的错误。

JS:

$('#flip').on('click', function() {
    $(".card", this).toggleClass("flipped");
});

HTML:

<div id="flip" class="flip">
    <div class="card">
        <div class="face front">Front</div>
        <div class="face back">Back</div>
    </div>
</div>
<div id="flip" class="flip">
    <div class="card">
        <div class="face front">Front</div>
        <div class="face back">Back</div>
    </div>
</div>

CSS:

.flip {
    perspective: 800;
    width: 100%;
    height: 200px;
    position: relative;
    margin: 50px auto;
}

.flip .card.flipped {
    transform: rotatex(-180deg);
}

.flip .card {
    width: 100%;
    height: 100%;
    transform-style: preserve-3d;
    transition: 0.5s;
}

.flip .card .face {
    width: 100%;
    height: 100%;
    position: absolute;
    backface-visibility: hidden;
    z-index: 2;
    font-family: Georgia, 'Times New Roman', Times, serif;
    font-size: 3em;
    text-align: center;
    line-height: 200px;
}

.flip .card .front {
    position: absolute;
    z-index: 1;
    background: $da_grey;
    color: $white;
    cursor: pointer;
}

.flip .card .back {
    transform: rotatex(-180deg);
    background: #121212;
    color: $white;
    cursor: pointer;
}

1 个答案:

答案 0 :(得分:3)

这是因为您正在复制ID值,这应该是唯一的。将其更改为:

<div id="flip1" class="flip">
    <div class="card">
        <div class="face front">Front</div>
        <div class="face back">Back</div>
    </div>
</div>
<div id="flip2" class="flip">
    <div class="card">
        <div class="face front">Front</div>
        <div class="face back">Back</div>
    </div>
</div>
$('#flip1, #flip2').on('click', function() {
    $(".card", this).toggleClass("flipped");
});

最好为此使用类:

$('.flip').on('click', function() {
    $(".card", this).toggleClass("flipped");
});

但请确保不重复 ID