简体   繁体   English

CSS3过渡:移动时稍微倾斜/旋转,然后恢复为水平

[英]CSS3 transition: tilt/rotate slightly while moving, then restore to horizontal

Is it possible, using CSS transitions, to tilt (rotate) an element slightly off-horizontal during the first half of its movement--and tilt it back to horizontal during the second half, as it reaches the end of its movement? 使用CSS过渡,是否有可能在元素移动的上半部分使其稍微偏离水平方向倾斜(旋转),并在元素到达其运动结束时在后半部分中使其倾斜回到水平状态? I don't want a 360-degree spin. 我不要360度旋转。 Just a slight tilt, then tilt back again. 稍微倾斜一下,然后再次向后倾斜。

Here's a picture of the beginning, middle, and end of the transition I have in mind: 这是我想到的过渡的开始,中间和结束的图片:

运动中倾斜

This question is best demonstrated by watching it. 通过观看可以很好地证明这个问题。 Here's a fiddle that shows what I would like to achieve--but I'd like to achieve it with CSS transitions, not JavaScript: 这是一个小提琴,显示了我想要实现的目标,但是我想通过CSS转换而不是JavaScript来实现:

http://jsfiddle.net/bmorearty/S5Us6/22/ http://jsfiddle.net/bmorearty/S5Us6/22/

When you run this, watch the gray box closely. 运行此程序时,请密切注意灰色框。 It tilts a bit during motion, then reverses its tilt halfway through--so when it comes to rest, it is no longer tilted. 它在运动过程中会稍微倾斜,然后在中途反转其倾斜度-因此当它静止时,它不再倾斜。

I would like whole motion this to happen in a single transition from one state to another simply by adding a class to an element--not in two transitions, because that would require me to time the end of one with the beginning of the next. 我希望整个动作是在一个状态从一个状态到另一个状态的单个转换中发生的,只需将一个类添加到元素中即可,而不是在两个转换中进行,因为这将需要我将一个状态的结束与下一个状态的开始计时。

I suspect the answer would incorporate transition-delay and/or @keyframes . 我怀疑答案会包含transition-delay和/或@keyframes

Thanks. 谢谢。

You could do something like this. 你可以做这样的事情。 http://cdpn.io/sIxFA http://cdpn.io/sIxFA

Obviously, you could smooth out the rotation as you please, and add the rotate class on click. 显然,您可以根据需要平滑旋转,并在单击时添加旋转类。

this would be the css for it: 这将是它的CSS:

#card {
    padding: 2em;
    border: 1px solid gray;
    display: inline-block;
    position: relative;
    background-color: #eee;
    /*instead of infinite you can add a number of times you want it running*/
    animation: moving infinite 6s;

}
@keyframes moving {
    0%{
      margin: 0;
    }
  50% {
 -webkit-transform: rotate(45deg);
  }
  100% {
    margin: 50px;
    }
}

I got it! 我知道了!

The solution is to use a CSS animation with a keyframe that transforms the rotation (eg, to about 8deg) when it is 50% of the way through, but returns the rotation to 0deg at the end. 解决方案是使用带有关键帧的CSS动画,当关键帧经过50%时将其旋转(例如,旋转到大约8度),但最后将旋转返回到0度。 It's pretty sweet. 很甜

Here's a demo on JSBin: 这是有关JSBin的演示:

http://jsbin.com/ogiqad/4/edit http://jsbin.com/ogiqad/4/edit

(The code below uses -webkit but you can add all the other browser variations to make it work on more browsers.) (以下代码使用-webkit,但您可以添加所有其他浏览器变体以使其在更多浏览器上运行。)

@-webkit-keyframes tilt-and-move {
  0% {
    left: 0;
    top: 0;
  }
  50% {
    -webkit-transform: rotate(8deg);
  }
  100% {
    left: 100px;
    top: 100px;
  }
}

#card {
    position: relative;
}

#card.moved {
    left: 100px;
    top: 100px;
    -webkit-animation-duration: 0.4s;
    -webkit-animation-name: tilt-and-move;
}

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

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