Image Gallery With CSS3 and JavaScript

Introduction

 
This example will be work properly in IE10, Firefox, Opera, Chrome and Safari.
 
IE9 and earlier versions of IE don't support the transition property used in the program.  
 
In this program, when we hover the mouse over an image, the image will be rotated on the 360 deg angle and the information regarding that image will be shown just like this:
 
1.gif
 
2.gif
 
Step 1: First we will use some images like this:
  1. <div id="MyGallery">  
  2.      <div id="img1">  
  3.          <img src="1.jpg" width="200px" height="200px" onmouseover="ShowImage1()">  
  4.          <a>First Baby</a>  
  5.      </div>  
  6.      <div id="img2">  
  7.          <img src="2.jpg" width="200px" height="200px" onmouseover="ShowImage2()">  
  8.          <a>Second baby</a>  
  9.      </div>  
  10.      <div id="img3">  
  11.          <img src="3.jpg" width="200px" height="200px" onmouseover="ShowImage3()">  
  12.          <a>Third Baby</a>  
  13.      </div>  
  14.      <div id="img4">  
  15.          <img src="4.jpg" width="200px" height="200px" onmouseover="ShowImage4()">  
  16.          <a>Forth Baby</a>  
  17.      </div>  
  18.      <div id="img5">  
  19.          <img src="5.jpg" width="200px" height="200px" onmouseover="ShowImage5()">  
  20.          <a>Fifth Baby</a>  
  21.      </div>  
  22.      <div id="img6">  
  23.          <img src="6.jpg" width="200px" height="200px" onmouseover="ShowImage6()">  
  24.          <a>Sixth Baby</a>  
  25.      </div>  
  26.      <div id="img7">  
  27.          <img src="7.jpg" width="200px" height="200px" onmouseover="ShowImage7()">  
  28.          <a>Seventh Baby</a>  
  29.      </div>  
  30.      <div id="img8">  
  31.          <img src="8.jpg" width="200px" height="200px" onmouseover="ShowImage8()">  
  32.          <a>Eight Baby</a>  
  33.      </div>  
  34.  </div> 
Step 2: Now we will use an image and a table to show the data when we hover our mouse over the image.
  1. <div id="img9">  
  2.      <img id="imgmain" style="display: none;" width="250px" height="250px">  
  3.      <br />  
  4.      <br />  
  5.      <br />  
  6.      <table id="table1" style="display: none;">  
  7.          <tr>  
  8.              <td style="font-size: Large; font-weight: bold; color: Brown; font-style: Italic;">Name :</td>  
  9.              <td style="font-size: Large; color: Brown;">  
  10.                  <p id="pname"></p>  
  11.              </td>  
  12.              <tr>  
  13.          <tr>  
  14.              <td style="font-size: Large; font-weight: bold; color: Brown; font-style: Italic;">Age :</td>  
  15.              <td style="font-size: Large; color: Brown;">  
  16.                  <p id="page"></p>  
  17.              </td>  
  18.              <tr>  
  19.          <tr>  
  20.              <td style="font-size: Large; font-weight: bold; color: Brown; font-style: Italic;">Mother's Name :</td>  
  21.              <td style="font-size: Large; color: Brown;">  
  22.                  <p id="pmothername"></p>  
  23.              </td>  
  24.              <tr>  
  25.          <tr>  
  26.              <td style="font-size: Large; font-weight: bold; color: Brown; font-style: Italic;">Father's Name :</td>  
  27.              <td style="font-size: Large; color: Brown;">  
  28.                  <p id="pfathername"></p>  
  29.              </td>  
  30.              <tr>  
  31.      </table>  
  32.  </div> 
Step 3: Now we will set the style of our gallery; first we set the style of the main div like this:
  1. #MyGallery {  
  2. margin: 20px auto;  
  3. padding: 40px;  
  4. position: relative;}   
  5. #MyGallery div {  
  6. background: pink;  
  7. position: absolute;  
  8. overflow: hidden;  
  9. opacity: 0.6;  
  10. transition: all 1500ms linear;  
  11. -webkit-transition: all 1500ms linear;   
  12. -webkit-border-radius: 20px;  
  13. -moz-border-radius: 20px;  
  14. border-radius: 20px;   
Here we will set the transition property like this:
 
-webkit-transition: all 1500ms linear;
 
It specifies that the transition effect duration will be 1500ms and the linear property specifies that the transition effect has the same speed from the start to the end.
 
Step 4: Now we will write the code for the rotation of the div like this:
  1. #MyGallery div#img1:hover,  
  2. #MyGallery div#img2:hover,  
  3. #MyGallery div#img3:hover,  
  4. #MyGallery div#img4:hover,  
  5. #MyGallery div#img5:hover,  
  6. #MyGallery div#img6:hover,  
  7. #MyGallery div#img7:hover,  
  8. #MyGallery div#img8:hover {  
  9. z-index: 999;   
  10. /* Safari */  
  11. -webkit-transform: rotate(360deg);   
  12. /* Firefox */  
  13. -moz-transform: rotate(360deg);   
  14. /* IE */  
  15. -ms-transform: rotate(360deg);   
  16. /* Opera */  
  17. -o-transform: rotate(360deg);   
  18. opacity: 1;}  
Here we will specify that when we put our mouse over an image it will be rotated 360 degrees. 
 
Step 5: Now we will write the code to set the position of the images like this. Here we use the rotate property to rotate the images at the specified angle like this:
  1. #MyGallery #img1 {  
  2. -webkit-transform: rotate(-10deg);  
  3. -moz-transform: rotate(-10deg);  
  4. top: 30px;  
  5. left: 100px;  
  6. z-index:1;}   
  7. #MyGallery #img2 {  
  8. -webkit-transform: rotate(10deg);  
  9. -moz-transform: rotate(10deg);  
  10. top: 60px;  
  11. left:300px;  
  12. z-index:3;}  
  13. #MyGallery #img3 {  
  14. -webkit-transform: rotate(40deg);  
  15. -moz-transform: rotate(40deg);  
  16. top: 90px;  
  17. left:410px;  
  18. z-index:6;}   
  19. #MyGallery #img4 {  
  20. -webkit-transform: rotate(15deg);  
  21. -moz-transform: rotate(15deg);  
  22. top: 120px;  
  23. left: 270px;  
  24. z-index:9;}   
  25. #MyGallery #img5 {  
  26. top: 180px;  
  27. left: 110px;  
  28. z-index:12;}   
  29. #MyGallery #img6 {  
  30. -webkit-transform: rotate(-15deg);  
  31. top: 190px;  
  32. -moz-transform: rotate(-15deg);  
  33. left: 360px;  
  34. z-index:14;}  
  35. #MyGallery #img7 {  
  36. -webkit-transform: rotate(15deg);  
  37. -moz-transform: rotate(15deg);  
  38. top: 210px;  
  39. left: 280px;  
  40. z-index:14;}  
  41. #MyGallery #img8 {  
  42. -webkit-transform: rotate(-15deg);  
  43. -moz-transform: rotate(-15deg);  
  44. top: 250px;  
  45. left: 500px;  
  46. z-index:14;}  
  47. #MyGallery #img9 {   
  48. top: 10px;  
  49. left: 850px;  
  50. z-index:14;} 
Step 6: Now we will write the JavaScript code for when the mouse hovers over the image the table will show the information:
 
<img src="1.jpg" width="200px" height="200px" onmouseover="ShowImage1()"> 
 
JavaScript code:
  1. <script language="javascript">  
  2.      function ShowImage1() {  
  3.          document.getElementById('table1').style.display = 'block';  
  4.          document.getElementById('imgmain').style.display = 'block';  
  5.          document.getElementById('imgmain').src = '1.jpg';  
  6.          document.getElementById('pname').innerHTML = 'Khyati Zindal';  
  7.          document.getElementById('page').innerHTML = '5';  
  8.          document.getElementById('pmothername').innerHTML = 'Anjana';  
  9.          document.getElementById('pfathername').innerHTML = 'Abhinav';  
  10.      }  
  11. </script> 
Here we set the display property of the table so it will be visible when we will put our mouse over the image. Here we also set the source of the image and set the data in the <p> tags. So the output will be:
 
3.gif
 
  1. <script language="javascript">  
  2.      function ShowImage1() {  
  3.          document.getElementById('table1').style.display = 'block';  
  4.          document.getElementById('imgmain').style.display = 'block';  
  5.          document.getElementById('imgmain').src = '1.jpg';  
  6.          document.getElementById('pname').innerHTML = 'Khyati Zindal';  
  7.          document.getElementById('page').innerHTML = '5';  
  8.          document.getElementById('pmothername').innerHTML = 'Anjana';  
  9.          document.getElementById('pfathername').innerHTML = 'Abhinav';  
  10.      }  
  11.      function ShowImage2() {  
  12.          document.getElementById('table1').style.display = 'block';  
  13.          document.getElementById('imgmain').style.display = 'block';  
  14.          document.getElementById('imgmain').src = '2.jpg';  
  15.          document.getElementById('pname').innerHTML = 'Megha';  
  16.          document.getElementById('page').innerHTML = '4';  
  17.          document.getElementById('pmothername').innerHTML = 'Girja';  
  18.          document.getElementById('pfathername').innerHTML = 'Yogesh';  
  19.      }  
  20.      function ShowImage3() {  
  21.          document.getElementById('table1').style.display = 'block';  
  22.          document.getElementById('imgmain').style.display = 'block';  
  23.          document.getElementById('imgmain').src = '3.jpg';  
  24.          document.getElementById('pname').innerHTML = 'Mahak Garg';  
  25.          document.getElementById('page').innerHTML = '4';  
  26.          document.getElementById('pmothername').innerHTML = 'Manju';  
  27.          document.getElementById('pfathername').innerHTML = 'Arun';  
  28.      }  
  29.      function ShowImage4() {  
  30.          document.getElementById('table1').style.display = 'block';  
  31.          document.getElementById('imgmain').style.display = 'block';  
  32.          document.getElementById('imgmain').src = '4.jpg';  
  33.          document.getElementById('pname').innerHTML = 'Abhinav';  
  34.          document.getElementById('page').innerHTML = '3';  
  35.          document.getElementById('pmothername').innerHTML = 'Manju';  
  36.          document.getElementById('pfathername').innerHTML = 'Arun';  
  37.      }  
  38.      function ShowImage5() {  
  39.          document.getElementById('table1').style.display = 'block';  
  40.          document.getElementById('imgmain').style.display = 'block';  
  41.          document.getElementById('imgmain').src = '5.jpg';  
  42.          document.getElementById('pname').innerHTML = 'Meghal';  
  43.          document.getElementById('page').innerHTML = '5';  
  44.          document.getElementById('pmothername').innerHTML = 'Kusum';  
  45.          document.getElementById('pfathername').innerHTML = 'Anil';  
  46.      }  
  47.      function ShowImage6() {  
  48.          document.getElementById('table1').style.display = 'block';  
  49.          document.getElementById('imgmain').style.display = 'block';  
  50.          document.getElementById('imgmain').src = '6.jpg';  
  51.          document.getElementById('pname').innerHTML = 'Pratyaksh';  
  52.          document.getElementById('page').innerHTML = '3';  
  53.          document.getElementById('pmothername').innerHTML = 'Rekha';  
  54.          document.getElementById('pfathername').innerHTML = 'Narendra';  
  55.      }  
  56.      function ShowImage7() {  
  57.          document.getElementById('table1').style.display = 'block'; document.getElementById('imgmain').style.display = 'block';  
  58.          document.getElementById('imgmain').src = '7.jpg';  
  59.          document.getElementById('pname').innerHTML = 'Miya';  
  60.          document.getElementById('page').innerHTML = '3';  
  61.          document.getElementById('pmothername').innerHTML = 'Kate';  
  62.          document.getElementById('pfathername').innerHTML = 'John';  
  63.      }  
  64.      function ShowImage8() {  
  65.          document.getElementById('table1').style.display = 'block';  
  66.          document.getElementById('imgmain').style.display = 'block';  
  67.          document.getElementById('imgmain').src = '8.jpg';  
  68.          document.getElementById('pname').innerHTML = 'Nidhi';  
  69.          document.getElementById('page').innerHTML = '4';  
  70.          document.getElementById('pmothername').innerHTML = 'Nimi';  
  71.          document.getElementById('pfathername').innerHTML = 'Rakesh';  
  72.      }   
  73. </script> 


Similar Articles