Create a Page Turner With an Overlay in Javascript, CSS and HTML (Part-II)

Introduction

 
Before reading this article, please go through the following article:
Now we will discuss how to create a simple Page Turner with Overlay using JavaScript, CSS, and HTML. Like this: 
 
image1.gif
 
When we click on the image:
 
image2.gif
 
So when we click on the image the Overlay screen will be visible with the image and other data. Here we turn the page by clicking on the "Back" and the "Next" button like this:
 
image3.gif
 
Step 1: First we use two <div> tags, one for the Overlay screen (Light) and the other for the Back part (black) like this:
  1. <div id="light" class="overlay">  
  2.     <table width="100%">  
  3.         <tr>  
  4.             <td align="right">  
  5.                 <img src="Close.jpg" width="20px" onclick="document.getElementById('fade').style.display='block';  
  6. document.getElementById('fade').style.opacity='1';document.getElementById('light').style.display='none';" /></td>  
  7.         </tr>  
  8.         <tr>  
  9.             <td>  
  10.                 <img id="imgMain" src="Penguins.jpg" height="20%" />  
  11.             </td>  
  12.         </tr>  
  13.         <tr>  
  14.             <td>  
  15.                 <p id="info">These penguins are an elite strike force with unmatched commando skills.</p>  
  16.             </td>  
  17.         </tr>  
  18.     </table>  
  19. </div>  
  20. <div id="fade" class="black">  
  21. </div> 
Here we will specify the tags (<img> and <p>) to show our information in the overlay screen. Now we will set the style of both the screens like this:
  1. <style>  
  2.     .black {  
  3.         positionabsolute;  
  4.         top: 0%;  
  5.         left: 0%;  
  6.         width50%;  
  7.         height50%;  
  8.         background-color: White;  
  9.         z-index1001;  
  10.         -moz-opacity: 0.8;  
  11.         filter: alpha(opacity=80);  
  12.     }   
  13.     .overlay {  
  14.         displaynone;  
  15.         positionabsolute;  
  16.         -webkit-border-radius: 20px;  
  17.         border5px solid red;  
  18.         background-colorwhite;  
  19.         z-index1002;  
  20.         overflowauto;  
  21.     }  
  22. </style> 
Here we set the Opacity and the z-index, which creates the overlay screen.
 
Step 2: Now we will use our first div tag to show the Album like this:
  1. <div id="backpageTurnermain" class="pageTurner back">  
  2.     <img src="Penguins.jpg" id="img1" height="120px" width="120px" onclick="Show();document.getElementById('light').style.display='block';  
  3. document.getElementById('fade').style.opacity='0.2';" />  
  4.     <p>Penguins</p>  
  5.     <br />  
  6.     <a id="a1" style="font-size: 10px;" onclick="flop()">Back</a>  
  7. </div>  
  8. <div class="pageTurner frontPageTurner" id="frontPageTurnerpageTurner">  
  9.     <h3>ALBUM </h3>  
  10. </div> 
Now we will set its CSS property like this:
  1. .pageTurner {  
  2.     position: absolute;  
  3.     top: 100;  
  4.     height: 200;  
  5.     width: 150;  
  6.     background-color: Khaki;  
  7.     padding: 15;  
  8.     border-radius: 10;  
  9.     border: 1px solid Tan;  
  10.     -webkit-transition: -webkit-transform 1.2s;  
  11.     -webkit-backface-visibility: hidden;\  
  12. }   
  13. .frontPageTurner {  
  14.     left: 225;  
  15.     -webkit-transform-origin: 0px 0px;  
  16.     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 is 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 the z-index property 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();">
 
Here we will use the Show() function to animate the Overlay screen like this:
  1. <script type="text/javascript">  
  2.     var x = 10;  
  3.     var l = 10;  
  4.     var t = 10;   
  5.     function Show() {   
  6.         document.getElementById('light').style.width = "0px";  
  7.         document.getElementById('light').style.height = "0px";  
  8.         setTimeout("Show1()", 500);  
  9.     }  
  10.     function Show1() {  
  11.         if ((x < 260) && (l < 500)) {  
  12.             if (t < 150) {  
  13.                 document.getElementById('light').style.top = t + "px";  
  14.             }  
  15.             else {  
  16.                 document.getElementById('light').style.top = "120px";   
  17.             }  
  18.             document.getElementById('light').style.left = l + "px";  
  19.             document.getElementById('light').style.width = x + "px";  
  20.             document.getElementById('light').style.height = x + "px";   
  21.             x = x + 10;  
  22.             l = l + 10;  
  23.             t = t + 10;  
  24.             setTimeout("Show1()", 10);  
  25.         }  
  26.         else {  
  27.             x = 0  
  28.             l = 0;  
  29.             t = 0;   
  30.         }  
  31.     }  
  32. </script>  
Here we increase the height and width of the Overlay screen by calling the function Show1() after a specified period of time.
 
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" onclick="Show();document.getElementById('light').style.display='block';  
  3. document.getElementById('fade').style.opacity='0.2';ShowImage2();" />  
  4.     <p>Jellyfish</p>  
  5.     <br />   
  6.     <a id="a1" style="font-size: 10px;" onclick="flop1()">Back</a>  
  7. </div>   
  8. <div class="pageTurner frontPageTurner1" id="frontPageTurnerpageTurner1">   
  9.     <img src="Lighthouse.jpg" height="120px" width="120px" onclick="Show();document.getElementById('light').style.display='block';  
  10. document.getElementById('fade').style.opacity='0.2';ShowImage1();" />  
  11.     <p>Lighthouse</p>  
  12.     <br />   
  13.     <a id="a2" style="font-size: 10px;" onclick="flip1()">Next</a>   
  14. </div>   
  15. <div class="pageTurner back2" id="backpageTurner2" onclick="flop2()">  
  16.     <br />   
  17. </div>  
  18. <div class="pageTurner frontPageTurner2" id="frontPageTurnerpageTurner2">   
  19.     <img src="Hydrangeas.jpg" height="120px" width="120px" onclick="Show();document.getElementById('light').style.display='block';  
  20. document.getElementById('fade').style.opacity='0.2';ShowImage3();" />  
  21.     <p>Hydrangeas</p>  
  22.     <br />   
  23.     <a id="a3" style="font-size: 10px;" onclick="flip2()">Next</a>  
  24. </div>   
  25. </div> 
After that we will be set its CSS like this:
  1. .back {  
  2.     left: 60;  
  3.     -webkit-transform-origin: 100% 70%;  
  4.     -webkit-transform: rotateY(180deg);  
  5. }   
  6. .frontPageTurner1 {  
  7.     left: 215;  
  8.     -webkit-transform-origin: 0px 0px;  
  9.     z-index: 0.8;  
  10. }   
  11. .back1 {  
  12.     left: 57;  
  13.     -webkit-transform-origin: 100% 70%;  
  14.     -webkit-transform: rotateY(180deg);  
  15. }   
  16. .frontPageTurner2 {  
  17.     left: 215;  
  18.     -webkit-transform-origin: 0px 0px;  
  19.     z-index: -1;  
  20. }   
  21. .back2 {  
  22.     left: 80;  
  23.     -webkit-transform-origin: 100% 70%;  
  24.     -webkit-transform: rotateY(180deg);  
  25. }   
  26. body {  
  27.     -webkit-transform-style: preserve-3d;  
  28.     -webkit-perspective: 1000px;  
  29.     text-align: center;  
After that we will write the JavaScript code like this:
  1. <script type="text/javascript">  
  2.     var x = 10;  
  3.     var l = 10;  
  4.     var t = 10;   
  5.     function Show() {   
  6.         document.getElementById('light').style.width = "0px";  
  7.         document.getElementById('light').style.height = "0px";  
  8.         setTimeout("Show1()", 500);  
  9.     }  
  10.     function Show1() {  
  11.         if ((x < 260) && (l < 500)) {  
  12.             if (t < 150) {  
  13.                 document.getElementById('light').style.top = t + "px";  
  14.             }  
  15.             else {  
  16.                 document.getElementById('light').style.top = "120px";   
  17.             }  
  18.             document.getElementById('light').style.left = l + "px";  
  19.             document.getElementById('light').style.width = x + "px";  
  20.             document.getElementById('light').style.height = x + "px";   
  21.             x = x + 10;  
  22.             l = l + 10;  
  23.             t = t + 10;  
  24.             setTimeout("Show1()", 10);  
  25.         }  
  26.         else {  
  27.             x = 0  
  28.             l = 0;  
  29.             t = 0;   
  30.         }  
  31.     }  
  32.     function flip() {  
  33.         document.getElementById("frontPageTurnerpageTurner").style.webkitTransform = "rotateY(-180deg)";  
  34.         document.getElementById("backpageTurnermain").style.webkitTransform = "rotateY(0deg)";  
  35.     }   
  36.     function flop() {  
  37.         document.getElementById("frontPageTurnerpageTurner").style.webkitTransform = "rotateY(0deg)";  
  38.         document.getElementById("backpageTurnermain").style.webkitTransform = "rotateY(180deg)";  
  39.     }  
  40.     function flip1() {  
  41.         document.getElementById("frontPageTurnerpageTurner1").style.webkitTransform = "rotateY(-180deg)";  
  42.         document.getElementById("backpageTurner1").style.webkitTransform = "rotateY(0deg)";  
  43.     }   
  44.     function flop1() {  
  45.         document.getElementById("frontPageTurnerpageTurner1").style.webkitTransform = "rotateY(0deg)";  
  46.         document.getElementById("backpageTurner1").style.webkitTransform = "rotateY(180deg)";  
  47.     }  
  48.     function flip2() {  
  49.         document.getElementById("frontPageTurnerpageTurner2").style.webkitTransform = "rotateY(-180deg)";  
  50.         document.getElementById("backpageTurner2").style.webkitTransform = "rotateY(0deg)";  
  51.     }   
  52.     function flop2() {  
  53.         document.getElementById("frontPageTurnerpageTurner2").style.webkitTransform = "rotateY(0deg)";  
  54.         document.getElementById("backpageTurner2").style.webkitTransform = "rotateY(180deg)";  
  55.     }  
  56. </script> 
Here we will set the flip() and flop() function on the "Back" and the "Next" Button so the page will be turned when we click on these buttons.
 
Output
 
image4.gif
 
image5.gif
 
image6.gif