How to Create a Magnifying Lens of Image using jQuery and Magnify.js

Getting Started

First, we need the jQuery library and the Magnify CSS and JS. I've included these files in the downloadable of this tutorial but if you want, you can download them yourself using the links below: For jQuery For Magnify ->Github Repo

Displaying our Image

Next, we display our image that we want to add the magnify functionality. Take note that in order for this function to work, we will be using two images with same aspect ratio, one smaller and one larger. In this tutorial, I've also included sample images in the downloables that you can use. Create a new file, name it as index.html and paste the codes below.
  1. <!DOCTYPE html>
  2.         <meta charset="utf-8">
  3.         <title>How to Create a Magnifying Lens of Image using jQuery and Magnify.js</title>
  4.         <link rel="stylesheet" type="text/css" href="magnify/magnify.min.css">
  5.         <style type="text/css">
  6.                 /*resize magnifying lens*/
  7.                 .magnify > .magnify-lens {
  8.                         width: 100px;
  9.                         height: 100px;
  10.                 }
  11.         </style>
  12. </head>
  13. <img src="pc_smaller.jpg" class="zoom" data-magnify-src="pc_larger.jpg">
  14.  
  15. <script src="jquery.min.js"></script>
  16. <script src="magnify/magnify.min.js"></script>
  17.         $(document).ready(function() {
  18.                 $('.zoom').magnify();
  19.         });
  20. </body>
  21. </html>
Note: We have called the magnify() function in our script using.
  1. $(document).ready(function() {
  2.        $('.zoom').magnify();
  3. });
That ends this tutorial. Happy Coding :)

Add new comment