我怎么能触发这个:用jQuery悬停翻转效果?

时间:2015-01-29 15:23:12

标签: javascript jquery css onclick rotation

此代码就像一个翻转div的魅力:

查看

<div class="flip-container" ontouchstart="this.classList.toggle('hover');">
    <div class="flipper">
        <div class="front">
            FLIP ME
        </div>
        <div class="back">
            FLIP ME AGAIN
        </div>
    </div>

CSS

/* entire container, keeps perspective */
.flip-container {
        perspective: 1000;
}
        /* flip the pane when hovered */
        .flip-container:hover .flipper, .flip-container.hover .flipper {
                transform: rotateY(180deg);
        }

.flip-container, .front, .back {
        width: 320px;
        height: 480px;
}

/* flip speed goes here */
.flipper {
        transition: 0.6s;
        transform-style: preserve-3d;

        position: relative;
}

/* hide back of pane during swap */
.front, .back {
        backface-visibility: hidden;

        position: absolute;
        top: 0;
        left: 0;
}

/* front pane, placed above back */
.front {
        z-index: 2;
        /* for firefox 31 */
        transform: rotateY(0deg);
}

/* back, initially hidden pane */
.back {
        transform: rotateY(180deg);
}

但是当我将鼠标悬停在div上时,它会翻转。我希望它只在我点击它时翻转,并且每次点击都会翻转一次。 CSS中似乎没有:onclick方法。有没有办法可以使用jQuery来触发上面的CSS?

3 个答案:

答案 0 :(得分:1)

我终于找到了一个非常简单的解决方案。我所要做的就是删除:hover方法,然后切换类:

<强> HTML

<div id="flip-container" ontouchstart="this.classList.toggle('hover');">
    <div class="flipper">
        <div class="front">
            FLIP ME
        </div>
        <div class="back">
            FLIP ME AGAIN
        </div>
    </div>
</div>

<强> CSS

#flip-container {
  width: 320px;
  height: 480px;
}

.flip-container {
        perspective: 1000;
}
        .flip-container .flipper, .flip-container .flipper {
                transform: rotateY(180deg);
        }

.flip-container, .front, .back {
        width: 320px;
        height: 480px;
}

.flipper {
        transition: 0.6s;
        transform-style: preserve-3d;
        position: relative;
}

.front, .back {
        backface-visibility: hidden;

        position: absolute;
        top: 0;
        left: 0;
}

.front {
        z-index: 2;
        /* for firefox 31 */
        transform: rotateY(0deg);
}

.back {
        transform: rotateY(180deg);
}

<强> JS

$("#flip-container").click(function() {
  $(this).toggleClass("flip-container");
});

答案 1 :(得分:0)

您可以使用:active或:target来获得您正在寻找的效果。看看这个帖子:Can I have an onclick effect in CSS?

答案 2 :(得分:0)

除了我的头脑,你还需要一个带定时器的点击事件。

<强>的javaScript

var myTimerForFlip,
    myFlipTime = 600;

$('.flip-container').click(function(){
      stopMyFlip(); //reset the animation
      $(this).addClass('animatingflip'); //add animation class
      myTimerForFlip = setTimeout(removeFlip , myFlipTime); //start up timer again
});

var removeFlip = function (){
     $('.animatingflip').removeClass('animatingflip');
};

function stopMyFlip () {
    clearTimeout(myTimerForFlip);
}

新CSS

/* flip the pane when hovered */
.animatingflip .flipper, .animatingflip .flipper {
      transform: rotateY(180deg);
}

所以基本上,添加一个类&gt;动画发生了。然后在.6s之后删除课程。