I have created a cube based on this example.
http://css3.bradshawenterprises.com/transforms/#cubecarousel
What I need to understand is how can I get an exact measure (for example of 200px) of the facing side?
Usually you define the cube's faces and then you apply the transformation at half of the object (50%) so the object gets shifted forward.
How would I push it back so the facing side matches 200px?
Thank you
You should post your current code.
Anyway, I guess that your current images are 200px ?
Then taking into account that (from your link):
A 3D image slider
Note that because of the way a cube works, the image
is moved out towards the screen, and is bigger than it should be. You
should move it back by half the width of an image to make sure it is
normal size.
If you translate your cube 100px (half the width) backwards, your images will be exactly 200px.
If your images are not 200px, definitely post your code
Ok, here is the fiddle: jsfiddle.net/K99GS I tried pushing it backwards, but it changes the pivot so it rotates from the wrong origin...
Ok after trying for one day... I found out my solution was a DUD.
Here is the exact cube (216px) and the code.
How do I push it back????????
Here is the relevant css code:
#controls, #transparency {
text-align:center;
}
#controls span {
padding-right:2em;
cursor:pointer;
}
#cubeCarousel {
-webkit-perspective: 800;
-moz-perspective: 800px;
-ms-perspective: 800;
perspective: 800;
-webkit-perspective-origin: 50% 100px;
-moz-perspective-origin: 50% 100px;
-ms-perspective-origin: 50% 100px;
perspective-origin: 50% 100px;
margin:100px auto 20px auto;
width:216px;
height:216px;
}
#cubeCarousel #cubeSpinner {
position: relative;
margin: 0 auto;
height: 216px;
width: 216px;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-transform-origin: 50% 108px 0;
-moz-transform-origin: 50% 108px 0;
-ms-transform-origin: 50% 108px 0;
transform-origin: 50% 108px 0;
-webkit-transition:all 1.0s ease-in-out;
-moz-transition:all 1.0s ease-in-out;
-ms-transition:all 1.0s ease-in-out;
transition:all 1.0s ease-in-out;
}
#cubeCarousel #Ycube,#cubeCarousel #Zcube {
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
transform-style: preserve-3d;
}
#cubeCarousel .face {
position: absolute;
height: 216px;
width: 216px;
padding: 0px;
background-color:rgba(0,0,0,0.5);
}
#cubeSpinner .one {
-webkit-transform: translateZ(108px);
-moz-transform: translateZ(108px);
-ms-transform: translateZ(108px);
transform: translateZ(108px);
}
#cubeSpinner .two {
-webkit-transform: rotateY(90deg) translateZ(108px);
-moz-transform: rotateY(90deg) translateZ(108px);
-ms-transform: rotateY(90deg) translateZ(108px);
transform: rotateY(90deg) translateZ(108px);
}
#cubeSpinner .three {
-webkit-transform: rotateY(180deg) translateZ(108px);
-moz-transform: rotateY(180deg) translateZ(108px);
-ms-transform: rotateY(180deg) translateZ(108px);
transform: rotateY(180deg) translateZ(108px);
}
#cubeSpinner .four {
-webkit-transform: rotateY(-90deg) translateZ(108px);
-moz-transform: rotateY(-90deg) translateZ(108px);
-ms-transform: rotateY(-90deg) translateZ(108px);
transform: rotateY(-90deg) translateZ(108px);
}
See it in action ->
http://jsfiddle.net/K99GS/5/
Related
Demo
The core of the flipping animation is on transform: rotateY(180deg);. Of course transition is needed as well for animation. I copied the code from another site, which uses transition: all ...;.
For some reason, some properties (eg: height, width) must have no transition. But I don't know which properties are essential for flipping animation.
Anyone knows how I can change the line transition: all ...; to keep the flipping animation, while not affecting unrelated properties?
This page explains everything and has live demos. I've just needed a simple Google search. :)
http://desandro.github.io/3dtransforms/docs/card-flip.html
Apparently, the trick is to use
transition: transform 1s;
change the style when you :hover on parent
.card {
-moz-perspective: 600;
-webkit-perspective: 600;
perspective: 600;
position: relative;
height: 200px;
width: 200px;
}
.card .side {
overflow: hidden;
position: absolute;
top: 0;
left: 0;
width: inherit;
height: inherit;
-moz-box-shadow: 0 0 5px #888;
-webkit-box-shadow: 0 0 5px #888;
box-shadow: 0 0 5px #888;
-moz-transform-style: perserve-3d;
-webkit-transform-style: perserve-3d;
transform-style: perserve-3d;
-moz-backface-visibility: hidden;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-moz-transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
-webkit-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
}
.card .side.front {
z-index: 900;
-moz-transform: rotateY(0deg);
-ms-transform: rotateY(0deg);
-webkit-transform: rotateY(0deg);
transform: rotateY(0deg);
}
.card:hover .side.front {
-moz-transform: rotateY(180deg);
-ms-transform: rotateY(180deg);
-webkit-transform: rotateY(180deg);
transform: rotateY(180deg);
}
.card .side.back {
z-index: 800;
-moz-transform: rotateY(-180deg);
-ms-transform: rotateY(-180deg);
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}
.card:hover .side.back {
z-index: 800;
-moz-transform: rotateY(0deg);
-ms-transform: rotateY(0deg);
-webkit-transform: rotateY(0deg);
transform: rotateY(0deg);
}
<article class="card">
<section class="side front">front</section>
<section class="side back">back</section>
</article>
I have started a 3D rotating image carousel which works fine in Chrome & Firefox but not IE.
I know that IE10+ doesn't yet support the preserve-3d tag but there is meant to be a workaround of putting the transforms on the children, but I can't get it to work.
Any ideas and help will be most welcome.
http://codepen.io/gnm67/pen/izyjs
#Carousel {
display: block;
margin:100px auto 20px auto;
-webkit-perspective: 1000px;
-moz-perspective: 1000px;
-ms-perspective: 1000px;
perspective: 1000px;
-webkit-perspective-origin: 50% 100px;
-moz-perspective-origin: 50% 100px;
-ms-perspective-origin: 50% 100px;
perspective-origin: 50% 100px;
}
#Spinner {
position: relative;
margin: 0 auto;
height: 350px;
width: 500px;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-transform-origin: 50% 400px 0px;
-moz-transform-origin: 50% 400px 0px;
-ms-transform-origin: 50% 400px 0px;
transform-origin: 50% 400px 0px;
-webkit-transition:all 1.0s ease-in-out;
-moz-transition:all 1.0s ease-in-out;
-ms-transition:all 1.0s ease-in-out;
transition:all 1.0s ease-in-out;
-ms-perspective: 1000px;
}
#Carousel .face {
position: absolute;
height: 350px;
width: 500px;
padding: 0px;
}
#Carousel img {
width:500px;
-moz-box-shadow:1px 1px 5px #000;
-webkit-box-shadow:1px 1px 5px #000;
box-shadow:1px 1px 5px #000;
}
#Spinner .f0 {
-webkit-transform: rotateY(0deg) translateZ(344px);
-moz-transform: rotateY(0deg) translateZ(344px);
-ms-transform: perspective(1000px) rotateY(0deg) translateZ(344px);
transform: rotateY(0deg) translateZ(344px);
}
#Spinner .f1 {
-webkit-transform: rotateY(72deg) translateZ(344px);
-moz-transform: rotateY(72deg) translateZ(344px);
-ms-transform: perspective(1000px) rotateY(72deg) translateZ(344px);
transform: rotateY(72deg) translateZ(344px);
}
#Spinner .f2 {
-webkit-transform: rotateY(144deg) translateZ(344px);
-moz-transform: rotateY(144deg) translateZ(344px);
-ms-transform: perspective(1000px) rotateY(144deg) translateZ(344px);
transform: rotateY(144deg) translateZ(344px);
}
#Spinner .f3 {
-webkit-transform: rotateY(216deg) translateZ(344px);
-moz-transform: rotateY(216deg) translateZ(344px);
-ms-transform: perspective(1000px) rotateY(216deg) translateZ(344px);
transform: rotateY(216deg) translateZ(344px);
}
#Spinner .f4 {
-webkit-transform: rotateY(288deg) translateZ(344px);
-moz-transform: rotateY(288deg) translateZ(344px);
-ms-transform: perspective(1000px) rotateY(288deg) translateZ(344px);
transform: rotateY(288deg) translateZ(344px);
}
Note that the -ms- prefix is deprecated, it was supposed to be used only in the release preview, as of the final IE10 the unprefixed properties are supported, which will overwrite the prefixed ones!
That being said, you'll have to change the position of the individual faces instead of rotating the container.
I found the easiest is to shift the faces transform origin into the center, that way you only have to rotate the faces to get the animation running.
Here's a bare to the bones example based on your code: http://jsfiddle.net/k1m045uu/
<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#Carousel {
display: block;
margin:100px auto 20px auto;
}
#Spinner {
position: relative;
margin: 0 auto;
height: 350px;
width: 500px;
}
#Carousel .face {
position: absolute;
height: 350px;
width: 500px;
transform-origin: 50% 50% -250px;
transition: all 1.0s ease-in-out;
}
#Carousel img {
width: 500px;
box-shadow: 0 0 0 1px #000;
}
#Spinner .f0 {
transform: perspective(1000px) rotateY(0deg) translateZ(95px);
}
#Spinner .f1 {
transform: perspective(1000px) rotateY(72deg) translateZ(95px);
}
#Spinner .f2 {
transform: perspective(1000px) rotateY(144deg) translateZ(95px);
}
#Spinner .f3 {
transform: perspective(1000px) rotateY(216deg) translateZ(95px);
}
#Spinner .f4 {
transform: perspective(1000px) rotateY(288deg) translateZ(95px);
}
</style>
<script src='http://code.jquery.com/jquery-1.11.1.min.js'></script>
</head>
<body>
<div id="Carousel">
Previous Next
<div id="Spinner">
<img class="face f0" src="img/test1.jpg" />
<img class="face f1" src="img/test2.jpg" />
<img class="face f2" src="img/test3.jpg" />
<img class="face f3" src="img/test4.jpg" />
<img class="face f4" src="img/test5.jpg" />
</div>
</div>
<script>
var elements = $('.face');
var rotates = [0, 72, 144, 216, 288];
function rotate(deg)
{
elements.each(function(index)
{
rotates[index] = rotates[index] + deg;
$(this).css('transform', 'perspective(1000px) rotateY(' + rotates[index] + 'deg) translateZ(95px)');
});
}
</script>
</body>
</html>
See also How to recreate perspective-origin effect by transforming child elements? for some more info regarding perspective origin and a further example.
Whenever I execute an Animate.css shake animation after a CSS3 rotate animation the div being shook will disappear. This only happens on the back-face of the rotated divs. The front-face doesn't have this issue.
I've put together a jsfiddle to demonstrate the problem.
http://jsfiddle.net/kG9Ld/1/
HTML
<div class="wrappers">
<div class="wrapper shaker front">
<p>Front</p>
<p>Flip</p>
<p>Shake</p>
</div>
<div class="wrapper shaker back">
<p>Back</p>
<p>Flip</p>
<p>Shake</p>
</div>
</div>
CSS
.wrappers {
-webkit-transition:-webkit-transform 1s;
-moz-transition:-moz-transform 1s;
-ms-transition:-ms-transform 1s;
-o-transition:-o-transform 1s;
transition: transform 1s;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.wrapper {
height: 100px;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;
-o-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
position: absolute;
width: 100px;
}
.front {
background-color: red;
z-index: 3;
}
.back {
background-color: green;
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
-ms-transform: rotateY(180deg);
-o-transform: rotateY(180deg);
transform: rotateY(180deg);
z-index: 1;
}
.flipped {
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
-ms-transform: rotateY(180deg);
-o-transform: rotateY(180deg);
transform: rotateY(180deg);
}
.flipped .back {
z-index: 3;
}
JavaScript
$('.flip').on('click', function (e) {
$wrappers = $('.wrappers');
$wrappers.toggleClass('flipped');
});
$('.shake').on('click', function (e) {
$('.shaker').addClass('animated shake');
});
$('.shaker').on('webkitAnimationEnd mozAnimationEnd oAnimationEnd animationEnd', function(e) {
$('.shaker').removeClass('animated shake');
});
The problem was that the shaking animation's transform property was overwriting the .back element's transform property: rotateY(180deg).
The solution would just be to modify the animation and use a combination of transformations:
WORKING EXAMPLE HERE
-webkit-transform: translateX(0) rotateY(180deg);
This would of course require new keyframes:
#-webkit-keyframes shakeBack {
0%, 100% {
-webkit-transform: translateX(0) rotateY(180deg);
transform: translateX(0) rotateY(180deg);
}
10%, 30%, 50%, 70%, 90% {
-webkit-transform: translateX(-10px) rotateY(180deg);
transform: translateX(-10px) rotateY(180deg);
}
20%, 40%, 60%, 80% {
-webkit-transform: translateX(10px) rotateY(180deg);
transform: translateX(10px) rotateY(180deg);
}
}
You now need to add a different animation based on whether the front/back of the element is visible.
.shakeBack {
-webkit-animation-name: shakeBack;
animation-name: shakeBack;
}
Modified JS - this could be written better, but you get the point.
$('.shaker.front .shake').on('click', function (e) {
$(this).parents('.shaker').addClass('animated shake');
});
$('.shaker.back .shake').on('click', function (e) {
$(this).parents('.shaker').addClass('animated shakeBack');
});
Alright. I've done quite a bit of research, but I wasn't able to come up with anything. After possibly over-thinking the problem, here is what I'd appreciate your help with.
I would like to be able to rotate a <div> around its center so its backside becomes visible. I was able to do that with rotateY--without any problems. But when using rotateX the <div> does not rotate around its center anymore.
The CSS:
.flip-container {
font-size: 30px;
-webkit-perspective: 1000;
-moz-perspective: 1000;
margin: 0 auto;
color: black;
}
.flip-container:hover .flipper {
-webkit-transform: rotateX(180deg);
-moz-transform: rotateX(180deg);
-o-transform: rotateX(180deg);
-ms-transform: rotateX(180deg);
}
.flipper {
-webkit-transition: 0.6s;
-webkit-transform-style: preserve-3d;
-moz-transition: 0.6s;
-moz-transform-style: preserve-3d;
position: relative;
}
.front, .back {
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
background: transparent;
}
.front {
z-index: 1;
}
.flip-container, .front, .back {
width: 200px;
height: 200px;
}
.back {
-webkit-transform: rotateX(-180deg);
-moz-transform: rotateX(-180deg);
-o-transform: rotateX(180deg);
-ms-transform: rotateX(180deg);
This problem is driving me up the walls. I know it's something small I overlooked; but I can't find it. Any helps or hints would be greatly appreciated. :)
Fiddle
For a vertical rotation, you need to specify transform-origin, with :
<x> : 50%
<y> : half height of the div
Updated Fiddle
.flipper {
-webkit-transform-origin: 50% 100px;
-moz-transform-origin: 50% 100px;
-o-transform-origin: 50% 100px;
-ms-transform-origin: 50% 100px;
transform-origin: 50% 100px;
-webkit-transition: 0.6s;
-webkit-transform-style: preserve-3d;
-moz-transition: 0.6s;
-moz-transform-style: preserve-3d;
position: relative;
}
I would like to reproduce this jsfiddle I that prepared based on this awesome tutorial (please check the demo). But I don't want the keys functionality, just on hover.
http://jsfiddle.net/b5rmW/5/
But that only uses 2 faces (front and back).
I tried, like this:
#cube {
position: relative;
margin: 100px auto 0;
height: 300px;
width: 300px;
-webkit-transition: -webkit-transform .5s linear;
-webkit-transform-style: preserve-3d;
-moz-transition: -moz-transform .5s linear;
-moz-transform-style: preserve-3d;
}
.face {
position: absolute;
height: 300px;
width: 300px;
padding: 0px;
background-color: rgba(50, 50, 50, 1);
font-size: 27px;
line-height: 1em;
color: #fff;
border: 1px solid #555;
border-radius: 3px;
}
#cube .one {
-webkit-transform: rotateX(90deg) translateZ(150px);
-moz-transform: rotateX(90deg) translateZ(150px);
background:red;
}
#cube .two {
-webkit-transform: translateZ(150px);
-moz-transform: translateZ(150px);
background:gold;
}
#cube .three {
-webkit-transform: rotateY(90deg) translateZ(150px);
-moz-transform: rotateY(90deg) translateZ(150px);
background:blue;
}
#cube .four {
-webkit-transform: rotateY(180deg) translateZ(150px);
-moz-transform: rotateY(180deg) translateZ(150px);
background:green;
}
#cube .five {
-webkit-transform: rotateY(-90deg) translateZ(150px);
-moz-transform: rotateY(-90deg) translateZ(150px);
background:orange;
}
#cube .six {
-webkit-transform: rotateX(-90deg) rotate(180deg) translateZ(150px);
-moz-transform: rotateX(-90deg) rotate(180deg) translateZ(150px);
}
#cube:hover{
transform:rotateY(90deg);
}
http://jsfiddle.net/5XTeU/1/
But the effect seems not to be the same.
What do you think is the minimum divs needed to achieve this first fiddle??
Thanks.
Update: So a slight misunderstanding on which faces need to exist… so this update is for a front and side face rotation.
However, in the original answer below, points 1) and 2) are still valid problems with the code. Points 3) and 4) no longer apply since they were concerned with the back face. The remaining CSS rules can be removed. You could also pull in the perspective wrapper to give the cube a "less flat" look - see updated demo.
HTML
<div id="experiment">
<div class="cube">
<div class="face front">
front face
</div>
<div class="face side">
side face
</div>
</div>
</div>
CSS
#experiment {
-webkit-perspective: 800;
-webkit-perspective-origin: 50% 200px;
-moz-perspective: 800;
-moz-perspective-origin: 50% 200px;
}
.cube {
position: relative;
margin: 100px auto 0;
height: 300px;
width: 300px;
-webkit-transition: -webkit-transform .5s linear;
-webkit-transform-style: preserve-3d;
-moz-transition: -moz-transform .5s linear;
-moz-transform-style: preserve-3d;
}
.face {
position: absolute;
height: 300px;
width: 300px;
padding: 0px;
font-size: 27px;
line-height: 1em;
color: #fff;
border: 1px solid #555;
border-radius: 3px;
}
.cube .front {
-webkit-transform: translateZ(150px);
-moz-transform: translateZ(150px);
background-color:red;
}
.cube .side {
-webkit-transform: rotateY(-90deg) translateZ(150px);
-moz-transform: rotateY(-90deg) translateZ(150px);
background-color:orange;
}
.cube:hover{
-webkit-transform:rotateY(90deg);
}
Original Answer
There are 4 problems with the demo code, so let's look at them individually and see what the solution to each one is:
1) the HTML has a typo on class for the front face - it is missing an r
<div class="face font"> instead of <div class="face front">
2) For Webkit browsers you need to use the prefixed property for transform
-webkit-transform:rotateY(90deg); instead of transform:rotateY(90deg);
3) The back face you have chosen is the wrong face. You have repurposed the left face by accident. The front face is correct, which is a <div> translated 150px outwards. So the corresponding back face should be the one translated -150px inwards. However, if we just do that, the position would be correct but when rotated around the centre of the cube the back face would end up backwards. So the correct back face is the one that is initially rotated by 180° around the Y axis. However, by rotating around the Y axis the translation along Z still needs to be +150px and not -150px.
.cube .back{
-webkit-transform: rotateY(180deg) translateZ(150px);
-moz-transform: rotateY(180deg) translateZ(150px);
background:orange;
}
4) The rotation to get the back face into the position where the front starts should be a rotation of 180° and not 90°
.cube:hover{
-webkit-transform:rotateY(180deg);
}
Putting all those changes together gives this demo.
HTML
<div class="cube">
<div class="face front">
front face
</div>
<div class="face back">
back face
</div>
</div>
CSS
.cube {
position: relative;
margin: 100px auto 0;
height: 300px;
width: 300px;
-webkit-transition: -webkit-transform .5s linear;
-webkit-transform-style: preserve-3d;
-moz-transition: -moz-transform .5s linear;
-moz-transform-style: preserve-3d;
}
.face {
position: absolute;
height: 300px;
width: 300px;
padding: 0px;
font-size: 27px;
line-height: 1em;
color: #fff;
border: 1px solid #555;
border-radius: 3px;
}
.cube .front {
-webkit-transform: translateZ(150px);
-moz-transform: translateZ(150px);
background-color: red;
}
.cube .back {
-webkit-transform: rotateY(180deg) translateZ(150px);
-moz-transform: rotateY(180deg) translateZ(150px);
background:orange;
}
.cube:hover{
-webkit-transform:rotateY(180deg);
-moz-transform: rotateY(180deg);
transform:rotateY(180deg);
}