Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 transform: rotate() and rotateY() not working together

Tags:

css

I have a simple div that I want to rotate it -35 degree first and then let it spin around the y-axis.

enter image description he4re

However by using transform: rotate(-35deg) rotateY(180deg);, what I really got this like this:

enter image description h4ere

The y-axis rotates with the div, makes my attempt fail.

So the question is, is there a way to reset the angle of the y-axis (probably using additional parent element and transform-origin?) after I rotate the element, to get the result I wanted?

like image 206
Mark Ni Avatar asked Dec 17 '13 01:12

Mark Ni


People also ask

Can I use CSS transform rotate?

The transform CSS property lets you rotate, scale, skew, or translate an element.

How do I change the position of a transform in CSS?

CSS Demo: transform-origin Reading from right to left, translate(100%, -50%) is the translation to bring the transform origin to the true origin, rotate(45deg) is the original transformation, and translate(-100%, 50%) is the translation to restore the transform origin to its original location.

Does CSS3 support 2D Tranforms?

CSS3 allows us to manipulate objects in 2D space by the use of the tranform property and its functions for 2D transformations.

How do you rotate objects using CSS3?

CSS rotate() The rotate() function accepts one argument: the angle at which you want to rotate your web element. You can rotate an element clockwise or counter-clockwise. Let's take a look at the syntax for the rotate() function: transform: rotate(angle);


2 Answers

Yes, although it will require using a container div. Consider putting a div within a div:

<div class="outer">
  <div class="inner"></div>
</div>

then apply your Z transform (the first one) to the inner div:

.inner{
   transformZ(Ndeg);
}

and your Y transform to the container div, whose Y axis as not been transformed by the Z rotation

.outer {
   transformY(Ndeg);
}

Why this is necessary is complicated and is probably beyond the scope of this answer, but suffice it to say that the history of 3d programming and drawing leads programmers to expect the behavior you see here... that way multiple successive transforms can have predictable, uniform results. If browsers were not rectilinear with respect to DOM elements, or if you had drawn a romboid shape (a kite or the like) using SVG, you could rotate it as you desire, with a transformation orientation going from one corner to the other.

like image 199
stolli Avatar answered Nov 12 '22 00:11

stolli


The order of the transforms is the opposite of what seems intuitive. Explaining why is a little too long , but try this:

.test {
    position: absolute;
    width: 100px;
    height: 200px;
    left: 50px;
    top: 100px;
    background-color: lightblue;
    -webkit-animation: rotate 3s infinite linear;
    animation: rotate 3s infinite linear;
}

@-webkit-keyframes  rotate {
    0% { -webkit-transform: rotateY(0deg) rotate(-35deg)}
  100% { -webkit-transform: rotateY(360deg) rotate(-35deg)}
}

@keyframes  rotate {
    0% { transform: rotateY(0deg) rotate(-35deg)}
  100% { transform: rotateY(360deg) rotate(-35deg)}
}

I rotate -35 deg and then rotateY, animating it from 0 to 360 (read the transform from right to left).

demo

like image 37
vals Avatar answered Nov 12 '22 00:11

vals