【问题标题】:CSS Translate and Scale on Y-Axis?CSS 在 Y 轴上平移和缩放?
【发布时间】:2017-11-15 12:51:54
【问题描述】:

我正在参加学习 CSS 的在线课程,而我们只是介绍 CSS 动画。我正在尝试通过创建一个男人沿着小路走向屏幕的小动画来练习我学到的一些东西(现在只是基本的变换)。

基本上,我想同时翻译和缩放我的图像。我得到了这个工作正常,但现在我还想添加一些小的旋转,这样看起来这个人正在轻微地左右移动。这是我在 jsfiddle 中的代码,我不知道如何更改变换原点,以便该人在 Y 轴上沿直线行走,比例尺使他沿对角线行走。我希望这是有道理的......

代码中被注释掉的部分包括比例,一旦添加回来,没有比例的部分被注释掉,这很有趣,我认为这与原点有关?

https://jsfiddle.net/qLLqdxbm/

HTML:

<div class="man-scale">
  <img class="man-walk" src="http://clipart-library.com/img/1184697.png">
</div>

CSS:

.man-walk {
    width: 100px;
    height: 125px;
    position: absolute;
    top: 0;
    left: 50px;

    animation-name: man-walk;
    animation-duration: 0.45s;
    animation-iteration-count: infinite;
}

@keyframes man-walk {
    0% {
        transform: rotate(0deg);
    }

    25% {
        transform: rotate(1.5deg);
    }

    50% {
        transform: rotate(0deg);
    }

    75% {
        transform: rotate(-1.5deg);
    }

    100% {
        transform: rotate(0deg);
    }
}

.man-scale {
    width: 100px;
    height: 125px;

    animation-name: man-scale;
    animation-duration: 2s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}

/* define the animation */
@keyframes man-scale {
/*     0% {
        transform: translate(0px, 5px) scale(1.1);
    }

    25% {
        transform: translate(0px, 15px) scale(1.5);
    }

    50% {
        transform: translate(0px, 25px) scale(1.7);
    }

    75% {
        transform: translate(0px, 35px) scale(2.0);
    }

    100% {
        transform: translate(0px, 45px) scale(2.3);
    } */

    0% {
        transform: translate(0px, 5px);
    }

    25% {
        transform: translate(0px, 15px);
    }

    50% {
        transform: translate(0px, 25px);
    }

    75% {
        transform: translate(0px, 35px);
    }

    100% {
        transform: translate(0px, 45px);
    }
}

感谢您的帮助!

【问题讨论】:

  • 我正在使用旋转、缩放和平移...随着图像越来越近,正在使用缩放来放大图像,我不确定您的意思

标签: html css css-animations


【解决方案1】:

每次沿 X 和 Y 缩放图像时,原点都会在两个维度上移动一个特定的偏移量。如果您可以补偿 X 维度中的偏移,则可以实现垂直动画。

在这种情况下,在第一个关键帧中,比例增加了 0.1,即 100 * 0.1 = 10px 现在原点在 X 维度上偏移了 5px,以 translateX(-5px) 进行补偿。对于所有其他关键帧也是如此。

如果您想在 Y 维度上获得更快的动画,只需增加 Y 平移值而不触及 X 平移值。

.man-walk {
    width: 100px;
    height: 125px;
    position: absolute;
    top: 0;
    left: 50px;

    animation-name: man-walk;
    animation-duration: 0.45s;
    animation-iteration-count: infinite;
}

@keyframes man-walk {
    0% {
        transform: rotate(0deg);
    }

    25% {
        transform: rotate(1.5deg);
    }

    50% {
        transform: rotate(0deg);
    }

    75% {
        transform: rotate(-1.5deg);
    }

    100% {
        transform: rotate(0deg);
    }
}

.man-scale {
    width: 100px;
    height: 125px;

    animation-name: man-scale;
    animation-duration: 2s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}

/* define the animation */
@keyframes man-scale {
     0% {
        transform: translate(-5px, 30px) scale(1.1);
    }

    25% {
        transform: translate(-20px, 70px) scale(1.4);
    }

    50% {
        transform: translate(-35px, 120px) scale(1.7);
    }

    75% {
        transform: translate(-50px, 180px) scale(2.0);
    }

    100% {
        transform: translate(-65px, 250px) scale(2.3);
    } 
}
<div class="man-scale">
  <img class="man-walk" src="http://clipart-library.com/img/1184697.png">
</div>

可能有一些高级 CSS 技术可以自动计算偏移量。

【讨论】:

  • 谢谢!这正是我想要的,你的解释帮助我理解发生了什么。还有一个问题,我如何控制动画在 y 轴上的移动距离?调整 translate() 中的第二个值似乎没有太大效果。
  • 我以 40、50、60、70、80 的增量增加了它。您可以进一步增加它们以查看效果。
  • 很抱歉再次打扰您,但是如果您在页面首次加载时更改左上角坐标,您上面提到的等式会如何受到影响?我试着玩弄这些,现在翻译 + 比例再次出现在对角线上。例如,如果您将顶部更改为 315 像素,将左侧更改为 415 像素。
  • 不要更改图片上的topleft。制作父 div absolute,然后设置您想要的 topleft。它变成对角线的原因是因为中心移动了,而不是之前的50px = 100px/2
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
相关资源
最近更新 更多