简体   繁体   English

CSS 旋转、最终帧和内联块

[英]CSS rotation, final frame and inline-block

I've recently been experimenting with CSS animations and have come across some behaviour I can't explain with regards to final frame state.我最近一直在试验 CSS 动画,并且遇到了一些我无法解释的关于最终帧状态的行为。

Given this very small piece of HTML:鉴于这个非常小的 HTML 片段:

<span id="rotateme">This is text</span>

Some CSS:一些CSS:

#rotateme { display: inline-block; }
.clockwise { 
  animation: clockwise 1s;
  animation-fill-mode: forwards;
}
.anticlockwise {
  animation: anticlockwise 1s;
  animation-fill-mode: forwards;
}
@keyframes anticlockwise {
  from { transform: rotate(0deg); }  
  to { transform: rotate(-90deg); }
}
@keyframes clockwise {
  from { transform: rotate(-90deg); }  
  to { transform: rotate(0deg); }
}

And a little bit of Javascript to tie it together:还有一点点 Javascript 将它们联系在一起:

document.addEventListener("DOMContentLoaded", function(event) { 
  d3.select('#rotateme')
    .on('click', rotateAnticlockwise)

  function rotateClockwise() {
    d3
      .select(this)
        .classed('clockwise', true)
        .classed('anticlockwise', false)
        .on('click', rotateAnticlockwise)
  }

  function rotateAnticlockwise() {
    d3
      .select(this)
        .classed('clockwise', false)
        .classed('anticlockwise', true)
        .on('click', rotateClockwise)
  }
});

(For a live example, this is also in a codepen ) (对于一个活生生的例子,这也在一个代码笔中

If you click on the text it'll rotate, click on it again and it'll rotate back.如果你点击它会旋转的文本,再次点击它,它会旋转回来。 However, if you remove the display style from rotateme element then the final frame of the animation isn't preserved.但是,如果您从rotateme元素中删除display样式,则不会保留动画的最后一帧。 For the clockwise motion this means it snaps back to the original, horizontal position, and the anticlockwise motion starts from the wrong place.对于顺时针运动,这意味着它会弹回到原来的水平位置,而逆时针运动则从错误的位置开始。

My question is, what is that inline-block is doing in this situation that makes the animation work as I expect it to.我的问题是,在这种情况下, inline-block在做什么,使动画按我的预期工作。 ie Stay in what I understand to be the forward fill mode.即保持我所理解的前向填充模式。

I should add that I'm doing this in Chrome 43 just in case it's a browser quirk.我应该补充一点,我是在 Chrome 43 中这样做的,以防万一它是浏览器的怪癖。

Span 元素默认是内联的,因此在尺寸、位置等方面有限制。通过从样式显示中删除inline-block ,您允许它恢复为内联,从而去除尺寸和位置。

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

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