【问题标题】:how do animation without keyframes but with transition没有关键帧但有过渡的动画怎么做
【发布时间】:2018-03-12 03:16:54
【问题描述】:

div {
    height: 41.4vmin;
    width: 30vmin;
    margin: 0.7vmin;
    border-radius: 1.3vmin;
    background-image: url("https://sun9-8.userapi.com/c840530/v840530203/60933/tkZK7aYQUjM.jpg");
    background-size: contain;
}


@keyframes example {
  0% {}
  35% {background-image: url("https://sun9-8.userapi.com/c840530/v840530203/60933/tkZK7aYQUjM.jpg");}
  36% {background-image: url("https://sun9-8.userapi.com/c840530/v840530203/6092c/fR8eCsT009k.jpg");}
  100% {background-image: url("https://sun9-8.userapi.com/c840530/v840530203/6092c/fR8eCsT009k.jpg");transform: rotateY(160deg);}
}

div:hover {
  
  animation-fill-mode: forwards;
  animation-name: example;
  animation-duration: 1s;

}
<div></div>

我不使用关键帧,因为我需要为每个动画设置唯一的 url。如果我这样做,那么需要创建更多 100 个关键帧。 我想这样做是因为使用转换可以设置不同的 url 方法 js。但这看起来不像我想要的。

div {
    height: 41.4vmin;
    width: 30vmin;
    margin: 0.7vmin;
    border-radius: 1.3vmin;
    background-image: url("https://sun9-8.userapi.com/c840530/v840530203/60933/tkZK7aYQUjM.jpg");
    background-size: contain;
    transition: 
    /* step 1 */
    transform      1s,
    /* step 2 */
    background  0.0s 0.5s;
}



div:hover {
  transform: rotateY(160deg);
  background-image: url("https://sun9-8.userapi.com/c840530/v840530203/6092c/fR8eCsT009k.jpg");
}
  <div></div>

【问题讨论】:

  • 请不要将您的代码发布到 3rd 方网站,因为这些链接会随着时间的推移而失效,如果我们不得不去另一个网站查看您的代码,这会更加麻烦。只需在您的问题中将您的代码包含在“code sn-p”中即可。
  • transition 只能保持一个变化的时间对于真正的动画你需要动画关键字。
  • 好的,谢谢你编辑我的问题

标签: javascript css animation


【解决方案1】:

我的解决方案,您可以在 card-container 元素内创建两个元素 frontback 并相应地制作动画:

$(document).ready(function () {
  $('.card-container').click(function () {
    $(this).toggleClass('clicked');
  });
});
.card-container {
  position: relative;
  overflow: hidden;
  height: 41.4vmin;
  width: 30vmin;
  margin: 0.7vmin;
}

.card-container > div {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  border-radius: 1.3vmin;
  transition: transform 1s, background  0.0s 0.5s;
  transform-style: preserve-3d;
}

.card-container .front {
    background-image: url("https://sun9-8.userapi.com/c840530/v840530203/60933/tkZK7aYQUjM.jpg");
    background-size: contain;
    background-color: black;
    transform: rotateY(0deg);
}

.card-container .back {
  background-image: url("https://sun9-8.userapi.com/c840530/v840530203/6092c/fR8eCsT009k.jpg");
  background-size: contain;
  transform: rotateY(180deg);
}

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

.card-container.clicked .back {
  transform: rotateY(0deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card-container">
  <div class="front"></div>
  <div class="back"></div>
</div>

<div class="card-container">
  <div class="front"></div>
  <div class="back"></div>
</div>

【讨论】:

  • 感谢您的帮助我没有告诉我需要点击开始动画。如果我将使用您的代码,那么由于after,我无法收听事件click
  • @Pegos 我修改了答案。
  • 我看到了您的解决方案,一切正常。但只有一张卡,我尝试将click 类设置为多个,动画不起作用。如果您提出真正的解决方案,我将非常感激,当然感谢您的回答。
  • 好吧,我添加了另一个具有相同内容的 -card-container 并且我的代码可以正常工作。你能详细说明发生了什么问题吗?
  • 我需要设置类点击更多的卡片容器。不要一张卡,两张或更多
【解决方案2】:

将过渡移动到悬停状态并调整延迟,您将拥有完全相同的东西:

div.card {
  height: 41.4vmin;
  width: 30vmin;
  margin: 0.7vmin;
  border-radius: 1.3vmin;
  background-image: url("https://sun9-8.userapi.com/c840530/v840530203/60933/tkZK7aYQUjM.jpg");
  background-size: contain;
}

div.card:hover {
  transform: rotateY(160deg);
  background-image: url("https://sun9-8.userapi.com/c840530/v840530203/6092c/fR8eCsT009k.jpg");
  transition:   transform 1s linear, background 0s 0.5s linear;
}
&lt;div class="card"&gt;&lt;/div&gt;

【讨论】:

    【解决方案3】:

    为什么需要 100 个 URL 才能做到这一点?您只需要卡片的正面和卡片的背面。

    img {
          width:100%;
          border:1px solid #e0e0e0;
          border-radius:1vw;
        }
    
        .container {
          /* Position and Size the Container */
          position: absolute;
          top: 17%;
          bottom:0; /* Needed to keep the overall height larger than the card. */
          left: 20%;
          width: 25%;
     
          /* Will inherit to children only (not all descendants) */
          perspective: 900px; 
        }
    
        #card .front {
          /* Flip the front side of the card by rotating it around the y-axis. */
          transform: rotateY(180deg);
        }
    
        #card:hover {
          /* Rotate the card as a whole:  */
          transform: rotateY(180deg);
        }
    
        #card div {
          /* Forces both elements to come out of the normal flow and occupy the same space on the page. */
          position: absolute;
    
          /*
            The backface-visibility CSS property determines whether or not the back
            face of the element is visible when facing the user. The back face of 
            an element is always a transparent background, letting, when visible, 
            a mirror image of the front face be displayed. If your foreground element
            is opaque, this property may not need to be set.
          */
          backface-visibility: hidden;
        }
    
        #card {
          /*
            Indicates that the children of the element should be positioned in the 3D-space.
            And, pass any inherited perspective along to children.
          */
          transform-style: preserve-3d;
    
          /* 
            Changes to the transform property should take 1 second to
            change from their current value to their new value. 
          */
          transition: transform 1s cubic-bezier(.75,1.25,.5,1.25);
        }
    <!-- The "container" will be the 3D space for us to work in.   -->
      <div class="container">
    
        <!-- The "card" is the single entity that we will manipulate. -->
        <div id="card">   
    
          <!-- The child elements make up pieces of the overall object.--> 
          <div class="front"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Ace_of_spades.svg/2000px-Ace_of_spades.svg.png"></div>
          <div class="back"><img src="https://thumb1.shutterstock.com/display_pic_with_logo/3172829/620603528/stock-vector-ace-of-spades-with-eyel-vintage-design-t-shirts-620603528.jpg"></div>
        </div>
    
      </div>

    【讨论】:

    • 这很棒。我可能会建议将:hover 事件向上移动到父元素,这样#card 上的旋转就不会在过渡期间在卡片上移动光标时突然上升。
    • 你应该注意闪烁,检查这个stackoverflow.com/questions/49078460/…
    • @JonUleis 如果将鼠标悬停移动到.container,效果将不再起作用。
    • @TemaniAfif 这在这里不适用。您在这里看到的只是卡片旋转时,取决于您的鼠标所在的位置,它可能最终不再位于卡片上方,因此悬停不再适用并且效果恢复。您的链接答案完全描述了其他内容。
    • 是的,我知道这是完全不同的事情;)但我正在谈论旋转......最好将悬停应用在固定的高度/宽度元素上并旋转孩子以避免在 90 度时的状态并且鼠标失去悬停..这是我在这个答案中解释的一部分
    【解决方案4】:

    您需要使用带有回文计时的transition-timing-function,因此卡片图像转换始终可以在中间进行。这方面的示例包括 linearease-in-out(合并在下面)。

    请注意,尽管某些浏览器不支持 background-image 上的 transition,因此您可能需要以不同的方式处理此效果才能使其在 IE 中运行。

    div {
      height: 41.4vmin;
      width: 30vmin;
      margin: 0.7vmin;
      border-radius: 1.3vmin;
      background-image: url("https://sun9-8.userapi.com/c840530/v840530203/60933/tkZK7aYQUjM.jpg");
      background-size: contain;
      transition: transform 1s ease-in-out, background 0s .5s;
    }
    
    div:hover {
      transform: rotateY(160deg);
      background-image: url("https://sun9-8.userapi.com/c840530/v840530203/6092c/fR8eCsT009k.jpg");
    }
    &lt;div&gt;&lt;/div&gt;

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2014-09-10
      • 2022-01-13
      • 2013-07-17
      • 2017-08-20
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 2014-02-04
      相关资源
      最近更新 更多