Flipping Book Animation 3D Using CSS3 and JavaScript

Introduction

 
In this article we will create a flipping book animation using CSS3 and JavaScript. This article assumes the reader is familiar with basic CSS3 and JavaScript syntax and styles. This flipping book effect can be used to design an entire website on this style or any specific section of the website. Mostly storytelling websites fit very well with this effect. So let's start building this effect.
 

Flipping Book Animation

 
Flipping book animation is an effect that emulates a page transition effect of a book. Use the following procedure to implement this effect.
 
First, we need to fix our camera. That is the distance between the user and the book. This is done using the perspective property of CSS3.
 
Now we have our camera fixed. The next task is to design a binding line of the book around which pages will turn. To do this first we need to change the flat web page to 3D view. This can be done using the transform style property of CSS3 and setting it to "preserve-3D". To add a bit of "book placed on the floor" effect I'm translating the Z axes away from the viewer and then rotating it by 45 degrees.
 
The next task in the list is to add pages to the book. Each page is represented as a div. To convert a box into a 3D page we need to apply a CSS3 transformation known as origin shift. Let's shift the origin of the pages to their left edge. This means that all the transformations will consider the left edge as the origin of transformation and that really simplifies our task.
 
Now we have our camera ready, book ready and the action is about to begin!
 
For the action part, we need to use JavaScript. Using JavaScript we will detect the left/right navigation and then turn the pages accordingly. Detecting keys is very easy and can be done by binding the "keydown" event to the document. The next interesting move is to turn the pages, well that's also very simple; just remember the rotate function of CSS3 and apply it with an angle of rotation equal to 180 degrees for the right to left and 0 degrees for the left to right. Here 0 degrees is not equal to -180 degree since we are working in a 3D space. -180 degrees will rotate the page at the back of the book and this is not what we want so we need to use zero degrees to reset the rotation of 180 degrees.
 
So far so good! We are almost done but our JavaScript needs some tweaks for handling notorious user key inputs like if someone is turning the book page on the right when they all are already on the right side or vice versa. To handle such situations we will maintain the two stacks, one is for the left side book pages and the other is for the right-hand side book pages. Initially, the left side stack will be empty since the book is in the closed state. When the right side stack goes empty, it will indicate the closing of the book. So, in a nutshell, we need to take one page from the right stack and push it into the left stack or vice versa.
 
And here we go for the action! Copy and paste the following code:
 
HTML
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <scriptsrcscriptsrc="http://code.jquery.com/jquery-1.9.1.min.js"></script>  
  5.   <metacharsetmetacharset="utf-8">  
  6.   <title>JS Bin</title>  
  7. </head>  
  8. <body>  
  9.   <dividdivid="camera">  
  10.     <dividdivid="galry">  
  11.       <dividdivid="pic1"class="pic">  
  12.         <imgsrcimgsrc="http://goo.gl/qePIfH"alt="Front page"width="450"height="450"/>  
  13.       </div>  
  14.       <dividdivid="pic2"class="pic">  
  15.         <imgsrcimgsrc="http://goo.gl/hMf61G"width="450"height="450"/>  
  16.       </div>  
  17.       <dividdivid="pic3"class="pic">  
  18.         <imgsrcimgsrc="http://goo.gl/hMf61G"width="450"height="450"/>  
  19.       </div>  
  20.       <dividdivid="pic4"class="pic">  
  21.         <imgsrcimgsrc="http://goo.gl/hMf61G"width="450"height="450"/>  
  22.       </div>  
  23.       <dividdivid="pic5"class="pic">  
  24.         <imgsrcimgsrc="http://goo.gl/hMf61G"width="450"height="450"/>  
  25.       </div>  
  26.       <dividdivid="pic6"class="pic">  
  27.         <imgsrcimgsrc="http://goo.gl/AIHPPn"width="450"height="450"/>  
  28.       </div>  
  29.     </div>  
  30.   </div>  
  31. </body>  
  32. </html> 
CSS
  1. #camera{  
  2.   -webkit-perspective:500px;  
  3. }  
  4. #galry{  
  5.   position:absolute;  
  6.   top:100px;  
  7.   left:50%;  
  8.   border:1px solid black;  
  9.   width:1px;  
  10.   height:450px;  
  11.   -webkit-transform:translateZ(-100px) rotateX(45deg);  
  12.   -moz-transform:translateZ(-100px) rotateX(45deg);  
  13.   -o-transform:translateZ(-100px) rotateX(45deg);  
  14.   -ms-transform:translateZ(-100px) rotateX(45deg);  
  15.   -webkit-transform-style:preserve-3d;  
  16.   -moz-transform-style:preserve-3d;  
  17.   -ms-transform-style:preserve-3d;  
  18.   -o-transform-style:preserve-3d; <!--not supported 12+ -->  
  19. }  
  20.    
  21. .pic{  
  22.   margin:10%;  
  23.   position:absolute;  
  24.   border:1px;  
  25.   width:450px;  
  26.   height:450px;  
  27.   -webkit-transition:all 2s ;  
  28.   -ms-transition:all 2s ;  
  29.   -moz-transition:all 2s ;  
  30.   -o-transition:all 2s ;  
  31.   -moz-transform-origin:left ;  
  32.   -ms-transform-origin:left: ;  
  33.   -o-transform-origin:left ;  
  34.   -webkit-transform-origin:left;  
  35. }  
  36. #pic1{  
  37.   z-index:6;  
  38. }  
  39. #pic2{  
  40.   z-index:5;  
  41. }  
  42. #pic3{  
  43.   z-index:4;  
  44. }  
  45. #pic4{  
  46.   z-index:3;  
  47. }  
  48. #pic5{  
  49.   z-index:2;  
  50. }  
  51. #pic6{  
  52.   z-index:1;  
JavaScript
  1. var yAngle = 180;  
  2. var pages=6,tempPage=0;  
  3. var pageLStack=[];  
  4. var pageRStack=[];  
  5. for(var i=pages;i>=1;i--)  
  6.     pageRStack.push(i);  
  7.    
  8. document.addEventListener('keydown',function(e)  
  9. {  
  10.    switch(e.keyCode)  
  11.     {  
  12.    
  13.    case37:// for left key  
  14.     yAngle = -180;  
  15.    var pl=pageRStack.pop();  
  16.        if(pl){  
  17.           pageLStack.push(pl);  
  18.     $('#pic'+pl).css("webkit-transform","rotateY("+yAngle+"deg)");  
  19.     $('#pic'+pl).css("z-index",""+tempPage++);  
  20.           console.log(tempPage+1);  
  21.        }  
  22.    break;  
  23.          
  24.    case39:// for right key  
  25.     yAngle = 0;  
  26.    var pr=pageLStack.pop();  
  27.        if(pr<= pages){  
  28.                   pageRStack.push(pr);  
  29.     $('#pic'+pr).css("webkit-transform","rotateY("+yAngle+"deg)");  
  30.     $('#pic'+pr).css("z-index",""+tempPage++);  
  31.           console.log(tempPage+1);  
  32.        }  
  33.    break;          
  34.     }  
  35. },false); 
Output
 
 
 
 
 
 
 

Summary

 
That's all for this article. I hope you have enjoyed this Camera, Book, and Action style of effect. In case of any doubt feel free to ask in comments. Thanks for reading and don't forget to share and like this article.


Similar Articles