CSS Hover效果仅限于点击

时间:2015-08-23 16:02:06

标签: javascript css hover underscore.js

我有一组具有悬停效果的盒子,我希望仅在单击时才能翻转各个盒子。

我正在尝试删除这个css块:

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

并使用underscorejs仅在单击但不起作用时才将其添加回来:

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

);});


};

这是我当前在悬停时翻转的代码:

<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 个答案:

答案 0 :(得分:0)

尝试使用css

这个简单的代码

或转到此Fiddle code

我用

HTML部分

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