Revolving Door 3D Animation Using CSS3 Only

Introduction

 
In this article, we will create a revolving door animation using CSS3 and HTML 5. We will not use any JavaScript throughout this article. The revolving door is a 3D effect that consists of six flaps or doors fixed to a center poll and all doors revolve round it. This door revolves continuously and automatically stops whenever the user moves the cursor over the door. The doors can be used for presenting any content in it. In this article, I'm using these doors to link my various web profiles. So let's start coding it.
 

Revolving Door HTML

 
The HTML consists of a "camera" div that is our frame of reference and we don't need to bother about it much. The only thing to remember is that the entire animation must be enclosed in this frame.
 
The next important part of the animation consists of a poll. The poll is a center div around that all the doors will revolve. This poll is to be placed on the Z axes in such a way that all doors get enough space to revolve. 
 
Another important part is the door. A door consists of a rectangular div. All rectangles have their own center of rotation and are placed at an angle of 60 degrees with each other. The reason for 60 degrees is that the total angle in one complete revolution is 360 degrees and it consists of six doors. so the angle made by one door is:
 
Angle between two doors = 360/6 => 60degree
 
All the content is placed inside the door div to show it as "on door" content.
 
Let's see the HTML for the preceding analysis:
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>  
  5.   <meta charset="utf-8">  
  6.   <title>JS Bin</title>  
  7. </head>  
  8. <body>  
  9.   <div id="camera">  
  10.   <div id="poll">  
  11.       <div id="face1">  
  12.         <a href="https://www.facebook.com/">  
  13.     <img src="http://t3.gstatic.com/images?q=tbn:ANd9GcQjaVnNXN6CnjlL8Njlet7qpkoVOT5NvE0KKyEjZ6-HgSxIqFUVSg" width="200" height="200" />  
  14.   </a>  
  15.       </div>  
  16.       <div id="face2">  
  17.   <a href=""><img src="http://icons.iconarchive.com/icons/fasticon/web-2/256/Twitter-icon.png" width="200" height="200" ></a>  
  18.       </div>  
  19.       <div id="face3">  
  20.   <a href=""><img  
  21.   src="http://t3.gstatic.com/images?q=tbn:ANd9GcS8m0tLMxzpN-5Vt0WTDg9JXz6l5pTcRnIbcIu9OZ5XCDChChEYzQ" alt="Share on Google+" width="200" height="200"/></a>  
  22.       </div>  
  23.       <div id="face4">  
  24.         <a href="codewithlogic.wordpress.com" ><img  
  25.           src="http://recruitmentbuzz.co.uk/recruitment/wp-content/uploads/2013/06/r85.jpg" width=200 height=200 /></a>  
  26.       </div>  
  27.       <div id="face5">  
  28.         <a href="http://www.c-sharpcorner.com/authors/4aac15/arpit-jain.aspx" ><img  
  29.           src="http://www.c-sharpcorner.com/UploadFile/AuthorImage/4aac15.jpg" width=200 height=200 /></a>  
  30.       </div>  
  31.       <div id="face6">  
  32.         <a href="http://stackoverflow.com/users/637679/arpit" >  
  33.           <img src="http://www.skrenta.com/images/stackoverflow.jpg" width="200" height="200" /></a>  
  34.       </div>  
  35.   </div>  
  36.   </div>  
  37. </body>  
  38. </html> 
The preceding HTML will provide us a picture such as follows
 
 
Now to make the preceding output as a revolving door we need to use the power of CSS3.
 

Revolving Door CSS3

 
The CSS is as follows. The complete explanation of the code is given after this snippet.
  1. @-webkit-keyframes auto{  
  2.   0% {-webkit-transform:rotateY(0deg);}  
  3.  100% {-webkit-transform:rotateY(360deg);}  
  4. }  
  5. @-moz-keyframes auto{  
  6.   0% {-webkit-transform:rotateY(0deg);}  
  7.  100% {-webkit-transform:rotateY(360deg);}  
  8. }  
  9. @-o-keyframes auto{  
  10.   0% {-webkit-transform:rotateY(0deg);}  
  11.  100% {-webkit-transform:rotateY(360deg);}  
  12. }  
  13.   
  14. #camera{  
  15.   -webkit-perspective:1000px;  
  16. }  
  17.   
  18. #poll{  
  19.   position:absolute;  
  20.   margin-left:400px;  
  21.   margin-top:100px;  
  22.   height:200px;  
  23.   width:1px;  
  24.   -webkit-transform:translateZ(100px);  
  25.   -webkit-transform-style:preserve-3d;  
  26.    border:2px solid black;  
  27.   -webkit-animation:auto 20s;  
  28.   -webkit-animation-iteration-count:infinite;  
  29. }  
  30.   
  31. #poll:hover{  
  32.  -webkit-animation-play-state:paused;   
  33. }  
  34.   
  35. #face1{  
  36.   position:absolute;  
  37.   height:200px;  
  38.   width:200px;  
  39.   border:1px solid green;  
  40. }  
  41.   
  42. #face2{  
  43.   position:absolute;  
  44.   border:1px solid red;  
  45.   height:200px;  
  46.   width:200px;  
  47.   -webkit-transform:translateZ(90px) translateX(-151px) rotateY(60deg);  
  48. }  
  49. #face3{  
  50.   position:absolute;  
  51.   border:1px solid blue;  
  52.   height:200px;  
  53.   width:200px;  
  54.   -webkit-transform-origin:left;  
  55.   -webkit-transform:rotateY(120deg);  
  56. }  
  57. #face4{  
  58.   position:absolute;  
  59.   border:1px solid orange;  
  60.   height:200px;  
  61.   width:200px;  
  62.   -webkit-transform-origin:left;  
  63.   -webkit-transform:rotateY(180deg);  
  64. }  
  65. #face5{  
  66.   position:absolute;  
  67.   border:1px solid black;  
  68.   height:200px;  
  69.   width:200px;  
  70.   -webkit-transform-origin:left;  
  71.   -webkit-transform:rotateY(60deg);  
  72. }  
  73. #face6{  
  74.   position:absolute;  
  75.   border:1px solid orange;  
  76.   height:200px;  
  77.   width:200px;  
  78.   -webkit-transform-origin:left;  
  79.   -webkit-transform:rotateY(300deg);  

Don't be concerned about the length of the code. Once you get the logic it will be a cake walk for you to write this. Our first task in CSS is to provide the proper depth to our frame of reference and make it ready for a 3D view. This is done in the "#camera" selector by setting the perspective depth to 1000px.
 
The next step in CSS is to place the poll in its correct position. By correct position, I mean giving the proper space around the poll for doors to revolve. This is done by giving a margin of 400px. To provide the space on the Z-axes, the poll is translated 100px forward (optional). To preserve the 3D nature of the animation we have set the transform style to "preserve-3d".
 
To revolve this poll and our doors we have applied an animation to this poll. The keyframe animation of CSS3 revolves this poll from 0 degrees to 360 degrees in 20 seconds. All the doors revolve aroung this poll since they are attached to the poll.
 
Upon hovering over this poll or door we are stopping the animation by changing the animation play state so that the user can choose the proper option.
 
Next, we need to design the doors. In the doors, we need to set a uniform width and height for all six doors. Apart from the width, height, position, and border that is the same in all doors, the important factor that we need to manage is the transformations to apply and the origin of the transformation.
The first door needs no transformations since it's a reference for the other doors.
 
Door 2 requires a translation of 90px on Z-axes, translation of -151px on the X-axes and a rotation of 60 degrees around the Y-axes.
 
Door 3 requires an origin transform on its left edge and then a revolution of 120 degrees on the Y-axes.
 
Door 4 requires an origin transform on the left edge and the rotation of 180 degrees on Y-axes.
 
Similarly for door 5 and door 6. While doing all this we will get the following series of images.
 
 
 
 
 
 
 
 
 

Summary

 
That's all for this article. I hope you have enjoyed reading this article. We have successfully created a revolving door animation. The CSS looks a bit lengthy because of different browser-specific prefixes but it is easy to understand. In case of any doubt feel free to ask in the comments.
 


Similar Articles