简体   繁体   English

动画前进后过渡不起作用

[英]Transition doesn't work after animation forwards

Hie! ie!

Transition doesn't work after animation with animation-fill-mode: forwards; 在具有animation-fill-mode的动画之后,过渡不起作用。 First animation fires and ends on hover, but then after unhovering transition doesn't apply. 第一个动画在悬停时触发并结束,但是在悬停过渡之后不适用。

Codepen here Codepen 在这里

It reproduces in chrome and safari. 它以chrome和safari复制。 In firefox works fine. 在Firefox中工作正常。

pug: 哈巴狗:

button Button

scss: scss:

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

button {
  padding: 5px 0;
  position: relative;
  border: none;
  border-radius: 0;
  background: transparent;
  outline: none;
  cursor: pointer;
  font-weight: 700;
  text-transform: uppercase;

  &:before {
    content: '';
    position: absolute;
    bottom: 0;
    left: 100%;
    right: 0;
    height: 4px;
    border-radius: 2px;
    background: blue;
    transition: all .25s;
  }

  &:hover {

    &:before {
      animation: nav-link-hover .25s forwards;
    }
  }
}

@keyframes nav-link-hover {

    0% {
        left: 0;
        right: 100%;
    }

    100% {
        left: 0;
        right: 0;
    }
}

UPDATE 更新
I've found workaround but will wait for any answer and gladly accept it if it will be better or for example successfully uses broken keyframes. 我已经找到了解决方法,但是会等待任何答案,如果它更好或者例如成功使用损坏的关键帧,则乐意接受它。

Still don't know what's wrong with keyframes in chrome/safari, but have found simple workaround. 仍然不知道chrome / safari中的关键帧有什么问题,但是找到了简单的解决方法。

Now first 'before'-element is on right with no width right: 0; width: 0; 现在第一个'before'元素在右边,没有宽度,在right: 0; width: 0; right: 0; width: 0; .
On hover it moves to left with 100% width, that begins to transitionally grow: left: 0; right: auto; width: 100%; 悬停时,它将以100%的宽度向左移动,并开始逐渐增长: left: 0; right: auto; width: 100%; left: 0; right: auto; width: 100%; .
Finally, when hover was lost, 'before' starts to decreasing, resting on the right end again. 最后,当悬停丢失时,“ before”开始减小,再次位于右端。

New code: 新代码:

button {
  padding: 5px 0;
  position: relative;
  border: none;
  border-radius: 0;
  background: transparent;
  outline: none;
  cursor: pointer;
  font-weight: 700;
  text-transform: uppercase;

  &:before {
    content: '';
    position: absolute;
    bottom: 0;
    right: 0;
    width: 0;
    height: 4px;
    border-radius: 2px;
    background: blue;
    transition: all .25s;
  }

  &:hover {

    &:before {
      left: 0;
      right: auto;
      width: 100%;
    }
  }
}

Codepen 码笔

Animating elements require a defined state before the animation begins. 动画元素需要在动画开始之前定义状态。 If you define a state on a trigger (ie. hover), some unexpected behaviour may occur. 如果在触发器上定义状态(即悬停),则可能会发生某些意外行为。 Therefore, you should set your starting point before the hover. 因此,您应该在悬停之前设置起点。

In this case, simply move left: 0; 在这种情况下,只需left: 0;移动left: 0; from the hover to the "non-hover" properties of &:before 从悬停到&:before的“非悬停”属性

button {
  padding: 5px 0;
  position: relative;
  border: none;
  border-radius: 0;
  background: transparent;
  outline: none;
  cursor: pointer;
  font-weight: 700;
  text-transform: uppercase;

  &:before {
    content: '';
    position: absolute;
    left: 0;
    bottom: 0;
    right: 0;
    width: 0;
    height: 4px;
    border-radius: 2px;
    background: blue;
    transition: all .25s;
  }

  &:hover {

    &:before {
      right: auto;
      width: 100%;
    }
  }
}

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

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