悬停时的CSS动画恢复未完成

时间:2016-03-07 19:15:54

标签: css



@import url(https://fonts.googleapis.com/css?family=Open+Sans);

body {
  height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to left, #517fa4, #243949);
  font-family: Open Sans;
}

.card-container {
  perspective: 900px;
}

.card {
  position: relative;
  width: 100px;
  height: 100px;
  box-shadow: 0 27px 55px 0 rgba(0, 0, 0, 0.3), 0 17px 17px 0 rgba(0, 0, 0, 0.15);
  transition: all 0.6s ease;
  transform-style: preserve-3d;
  backface-visibility: hidden;
  margin-right: 20px;
}

.front, .back {
  position: absolute;
  background: #7FC6A4;
  top: 0;
  left: 0;
  width: 100px;
  height: 100px;
  border-radius: 5px;
  color: white;
}

.front {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
}

.back {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.card:hover {
  transform: rotateY(180deg);
}

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

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<div class="card-container">
  <div class="card">
    <div class="front">
      <span class="fa fa-user"></span>
    </div>
    <div class="back">User</div>
  </div>
</div>
<div class="card-container">
  <div class="card">
    <div class="front">
      <span class="fa fa-cogs"></span>
    </div>
    <div class="back">Settings</div>
  </div>
</div>
<div class="card-container">
  <div class="card">
    <div class="front">
      <span class="fa fa-code"></span>
    </div>
    <div class="back">Code</div>
  </div>
</div>
&#13;
&#13;
&#13;

我有一个卡片翻转的简单CSS动画。你能否告诉我为什么悬停时的动画有时候无法完成并恢复原状?如果您尝试将鼠标悬停在卡片上,您可能会看到有时卡片会部分翻转并返回,即使我的鼠标仍悬停在卡片上。

1 个答案:

答案 0 :(得分:3)

改变这个:

true

到此:

.card:hover {
  transform: rotateY(180deg);
}

修改

为卡添加一个额外的容器,并为其添加一个右边距,如下所示。

.card-container:hover .card {
  transform: rotateY(180deg);
}

CSS:

<div class="card-container">
     <div class="card-flipper">
          <div class="card">
               ...
          </div>
     </div>
</div>