Magnific Pop Up.js Implementation To Zoom In Pictures

We all would have come across a scenario where we would have wanted to see the full size image of a picture which was resized to fit in the web page. Ideally on clicking the image it navigates to the picture URI in a new page, where we get to see the full size image, and we have to hit the back button to go back to the original web page.

In this scenario Magnific Pop Up.js gives us the flexibility to open the embedded web page image as a full sized pop up by just clicking the image

Let’s see how we can implement this in a sample web page.

What all files do we have to refer in the page

CSS

  1. <link rel="stylesheet" type="text/css" href="https://rawgit.com/dimsemenov/Magnific-Popup/master/dist/magnific-popup.css">  
Magnific Pop up.js
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
JQuery:
  1. <script type="text/javascript" src="https://rawgit.com/dimsemenov/Magnific-Popup/master/dist/jquery.magnific-popup.js"></script>  
Place the above references within the head tag of the html file.

Place the following CSS inside the Style tag within the head section,
  1. <style>  
  2.     table {  
  3.         border-spacing0;  
  4.         width100%;  
  5.     }  
  6.       
  7.     td {  
  8.         border-bottom1px solid #eee;  
  9.         background#ddd;  
  10.         color#000;  
  11.         padding10px 25px;  
  12.     }  
  13.       
  14.     th {  
  15.         background#87CEFA;  
  16.         padding10px 25px;  
  17.     }  
  18.       
  19.     td + td {  
  20.         border-left1px solid #eee;  
  21.     }  
  22. </style>  
I had to create mark up so that the image and corresponding data is displayed as a table. Place the following html mark up within the body:
  1. <section class="">  
  2.     <div class="container">  
  3.         <table>  
  4.             <thead>  
  5.                 <tr class="header">  
  6.                     <th> National Flower </th>  
  7.                     <th> Country </th>  
  8.                     <th> Image </th>  
  9.                 </tr>  
  10.             </thead>  
  11.             <tbody>  
  12.                 <tr>  
  13.                     <td>Tulip</td>  
  14.                     <td>Peru</td>  
  15.                     <td>  
  16.                         <image class="FlowerLink" src="https://lh5.ggpht.com/gDsUvYasttZ9ucT9IiL6hCZO_miY_Z2Esc7fdP8bR-_up7U3ymvrwK1LollkEeo2oAgy=h900" height="100" width="100"></image>  
  17.                     </td>  
  18.                 </tr>  
  19.                 <tr>  
  20.                     <td>Lotus</td>  
  21.                     <td>India</td>  
  22.                     <td>  
  23.                         <image class="FlowerLink" src="http://withanopenheartdotorg.files.wordpress.com/2013/01/tumblr_lqf3n7sp3d1qbe09go1_1280_large.png?w=320&h=294" height="100" width="100"></image>  
  24.                     </td>  
  25.                 </tr>  
  26.                 <tr>  
  27.                     <td>Sunflower</td>  
  28.                     <td>Switzerland</td>  
  29.                     <td>  
  30.                         <image class="FlowerLink" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTo4nS-7mMfZlhUpwLqLBFtQGNwR4FIH4am2CMqeRMUtGN3peXw" height="100" width="100"></image>  
  31.                     </td>  
  32.                 </tr>  
  33.             </tbody>  
  34.         </table>  
  35.     </div>  
  36. </section>  
As a final step lets place the following jQuery code which would invoke the Magnific JS methods within the head tag right after the External JS file references.
  1. $(document).ready(function()  
  2. {  
  3.     $(".FlowerLink").each(function()  
  4.     {  
  5.         $(this).wrap("<a class=\"FlowerLinkWrapper\" href=\"" + $(this).attr('src') + "\"></a>");  
  6.     });  
  7.     $('.FlowerLinkWrapper').magnificPopup(  
  8.     {  
  9.         type: 'image',  
  10.         closeOnContentClick: true,  
  11.         image:  
  12.         {  
  13.             verticalFit: true  
  14.         },  
  15.         zoom:  
  16.         {  
  17.             enabled: true,  
  18.             duration: 800 // don't foget to change the duration also in CSS  
  19.         }  
  20.     });  
  21. });  
Code Walk-through
  1. $(this).wrap("<a class=\"FlowerLinkWrapper\" href=\"" + $(this).attr('src') + "\"></a>");  
  2. });  
Above line of code wraps the images within an anchor tag and associates a class with the images. This is necessary for the Magnific.js call to work.
  1. $('.FlowerLinkWrapper').magnificPopup({})  
This line of code associates the magnific pop up event to the earlier anchor tags that we had wrapped around the images. Thus whenever the image is clicked it would open up as a pop up and show the full size image.

Sample Screenshots

output

run

There are lot of properties that we can set for the ‘magnificPopup’ method call which gives us better transitions and zoom effects . You may feel free to play around with these properties at the jsfiddle I have created here

Up Next
    Ebook Download
    View all
    Learn
    View all