简体   繁体   English

试图找出CSS动画

[英]Trying To Figure Out CSS Animations

I've been trying to animate a headline element in CSS so that when hovered it rotates the text forward. 我一直试图在CSS中为标题元素设置动画,以便在悬停时将文本向前旋转。 I've read through loads of tutorials and managed to get it rotating clockwise (or anticlockwise) but not how I want (forward, as if it's spinning towards me). 我已经阅读了大量的教程,并设法让它顺时针(或逆时针)旋转,但不是我想要的(向前,就像它向我旋转)。

A great example of the effect I am after is displayed at http://disruptltd.com/ when you hover the navigation links. 当您悬停导航链接时, http://disruptltd.com/会显示我所效果的一个很好的例子。

If anyone could tell me if that's possible using CSS or any information on how to achieve that effect I'd be massively appreciative 如果有人能告诉我是否可以使用CSS或任何有关如何实现这种效果的信息,我会非常感激

Here is the closest I've come so far, using this code : 这是我到目前为止最接近的,使用此代码:

.navigation h1:hover {
  display: inline-block;
  -moz-animation: spin-reverse 2s infinite linear;
  -o-animation: spin-reverse 2s infinite linear;
  -webkit-animation: spin-reverse 2s infinite linear;
  animation: spin-reverse 2s infinite linear;
}

@-moz-keyframes spin-reverse {
  0% { -moz-transform: rotate(0deg); }
  100% { -moz-transform: rotate(-359deg); }
}
@-webkit-keyframes spin-reverse {
  0% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(-359deg); }
}
@-o-keyframes spin-reverse {
  0% { -o-transform: rotate(0deg); }
  100% { -o-transform: rotate(-359deg); }
}
@-ms-keyframes spin-reverse {
  0% { -ms-transform: rotate(0deg); }
  100% { -ms-transform: rotate(-359deg); }
}
@keyframes spin-reverse {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(-359deg); }
} 

I've also made a jfiddle : http://jsfiddle.net/qzxbd5fz/ 我也做了一个jfiddle: http//jsfiddle.net/qzxbd5fz/

So basically I want the effect to look like it's spinning towards me, similar to a slot machine as opposed to this clock hand spinning style effect. 所以基本上我希望效果看起来像是旋转着我,类似于老虎机,而不是这种时钟旋转式的效果。

Thanks in advance 提前致谢

Try this 试试这个 http://jsfiddle.net/qzxbd5fz/1/ http://jsfiddle.net/qzxbd5fz/1/

This is what I've added 这就是我添加的内容

h1{
    -webkit-transition:font-size 3s ;
}
h1:hover {
    display: inline-block;
    font-size:50px;
}

Here is a rather simple example I have deviced, by slightly modifying your markup. 这是一个相当简单的例子,我稍微修改了你的标记。 As the slot machine spinning effect requires depth, it means that we need some kind of wrapping so that we can declare a perspective value to the parent element: 由于老虎机旋转效果需要深度,这意味着我们需要某种包装,以便我们可以向父元素声明perspective值:

<h1><span>SITE LOGO</span></h1>

The slot machine effect involves the following: 老虎机效果涉及以下内容:

  1. Giving depth to the element, so that when it's facing backwards (away from you), it is further away from the screen/viewer 赋予元素深度,以便当它面向后方(远离你)时,它远离屏幕/查看器
  2. Using scale the resize the element, since we are going to translate the element on the Z axis to give the depth effect 使用scale来调整元素的大小,因为我们要在Z轴上转换元素以给出深度效果

The code is as follow, and a sample fiddle can be seen here: http://jsfiddle.net/teddyrised/qzxbd5fz/2/ . 代码如下,这里可以看到一个示例小提琴: http//jsfiddle.net/teddyrised/qzxbd5fz/2/ To better demonstrate the effect I have opted to use CSS transition, but you can always convert it back to animation. 为了更好地演示我选择使用CSS转换的效果,但您始终可以将其转换回动画。

h1 {
    background: #eee;
    padding: 30px;
    perspective: 100px;
}
h1 span {
    display: block;
    text-align: center;
    transition: all 1s ease-in-out;
    -webkit-transform: rotateX(0) translateZ(50px) scale(0.5);
}
h1:hover span {
    -webkit-transform: rotateX(-360deg) translateZ(50px) scale(0.5);
}

More importantly, you can use backface-visibility: hidden on the <span> element to prevent the text from showing through when rotated -180deg away from the viewer along the X axis. 更重要的是,您可以使用backface-visibility: hidden<span>元素上,以防止文本在沿X轴旋转-180deg时显示。


A simpler solution 更简单的解决方案

After seeing that OP has clarified what kind of effect he wants to see, perhaps a simple translation trick with pseudo-elements will do fine. 在看到OP澄清了他想要看到什么样的效果之后,也许使用伪元素的简单翻译技巧就可以了。

The thing is that in order to have the effect as mentioned on the sample page linked, you will need to duplicate the site logo text in one way or another — I don't recommend using HTML to do so, because it has no semantic meaning (imagine a search engine reading two "Home" links). 问题是,为了获得链接的示例页面上提到的效果,您需要以某种方式复制站点徽标文本 - 我不建议使用HTML这样做,因为它没有语义含义(想象一下搜索引擎阅读两个“主页”链接)。 Instead, you can use either pseudo-elements (which require updating when you change the logo titles) or JS to duplicate the text. 相反,您可以使用伪元素(更改徽标标题时需要更新)或JS复制文本。 Here I have decided to use pseudo-elements for a pure CSS approach. 在这里,我决定使用伪元素进行纯CSS方法。

View the demo here: http://jsfiddle.net/teddyrised/qzxbd5fz/3/ 在这里查看演示: http//jsfiddle.net/teddyrised/qzxbd5fz/3/

Again, use the following markup: 再次,使用以下标记:

<h1><span>SITE LOGO</span></h1>

For your CSS: 对于你的CSS:

h1 {
    background: #000;
    font-family: Arial;
    overflow: hidden;
}
h1 span {
    color: #fff;
    transition: all .25s ease-in-out;
}
h1 span, h1 span::after {
    display: inline-block;
    position: relative;
}
h1 span::after {
    content: 'SITE LOGO';
    color: #1ddfb3;
    -webkit-transform: translate3D(-100%, 100%, 0);
}
h1:hover span {
    -webkit-transform: translateY(-100%);
}

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

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