Flip a 3D card with CSS

前端 未结 2 1768
星月不相逢
星月不相逢 2020-11-27 23:02

I\'m trying to make a 3D card flipping effect with CSS like this.

The difference is that I want to use only CSS to implem

2条回答
  •  情书的邮戳
    2020-11-27 23:28

    I simplified the code to make it shorter and make the 3d card flip on hover. The card flips on the Y axis from the front face to the backface this is what it looks like:

    This is what I changed for the filp effect: - the front face wasn't rotated on th Y axis on hover - the hover effect was launched when the .back div was hovered. This can create flickering as that div is rotating and "disapears" at mid rotation. It's better to launch the animation when the static parent is hovered. - the first parent isn't really usefull so I removed it

    Here is an example of a simple CSS only flipping card the flip animation is launched on hover :

    .card {
      position: relative;
      width: 9rem; height: 12rem;
      perspective: 30rem;
    }
    .front, .back {
      position: absolute;
      width: 100%; height: 100%;
      transition: transform 1s;
      backface-visibility:hidden;
    }
    .front { 
      background-color: #66ccff; 
    }
    .back { 
      background-color: #dd8800; 
      transform: rotateY(180deg); 
    }
    .card:hover .front{ transform: rotateY(180deg); }
    .card:hover .back { transform: rotateY(360deg); }
    Front
    Back

    Note that you will need to add vendor prefixes depending on the browsers you want to support. See canIuse for 3d transforms and transitions.

提交回复
热议问题