【问题标题】:Can I create a butterfly animation effect with translate3D (css), or another css animation on a single image or element?我可以使用 translate3D (css) 或在单个图像或元素上创建另一个 css 动画来创建蝴蝶动画效果吗?
【发布时间】:2020-12-29 03:42:04
【问题描述】:

我有一张蝴蝶的图片,类似于this

我试图弄清楚是否有任何方法可以让它看起来像它的翅膀通过 3D CSS 变换/翻译或动画打开和关闭,但不必将图像分成几部分(它可以是一个 div 的背景图片,如果有帮助的话)。

【问题讨论】:

    标签: css css-animations css-transitions css-transforms


    【解决方案1】:

    是的,将背景应用于两个元素,每个元素只显示一半,然后您只需在 Y 轴上旋转两个元素。

    .box {
      width:300px;
      margin:20px;
      display:flex;
      perspective:500px;
    }
    .box::before,
    .box::after{
      content:"";
      padding-top:56%; /* ratio based on your image */
      flex:1; /* half the main element size */
      background-image:url(https://i.imgur.com/DgMoHC5.jpg);
      background-size:200% 100%; /* twice bigger than the pseudo element to get half the image*/
      animation:left 1s linear infinite alternate;  
      transform-origin:right;
    }
    .box::after {
      background-position:right; /* get the right part of the image */
      animation-name:right;
      transform-origin:left;
    }
    
    @keyframes left{
      to {transform:rotateY(60deg)}
    }
    @keyframes right{
      to {transform:rotateY(-60deg)}
    }
    <div class="box"></div>

    带有一些翻译的更逼真的动画:

    .box {
      width: 300px;
      margin: 20px;
      display: flex;
      perspective: 500px;
    }
    
    .box::before,
    .box::after {
      content: "";
      padding-top: 56%;
      flex: 1;
      background-image: url(https://i.imgur.com/DgMoHC5.jpg);
      background-size: 200% 100%;
      animation: left 0.5s linear infinite alternate;
      transform-origin: right;
    }
    
    .box::after {
      background-position: right;
      animation-name: right;
      transform-origin: left;
    }
    
    @keyframes left {
      from {
        transform: translateZ(80px) rotateY(-30deg)
      }
      to {
        transform:translateZ(0px) rotateY(50deg) 
      }
    }
    
    @keyframes right {
      from { 
        transform: translateZ(80px) rotateY(30deg)
      }
      to {
        transform:translateZ(0px) rotateY(-50deg) 
      }
    }
    <div class="box"></div>

    【讨论】:

      猜你喜欢
      • 2021-04-22
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 2013-09-13
      相关资源
      最近更新 更多