简体   繁体   English

将绝对左侧与 animation 中的最大宽度匹配

[英]Match absolute left with max-width in animation

I'm making a worm, the worm moves from position A to B. The goal is to have the "body" of the worm stretch up to position B, and the "tail" gets contracted after it reaches B.我正在制作一条蠕虫,蠕虫从 position A 移动到 B。目标是让蠕虫的“身体”伸展到 position B,“尾巴”在到达 B 后收缩。

Currently, I'm expanding the max-width of the worm and then moving the absolute left position to the exact amount I expanded the worm.目前,我正在扩展蠕虫的最大宽度,然后将绝对左侧 position 移动到我扩展蠕虫的确切数量。 So, if the worm expanded 20vw, the absolute left of the whole worm after is contracted becomes 20vw.所以,如果蠕虫膨胀20vw,那么整个蠕虫收缩后的绝对左侧就变成了20vw。

I'm having difficulties trying to figure out how to 'hold' position B while contracting the tail of the worm.我很难弄清楚如何在收缩蠕虫尾巴的同时“握住” position B。 I'm able to expand and contract the body at the same time, but I want to separate these two movements.我可以同时伸展和收缩身体,但我想将这两个动作分开。 Only after the head reaches position B, the tail gets contracted.只有在头部到达 position B 后,尾部才会收缩。

Code: http://jsfiddle.net/gamealchemist/U2tPJ/79/代码: http://jsfiddle.net/gamealchemist/U2tPJ/79/

<div class='worm'>
              <div class='worm-eye'>
                <div class='worm-lid' />
                <div class='worm-iris'>
                  <div class='worm-iris-container'>
                    <div class='worm-iris-glow' />
                  </div>
                </div>
              </div>
            </div>
.worm {
  position: absolute;
  top: 30px;
  left: 50px;
  width: 30vw;
  height: 30vw;
  max-width: 100px;
  max-height: 100px;
  background-color: aquamarine;
  border-radius: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  animation: move-worm 3s ease-in-out;
  animation-fill-mode: forwards;
}

@keyframes move-worm {
  0% {max-width: 100px; }
  50% { max-width: calc(20vw + 100px); }
  100% {max-width: 100px; left: calc(20vw); }
}

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

The left and right CSS properties are animatable so we can use them to initially fix the left and move the right and then fix the right and move the left rather than animate the max-width.左右 CSS 属性是可动画的,因此我们可以使用它们来初始固定左侧并移动右侧,然后固定右侧并移动左侧,而不是为最大宽度设置动画。

 .worm { position: absolute; top: 30px; left: 50px; right: calc(50px + 100px); height: 100px; background-color: aquamarine; border-radius: 50px; display: flex; justify-content: center; align-items: center; animation: move-worm 3s ease-in-out; animation-fill-mode: forwards; } @keyframes move-worm { 0% { left: 50px; right: calc(100vw - 50px - 100px); } 50% { left: 50px; right: calc(100vw - 50px - 100px - 20vw); } 100% { left: calc(20vw + 50px); right: calc(100vw - 50px - 100px - 20vw) } }
 <div class='worm'> <div class='worm-eye'> <div class='worm-lid' /> <div class='worm-iris'> <div class='worm-iris-container'> <div class='worm-iris-glow' /> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM