CSS Hover effect only on click

2019-08-12 14:06发布

I have a group of boxes with a hover effect and I want the individual boxes to be flipped only when clicked.

I am trying to remove this block of css:

/*.flip-container:hover .flipper {
    transform: rotateY(180deg);
}*/

And use underscorejs to add it back only when clicked but not working:

window.onload = function(){
var memoryCards = document.getElementsByClassName("flipper");
_.each(memoryCards, function (card){
    this.addEventListener("click", function(){
    this.setAttribute("transform", "rotateY(180deg");
        }

);});


};

Here is my current code that flips on hover:

<div class="flip-container" >
    <div class="col-xs-3 box-size flipper" >
    <div class="front"></div>
    <div class="back"></div>
    </div>
    </div>

.box-size {
    height: 100px;
    width: 100px;
    border: 1px solid black;
    position: relative;
}



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


*.flip-container:hover .flipper {
    transform: rotateY(180deg);
}*

/* flip speed goes here */
.flipper {
    transition: 2.0s;
    transform-style: preserve-3d;
    position: relative;
}

/* front pane, placed above back */
.front {
    /*z-index: 2;*/
    /* for firefox 31 */
    transform: rotateY(0deg);
    background-color: green;
    position: absolute;
    top: 0;
    width: 100%;
    margin-left: -15px;
    height: 100px;
}


.back {
    transform: rotateY(180deg);
    background-color: red;
    position: absolute;
    top: 0;
    width: 100%;
    height: 100px;
    margin-left: -15px;
}

1条回答
Evening l夕情丶
2楼-- · 2019-08-12 14:26

Try this simple code..with css

or go to this Fiddle code

I use

HTML Part

<div class="flip-container">
  <div class="flipper" onclick="this.classList.toggle('flipped')">
      <div class="front">
        Front
      </div>
      <div class="back">
        Back
      </div>
   </div>
</div>

css part

    /* entire container, keeps perspective */
    .flip-container{
    /*perspective*/
    -webkit-perspective:1000;
       -moz-perspective:1000;
        -ms-perspective:1000;
         -o-perspective:1000;
            perspective:1000;
        padding:50px;
    }
    /* 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);
    }
    .flip-container, .front, .back{
    width:50px;
    height:50px;
    }
    /* 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, .back{
    /*backface-visibility*/
    -webkit-backface-visibility:hidden;
       -moz-backface-visibility:hidden;
        -ms-backface-visibility:hidden;
         -o-backface-visibility:hidden;
            backface-visibility:hidden;
    position:absolute;
    top:0;
    left:0;
    }
    /* front pane, placed above back */
    .front{
    z-index:2;
    }
    /* 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);
         background-color:blue;
       min-width:100px; 
       min-height:100px;
    }
    .front{
       background-color:red;
       min-width:100px;
       min-height:100px;
    }
查看更多
登录 后发表回答