Create a Page Turner With Javascript, CSS and HTML

Introduction

 
In this article we will discuss how to create a simple Page Turner using JavaScript, CSS and HTML. Like this:
 
image1.jpg
 
image2.jpg
 
image3.jpg

Step 1: First we will set the 3-d perspective of the <body> tag like this:
  1. body { -webkit-transform-style: preserve-3d;  
  2. -webkit-perspective: 1000px;  
  3. text-aligncenter;  
Only Safari and Chrome support this perspective property. This property is used to define how many pixels a 3D element is placed from the view.
 
Step 2: Now we will use our first div tag to show the Album like this:
  1. <div class="pageTurner frontPageTurner" id="frontPageTurnerpageTurner">  
  2.      <h3>ALBUM </h3>  
  3.  </div>  
  4.  <div class="pageTurner back" id="backpageTurner">  
  5.      <img src="Penguins.jpg" height="120px" width="120px" />  
  6.      <p>Penguins</p>  
  7.  </div> 
Now we will set its CSS property like this:
  1. .pageTurner { position: absolute; top: 100; height: 200; width:150;  
  2. background-color: Khaki; padding: 15;  
  3. border-radius: 10; border: 1px solid Tan;  
  4. -webkit-transition: -webkit-transform 1.2s;  
  5. -webkit-backface-visibility: hidden;  
  6. }  
  7.  .frontPageTurner { left: 225; -webkit-transform-origin: 0px 0px;z-index:1;} 
Here we will set the Animation Time Like this 
 
-webkit-transition: -webkit-transform 1.2s;
 
The following line is used to hide the page turns when it flipped over
 
-webkit-backface-visibility: hidden;
 
It defines whether or not an element should be visible when not facing the screen. This property is not supported by Opera. 
 
Here we used z-index property that is used to set the <div> like this 
 
.frontPageTurner { left: 225; -webkit-transform-origin: 0px 0px;z-index:1;} 
 
Now we will write the JavaScript code like this:
  1. <script type="text/javascript">  
  2.      function flip() {  
  3.          document.getElementById("frontPageTurnerpageTurner").style.webkitTransform = "rotateY(-180deg)";  
  4.          document.getElementById("backpageTurner").style.webkitTransform = "rotateY(0deg)";  
  5.      }  
  6.      function flop() {  
  7.          document.getElementById("frontPageTurnerpageTurner").style.webkitTransform = "rotateY(0deg)";  
  8.          document.getElementById("backpageTurner").style.webkitTransform = "rotateY(180deg)";  
  9.      }  
  10.      function main() {  
  11.          document.getElementById("frontPageTurnerpageTurner").addEventListener("click", flip, false);  
  12.          document.getElementById("backpageTurner").addEventListener("click", flop, false);  
  13.      }  
  14. </script>
We define the main() function in the <body> tag like this:
 
<body onload="main();"> 
 
This is basically used as a click event. 
 
So when we click on the page it rotates using flip and flop functions and the next (backPageTurner) will be opened. Where we will use an Image like this:
image4.jpg
 
Step 3: Now we will write the code for the remaining pages. First, we will use the <div> tags like this:
  1. <div class="pageTurner back1" id="backpageTurner1">  
  2.      <img src="Jellyfish.jpg" height="120px" width="120px" />  
  3.      <p>Jellyfish</p>  
  4.  </div>  
  5.  <div class="pageTurner frontPageTurner1" id="frontPageTurnerpageTurner1">  
  6.      <img src="Lighthouse.jpg" height="120px" width="120px" />  
  7.      <p>Lighthouse</p>  
  8.  </div>  
  9.  <div class="pageTurner back2" id="backpageTurner2">  
  10.  </div>  
  11.  <div class="pageTurner frontPageTurner2" id="frontPageTurnerpageTurner2">  
  12.      <img src="Hydrangeas.jpg" height="120px" width="120px" />  
  13.      <p>Hydrangeas</p>  
  14.  </div> 
After that we will be set its CSS like this:
  1. .back { left: 60; -webkit-transform-origin: 100% 70%;  
  2. -webkit-transform: rotateY(180deg);  
  3. }  
  4. .frontPageTurner1 { left: 215; -webkit-transform-origin: 0px 0px;z-index:0.8;}  
  5. .back1 { left: 57; -webkit-transform-origin: 100% 70%;  
  6.  -webkit-transform: rotateY(180deg);  
  7. }  
  8. .frontPageTurner2 { left: 215; -webkit-transform-origin: 0px 0px;z-index:-1;}  
  9. .back2 { left: 80; -webkit-transform-origin: 100% 70%;  
  10. -webkit-transform: rotateY(180deg);  
  11. }  
  12. body { -webkit-transform-style: preserve-3d;  
  13. -webkit-perspective: 1000px;  
  14. text-align: center;  
After that we will write the JavaScript code like this:
  1. <script type="text/javascript">  
  2.      function flip() {  
  3.          document.getElementById("frontPageTurnerpageTurner").style.webkitTransform = "rotateY(-180deg)";  
  4.          document.getElementById("backpageTurner").style.webkitTransform = "rotateY(0deg)";  
  5.      }  
  6.      function flop() {  
  7.          document.getElementById("frontPageTurnerpageTurner").style.webkitTransform = "rotateY(0deg)";  
  8.          document.getElementById("backpageTurner").style.webkitTransform = "rotateY(180deg)";  
  9.      }  
  10.      function flip1() {  
  11.          document.getElementById("frontPageTurnerpageTurner1").style.webkitTransform = "rotateY(-180deg)";  
  12.          document.getElementById("backpageTurner1").style.webkitTransform = "rotateY(0deg)";  
  13.      }  
  14.      function flop1() {  
  15.          document.getElementById("frontPageTurnerpageTurner1").style.webkitTransform = "rotateY(0deg)";  
  16.          document.getElementById("backpageTurner1").style.webkitTransform = "rotateY(180deg)";  
  17.      }  
  18.      function flip2() {  
  19.          document.getElementById("frontPageTurnerpageTurner2").style.webkitTransform = "rotateY(-180deg)";  
  20.          document.getElementById("backpageTurner2").style.webkitTransform = "rotateY(0deg)";  
  21.      }  
  22.      function flop2() {  
  23.          document.getElementById("frontPageTurnerpageTurner2").style.webkitTransform = "rotateY(0deg)";  
  24.          document.getElementById("backpageTurner2").style.webkitTransform = "rotateY(180deg)";  
  25.      }  
  26.      function main() {  
  27.          document.getElementById("frontPageTurnerpageTurner").addEventListener("click", flip, false);  
  28.          document.getElementById("backpageTurner").addEventListener("click", flop, false);  
  29.          document.getElementById("frontPageTurnerpageTurner1").addEventListener("click", flip1, false);  
  30.          document.getElementById("backpageTurner1").addEventListener("click", flop1, false);  
  31.          document.getElementById("frontPageTurnerpageTurner2").addEventListener("click", flip2, false);  
  32.          document.getElementById("backpageTurner2").addEventListener("click", flop2, false);  
  33.      }  
  34. </script>