How to apply multiple CSS3 rotation transformations (compounded) without javascript?

孤街醉人 提交于 2019-12-11 18:07:51

问题


For example, let's say we have several sibling elements. With CSS sibling selectors, how could we apply more rotation to each subsequent element in addition to the rotation applied to the previous one?

Seems it would be necessary to specify that the origin of the transformation is the result of the previous transformation, but I don't know how to do it.

Here's an attempt which obviously doesn't work.

 transform: rotate(5deg);

回答1:


Since these elements are siblings, and not children of each other, each element's transform has no effect on the others.

Unfortunately, without dynamic variables, you will not be able to apply transforms dynamically based on the sibling count using CSS. CSS doesn't support additive declarations, so you will not be able to simply add another transform for every subsequent sibling. You will need a preprocessor such as Sass or LESS to generate the static CSS for you. That static CSS looks like this:

.el + .el { transform: rotate(10deg); }
.el + .el + .el { transform: rotate(15deg); }

If you don't use a preprocessor, you will need to hardcode this CSS for as many elements as necessary, or use JavaScript to apply the transforms dynamically.




回答2:


You cannot do it with css alone except using some technique like Bolt's answer

Consider using some javascript library (less.js would be perfect for your this particular case)



来源:https://stackoverflow.com/questions/28734742/how-to-apply-multiple-css3-rotation-transformations-compounded-without-javascr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!