简体   繁体   English

CSS 3D动画,怎么样?

[英]CSS 3D animation, how?

I would like to animate things in 3D space. 我想在3D空间中制作动画。 I know this is possible with CSS and HTML5, but I can't find a good tutorial for practical use! 我知道这可以用CSS和HTML5,但我找不到一个实用的好教程!

I found this website as an example. 我发现这个网站就是一个例子。 You can select text etc. All the time. 您可以随时选择文本等。 Can somebody give a very easy to understand and little example how this works? 有人可以给出一个非常容易理解的小例子吗? I see the source code but I don't really understand it… 我看到源代码,但我真的不明白它...

Is this CSS3 or HTML5 or both? 这是CSS3还是HTML5或两者兼而有之?

How much JavaScript do I need? 我需要多少JavaScript?

Which browsers support this? 哪些浏览器支持这个?

There are two parts to this. 这有两个部分。

  1. The first one is about building the 3D shape. 第一个是关于构建 3D形状。
  2. The second one involves animating it (and this is the simple part). 第二个涉及动画 (这是简单的部分)。

UPDATE AUGUST 2019 更新2019年8月

This is a really old answer and I'll leave the meat of the original below, just removing the links that don't work anymore. 这是一个非常古老的答案,我将留下原始的肉,只是删除不再工作的链接。

But since I wrote this, both my coding abilities and browsers have progressed, so if you want to build a CSS cube in a more efficient fashion, check out this article I wrote on the topic: 但是自从我写这篇文章以来,我的编码能力和浏览器都取得了进步,所以如果你想以更有效的方式构建一个CSS多维数据集,请查看我在这个主题上写的这篇文章:

Simplifying CSS Cubes with Custom Properties 使用自定义属性简化CSS多维数据集


I'll go through each one of them in detail, but first I'll briefly answer the three questions you've asked at the end. 我将详细介绍其中的每一个,但首先我将简要回答你最后提出的三个问题。

is this css3 or html5 or both? 这是css3还是html5还是两者兼而有之?

It's CSS3. 这是CSS3。 CSS 3D transforms and keyframe animations . CSS 3D变换关键帧动画

how much javascript do I need? 我需要多少JavaScript?

Well, if you don't want to create 3D shape itself with JavaScript, then you don't need any JavaScript to animate it. 好吧,如果您不想使用JavaScript创建3D形状,那么您不需要任何JavaScript来为其设置动画。 Any browser that supports 3D transforms also supports keyframe animations and, if a browser supports keyframe animations, then you probably want to use those instead of jQuery (and maybe even your own custom JS) animations . 任何支持3D变换的浏览器也支持关键帧动画如果浏览器支持关键帧动画,那么你可能想要使用那些而不是jQuery(甚至可能是你自己的自定义JS)动画

which browsers support this? 哪些浏览器支持这个?

Things didn't look good two years ago, but they're getting better... 两年前事情看起来不太好,但是他们变得越来越好......

✓ Chrome , Safari , Firefox (though Firefox 3D animations are jiggly and not nice to my CPU... I do have a 6-year-old old laptop, it's true) Opera 15+ (WebKit) support 3D transforms. ✓浏览器 ,Safari浏览器火狐 (Firefox中虽然3D动画是不平稳的,而不是很高兴我的CPU ......我有一个6岁的旧笔记本电脑,这是真的) 歌剧15+(WebKit的)支持3D变换。 And of course keyframe animations. 当然还有关键帧动画。

Firefox supports everything unprefixed, Chrome/Safari/Opera need the -webkit- prefix - update : Chrome 36+ and Opera 23+ support them unprefixed as well, so it's only Safari left now. Firefox支持所有未加前缀的内容,Chrome / Safari / Opera需要-webkit-前缀 - 更新 :Chrome 36+和Opera 23+也支持它们,因此现在只剩下Safari了。 I won't be using any prefixes in the answer or in the demo ( -prefix-free takes care of things there). 我不会在答案或演示中使用任何前缀( -prefix-free处理那里的事情)。 Another update : Safari 9 unprefixes transforms. 另一个更新 :Safari 9前缀变换。

3D transforms were not supported by Opera before switching to WebKit. ✗3D转换不是由歌剧切换到之前的WebKit支持。

✗ IE up to and including 9 don't support 3D transforms. ✗IE9(包括9)不支持3D变换。 IE10 and IE11 support 3D transforms, but do not support nesting of 3D transformed elements (cannot create realistic looking 3D shapes via applying 3D transforms on both parent and child elements, as 3D transformed children of a 3D transformed parent get flattened into the plane of the parent). IE10IE11支持3D变换,但不支持3D变换元素的嵌套 (无法通过在父元素和子元素上应用3D变换来创建逼真的3D形状,因为3D变换后的父元素的3D变换后的子元素会变平。父母)。 Update : Edge now supports nesting of 3D transformed elements. 更新 :Edge现在支持嵌入3D转换元素。


Alright, I hope that clears a few things. 好吧,我希望清除一些东西。 Now... 现在...

1 Building a CSS cube. 1构建CSS多维数据集。

1.2 What is a cube and starting off with the HTML 1.2什么是立方体,从HTML开始

First of all, try to picture a cube. 首先,尝试画一个立方体。 Maybe this link helps? 也许这个链接有帮助? Or let's have an image here as well. 或者我们这里也有一张图片。

立方体

It has 6 square faces. 它有6个方面。 1 top, 1 bottom; 1个顶部,1个底部; 1 in front, 1 in the back; 前面1个,后面1个; 1 left, 1 right. 1左,1右。 This means that the HTML is simply: 这意味着HTML只是:

<ul class='cube'>
    <li class='face'></li>
    <li class='face'></li>
    <li class='face'></li>
    <li class='face'></li>
    <li class='face'></li>
    <li class='face'></li>
</ul>

You first put some regularity into the faces, give them equal width and height , position them absolutely so that they're all stacked one on top of the other, give them different backgrounds, they don't mind that. 你首先在脸上加上一些规律性,给它们相同的 widthheight ,绝对定位它们,使它们全部叠在一起,给它们不同的背景,他们不介意。 You can give them some padding, put some dummy text into them, whatever... 你可以给它们一些填充,把一些虚拟文本放进去,不管......

Now comes the interesting part: moving them so that they form a cube in space. 现在出现了一个有趣的部分:移动它们以便它们在空间中形成立方体。 You do that using 3D transforms. 你可以使用3D变换来做到这一点。

1.2 The Coordinate System 1.2协调系统

Consider this 3D coordinate system: 考虑这个3D坐标系:

三维坐标系

Initially, all the 6 faces are all exactly where you see the blue square, in the xOy plane (where O is the intersection of the 3 axes). 最初,所有6个面都是在xOy平面中看到蓝色方块的所有位置(其中O是3个轴的交点)。

Pay attention to the direction of the y-axis . 注意y-axis Coming from a mathematical background, this seemed a bit weird to me at first, but this is how the coordinate system is for the screen. 来自数学背景,这对我来说似乎有点奇怪,但这就是坐标系对于屏幕的看法。

In 2D, the origin O is at the top left corner, so the + (positive direction) of the x-axis points right and the + of the y-axis points down. 在2D中,原点O位于左上角,因此x-axis+ (正方向)指向右,而y-axis+指向下。

1.2.a Translations along the axes 1.2.a沿轴的翻译

So a translation of a positive value along the x-axis (for example, translateX(10px) ) moves the element to which it is applied to the right (towards the + of the x-axis ), while a translation of a negative value along the x-axis (something like translateX(-10px) ) moves it to the left. 因此,沿x-axis的正值的translateX(10px)例如, translateX(10px) )将其应用的元素向右移动(朝向x-axis+ ),同时转换负值沿x-axis (类似translateX(-10px) )将其移动到左侧。

Similarly, a translation of a positive value along the y-axis (like translateY(10px) ) moves the element down (towards the + of the y-axis ), while a translation of a negative value along the y-axis (like translateY(-10px) ) moves it up. 类似地,沿y-axis的正值的平移(如translateY(10px) )向下移动元素(朝向y-axis+ ),而沿y-axis的负值的平移(如translateY(-10px) )将其向上移动。

Now add another dimension. 现在添加另一个维度。 With the z-axis . z-axis The + (positive direction) of the z-axis comes out of the screen, towards you. z-axis+ (正方向)从屏幕出来,朝向你。 So a translation of a positive value along the z-axis (like translateZ(10px) ) moves the element forward (towards the + of the z-axis and towards you), while a translation of a negative value along the z-axis (like translateZ(-10px) ) moves it backward (away from you). 因此,沿z-axis的正值的平移(如translateZ(10px) )将元素向前移动(朝向z-axis+并朝向您),同时沿z-axis转换负值(像translateZ(-10px) )向后移动(远离你)。

1.2.b Rotations around the axes 1.2.b围绕轴旋转

Rotations of positive angle values (for example, rotate(15deg) - note that if the rotation axis is not specified, then it is assumed to be the z-axis ) in CSS are clockwise and rotations of negative angle values (like rotate(-15deg) ) are counter-clockwise. 正角度值的rotate(15deg)例如, rotate(15deg) - 请注意,如果未指定旋转轴,则假定为z-axis )CSS是顺时针和负角度值的rotate(-15deg)rotate(-15deg) )是逆时针的。

And clockwise means clockwise as seen from the + of the axis around which you rotate the element. 顺时针方向是顺时针方向,从旋转元素的轴的+看。

So a positive rotation around the x-axis means a clockwise rotation in the yOz plane as seen from the + of the x-axis , which is at the right. 因此,围绕x-axis的正旋转意味着在yOz plane x-axis yOz plane的顺时针旋转,如从x-axis+ ,其在右侧。

A positive rotation around the y-axis means a clockwise rotation in the zOx plane (the horizontal plane) as seen from the + of the y-axis , which is at the bottom. 围绕y-axis正旋转意味着在zOx plane (水平面)中的顺时针旋转,如从y-axis+ zOx plane看到的,其位于底部。

A positive rotation around the z-axis means a clockwise rotation in the xOy plane (the plane of the screen) as seen from the + of the z-axis , which is how you naturally see the screen. 围绕z-axis正向旋转意味着从z-axis+看到xOy plane (屏幕平面)的顺时针旋转,这就是您自然地看到屏幕的方式。

1.3 Put the faces in the right positions in order to form the cube 1.3将面放在正确的位置以形成立方体

1.3.1 Put one face at the front 1.3.1将一面放在前面

This means translating it forwards (in the positive direction) along the z-axis . 这意味着沿z-axis沿正方向)平移。 What is this? 这是什么? A translateZ of a positive value. 正值的translateZ What value? 有什么价值? Well, it should be half the width (or the height , doesn't matter, it's a square, they're equal). 嗯,它应该是width一半(或者height ,无所谓,它是一个正方形,它们是相等的)。

Suppose I have the width: 16em; 假设我有width: 16em;

Then in this case, you translate the face forward (along the positive z-axis ) by 16em/2 = 8em . 然后在这种情况下,您将面向前(沿正z-axis16em/2 = 8em In CSS, that's 在CSS中,那是

.face:nth-child(1) { transform: translateZ(8em); }

Note : The translate transform moves the entire coordinate system of the element that is translated (and consequently, the transform-origin for any subsequent transforms). 注意translate transform会移动已转换元素的整个坐标系(因此,任何后续转换的transform-origin )。

1.3.2 Put the second face at the back 1.3.2将第二张脸放在后面

That's simple, right? 那很简单,对吧? Just a translate along the z-axis , by the same value in the opposite direction, right? 只是沿着z-axis平移,沿相反方向的相同值,对吗? .face:nth-child(2) { transform: translateZ( -8em ); } .face:nth-child(2) { transform: translateZ( -8em ); } ); } , right? ); } ,对不对?

Well... actually... only if you don't want to put any content on that face. 嗯...实际上...只有当你不想在那张脸上放任何内容时。 Or if you don't want to have as a background an image for which it matters which is left and which is right. 或者,如果你不想将一个重要的图像作为背景,哪个是重要的,哪个是正确的。

Each of these squares that make up the cube has a front and a back . 构成立方体的每个方格都有正面和背面 The front is the one towards the positive direction of the z-axis ; 正面是朝向z-axis正方向的正面; the one that "looks at you from the computer screen". 那个“从电脑屏幕上看着你”的那个。 If you put text there, it flows normally on the front. 如果你把文字放在那里,它会在正面流动。 But it looks vertically mirrored on the back. 但它看起来在背面垂直镜像。

That's why the first thing that you should do is to rotate the second square face by 180° around the vertical axis ( y-axis ). 这就是为什么你应该做的第一件事是围绕垂直轴( y-axis )旋转第二个方形面180°。 After doing that, you can then translate this second square face along the z-axis in order to move it to the back. 完成后,您可以沿z-axis平移第二个方形面,以便将其移动到背面。

The translate value is again positive in this case. 在这种情况下, 翻译值再次为正 Just like the translate transform moves the coordinate system of the element that is translated, the rotate transform ... well... rotates it. 就像translate transform 移动被转换元素的坐标系一样,旋转transform ......好吧...... 旋转它。 This means that after rotateY(180deg) is applied, the + of the z-axis points towards the back (not towards the front anymore). 这意味着在应用rotateY(180deg)后, z-axis+指向后方 (不再朝向前方)。

So the CSS that rotates and then translates the second face into its position on the cube is: 因此,旋转然后将第二个面转换为其在多维数据集上的位置的CSS是:

.face:nth-child(2) { transform: rotateY(180deg) translateZ(8em); }

Note : the cube is a really simple 3D shape, but one CSS property that I find really useful to check whether I've rotated faces the right way is backface-visibility . 注意 :立方体是一个非常简单的3D形状,但是我发现一个CSS属性非常有用,可以检查我是否以正确的方式旋转面部是backface-visibility If I set it to hidden and I don't see the rotated element, it means that I'm looking at it from the back. 如果我把它设置为hidden而我看不到旋转的元素,则意味着我从后面看它。

1.3.3 Put the third face to the right 1.3.3将第三张脸放在右边

First of all, its front has to "look" towards the right. 首先,它的前方必须“向右看”。 This means that it has to be rotated around the y-axis so that the + of the z-axis ends up pointing towards the right and then it has to be translated along the positive z-axis by the same positive value ( 8em in this case) that is half the length of the side of the square. 这意味着它必须围绕y-axis旋转,使得z-axis+最终指向右边,然后它必须沿着正z-axis平移相同的正值( 8em ) case)是正方形边长的一半。

Rotated by what angle? 旋转了什么角度? Well, 90°, which means the CSS needed is: 好吧,90°,这意味着所需的CSS是:

.face:nth-child(3) { transform: rotateY(90deg) translateZ(8em); }

1.3.4 Put the fourth face to the left 1.3.4将第四张脸放在左边

First rotate it by 90° , but the other way , to make its front "look" towards the left. 首先将其旋转90° ,但另一方面 ,使其向前“向左”看。 "The other way" means rotateY(-90deg) , then apply the same old translateZ(8em) . “另一种方式”意味着rotateY(-90deg) ,然后应用相同的旧translateZ(8em) In one CSS line: 在一个CSS行中:

.face:nth-child(4) { transform: rotateY(-90deg) translateZ(8em); }

1.3.5 Put the fifth face at the top 1.3.5将第五张脸放在顶部

First rotate it by 90° around the x-axis and then translate along the z-axis (which points up after the rotation). 首先围绕x-axis旋转90° ,然后沿z-axis平移(在旋转后指向上方)。 CSS: CSS:

.face:nth-child(5) { transform: rotateX(90deg) translateZ(8em); }

1.3.6 Put the sixth (and last!) face at the top 1.3.6将第六个(和最后一个!)面放在顶部

Rotate it by 90° the other way around the x-axis , then translate it along the z-axis (which points down after the rotation). 将其沿x-axis旋转90° ,然后沿z-axis平移(在旋转后指向下方)。 CSS: CSS:

.face:nth-child(6) { transform: rotateX(-90deg) translateZ(8em); }

1.4 Perspective and realistic looking 3D shapes 1.4透视和逼真的3D形状

All the faces of the cube are now in place. 立方体的所有面都已到位。 But this won't look like much of a 3D shape unless the faces that are closer seem bigger than those that are further away. 但这看起来不像3D形状,除非更接近的面看起来比那些更远的面更大。 The CSS property that takes care of this is called perspective and is applied on the parent of the elements which have 3D transforms applied on them. 处理此问题的CSS属性称为perspective ,并应用于对其应用3D变换的元素的父元素。

It still won't look like much of a 3D shape if you look perpendicular to the center of the front face because you won't see any of the other faces. 如果你看起来垂直于正面的中心,它仍然看起来不像3D形状,因为你不会看到任何其他面。 To fix this, you need to rotate the cube itself in 3D. 要解决此问题,您需要在3D中旋转立方体。 And this is why it's important that the browser allows nesting of 3D transformed elements. 这就是为什么浏览器允许嵌套3D转换元素的重要性。

You enable nesting of 3D transformed elements with transform-style: preserve-3d; 您可以使用transform-style: preserve-3d;启用3D变换元素的嵌套transform-style: preserve-3d; on the parent element ( .cube in this case). 在父元素上(在本例中为.cube )。 Unfortunately, IE10/11 do not support the preserve-3d value for the transform-style property (only support the flat value), so you cannot have a realistic looking cube in IE yet (unless you take the 3D transforms applied on the cube and chain them before the transforms applied for each face, which means one set of keyframes for each face if you want to animate the cube as a whole ). 遗憾的是,IE10 / 11不支持transform-style属性的preserve-3d值(仅支持flat值),因此您无法在IE中拥有逼真的立方体(除非您在立方体上应用3D变换并且在为每个面应用变换之前对它们进行链接,这意味着如果要为整个立方体设置动画,则为每个面添加一组关键帧

Update : transform-style: preserve-3d now landing in IE as well. 更新transform-style: preserve-3d现在也在IE中登陆。

Note : If you also apply 3D transforms on the .cube , then you should move the perspective property on the parent of the .cube . 注意 :如果您还在.cube上应用3D变换,则应在.cube的父级上移动perspective属性。

2 Animating the CSS cube 2动画CSS多维数据集

That's done using just the regular keyframe animations for the .cube element. 这只是使用.cube元素的常规关键帧动画完成的。 Let's say you want to rotate it: 假设您要旋转它:

@keyframes ani { to { transform: rotate3d(5, 12, 13, 360deg); } }

You can have multiple keyframes, with rotations around different axes, but this is the basic idea. 你可以有多个关键帧,围绕不同的轴旋转,但这是基本的想法。

Its all css3 and javascript. 它的所有css3和javascript。 Just inspect the site in your favorite inspector, dig into the dom, and you'll see something like 只需在您最喜爱的检查员中检查网站,深入了解dom,您就会看到类似的内容

-webkit-perspective: none;
-webkit-perspective-origin: 200px 200px;
-webkit-transform: matrix3d(-0.9386958080415784, -0.197569680458564, 0.2825179663820851, 0, 0, 0.8194947008605812, 0.5730867606754029, 0, -0.34474654452969944, 0.537954139890128, -0.7692562404101148, 0, 0, 0, 0, 1);
-webkit-transform-style: preserve-3d;
-webkit-transition-delay: 0s;
-webkit-transition-duration: 16s;
-webkit-transition-property: all;
-webkit-transition-timing-function: linear;

how much javascript you need depends on how you want to implement it. 你需要多少javascript取决于你想要如何实现它。 It can be 'a lot' or 'a little'. 它可以是“很多”或“一点点”。

The more modern a browser, the better the change it will run well. 浏览器越现代,它就会越好运行。 Check here to see which browsers support what. 点击此处查看哪些浏览器支持哪些内容。

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

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