简体   繁体   中英

How can I trigger this :hover flip effect with jQuery?

This code works like a charm to flip a div:

view

<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);
}

But it flips when I hover over the div. I would like for it to flip only when I click on it, and keep flipping once every time I click. There doesn't appear to be an :onclick method in CSS. Is there a way I can use jQuery to trigger the CSS above?

I finally figured out a very simple solution. All I had to do was remove the :hover method and then toggle the class:

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");
});

You could use :active or :target to get the effect you're looking for. Look at this thread: Can I have an onclick effect in CSS?

Off the top of my head you'll need to a click event with a timer as well.

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);
}

New CSS

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

So basically, add a class > animation happens. Then after .6s remove the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM