{"id":578,"date":"2024-01-27T01:12:48","date_gmt":"2024-01-26T19:42:48","guid":{"rendered":"http:\/\/codetheory.in\/?p=578---201b6f44-1f77-440c-9abf-058944568ace"},"modified":"2024-01-27T01:12:48","modified_gmt":"2024-01-26T19:42:48","slug":"image-zoom-magnifying-glass-effect-with-css3-and-jquery","status":"publish","type":"post","link":"https:\/\/codetheory.in\/image-zoom-magnifying-glass-effect-with-css3-and-jquery\/","title":{"rendered":"Image Zoom Magnifying Glass Effect with CSS3 and jQuery"},"content":{"rendered":"

\"\"<\/p>\n

Ever wanted to add a magnifying glass effect to your product images for an awesome zoom-in and a great user experience ? You’ll be surprised to know that using CSS3 and jQuery, it’s really easy to integrate this effect to your website or webapp.<\/p>\n

<\/p>\n

Demo First<\/h2>\n
<\/pre>\n

Let’s Start<\/h2>\n

Well, the entire idea is based on this Walkthrough from TCP<\/a>. I just had the time to convert the code from the walkthrough into a dead simple plugin and that’s what I would like to talk about. Although the walkthrough does an excellent job at explaining how things work, I would still like to quickly skim over the basic concept in the plugin’s context, that I developed.<\/p>\n

The Logic<\/h2>\n

The basic idea is to have a large image, say 800×400 that we scale down to say 400×200 using the width<\/code> html attribute or the CSS property. Having the class magniflier<\/code> is important, as it is what our Javascript code is going to check for. The simple markup:<\/p>\n

\r\n<img class="magniflier" src="http:\/\/mysite.com\/myimage.png" width="400" \/>\r\n<\/pre>\n

Then on mousehover<\/code> we bring on a magnifying glass over the image. The glass effect is simply achieved with basic CSS3 shadows and rounded corners:<\/p>\n

\r\n.glass {\r\n  width: 175px;\r\n  height: 175px;\r\n  position: absolute;\r\n  border-radius: 50%;\r\n  cursor: crosshair;\r\n\r\n  \/* Multiple box shadows to achieve the glass effect *\/\r\n  box-shadow:\r\n    0 0 0 7px rgba(255, 255, 255, 0.85),\r\n    0 0 7px 7px rgba(0, 0, 0, 0.25),\r\n    inset 0 0 40px 2px rgba(0, 0, 0, 0.25);\r\n\r\n  \/* hide the glass by default *\/\r\n  display: none;\r\n}\r\n<\/pre>\n

The glass is initially created using jQuery which is basically just a div.glass<\/code> element. It remains hidden until the mouse is hovered over the image, which is when it appears using the jQuery fadeIn<\/code> method. It is interesting to note that if we use 10 images, then we’ll only have 1 glass element instead of 10s. This is how we create the element dynamically:<\/p>\n

\r\n\/\/ Add the magnifying glass\r\nif ($('.magniflier').length) {\r\n  var div = document.createElement('div');\r\n  div.setAttribute('class', 'glass');\r\n  var glass = $(div); \/\/ For later reference\/usage\r\n\r\n  $('body').append(div);\r\n}\r\n<\/pre>\n

Remember<\/strong>, when the mouse is hovered over an image, the glass’s background-image<\/code> is set to the original image<\/em> that has larger dimensions compared to our scaled down version or you can also specify a different image in the data-large<\/code> attribute if you would like to:<\/p>\n

\r\n<img class="magniflier" src="http:\/\/mysite.com\/myimage.png" data-large="http:\/\/mysite.com\/mylargeimage.png" \/>\r\n<\/pre>\n

It is important to calculate the dimensions of the large version of the image used for the zoomed-in preview. I have explained how to do it in one of my earlier posts<\/a>. The calculated width and height are stored in native_width<\/code> and native_height<\/code> respectively.<\/p>\n

When you have the exact size of the larger version, all you need to do is use a simple little formula to calculate the proper background-position<\/code> offsets for the magnifying glass based on the mouse positions.<\/p>\n

\r\n  var magnify = function(e) {\r\n\r\n    \/\/ The background position of div.glass will be\r\n    \/\/ changed according to the position\r\n    \/\/ of the mouse over the img.magniflier\r\n    \/\/\r\n    \/\/ So we will get the ratio of the pixel\r\n    \/\/ under the mouse with respect\r\n    \/\/ to the image and use that to position the\r\n    \/\/ large image inside the magnifying glass\r\n\r\n    var rx = Math.round(mouse.x\/cur_img.width()*native_width - ui.glass.width()\/2)*-1;\r\n    var ry = Math.round(mouse.y\/cur_img.height()*native_height - ui.glass.height()\/2)*-1;\r\n    var bg_pos = rx + "px " + ry + "px";\r\n\r\n    \/\/ Calculate pos for magnifying glass\r\n    \/\/\r\n    \/\/ Easy Logic: Deduct half of width\/height\r\n    \/\/ from mouse pos.\r\n\r\n    var glass_left = e.pageX - ui.glass.width() \/ 2;\r\n    var glass_top  = e.pageY - ui.glass.height() \/ 2;\r\n\r\n    \/\/ Now, if you hover on the image, you should\r\n    \/\/ see the magnifying glass in action\r\n    ui.glass.css({\r\n      left: glass_left,\r\n      top: glass_top,\r\n      backgroundPosition: bg_pos\r\n    });\r\n\r\n    return;\r\n  };\r\n<\/pre>\n

The formula to calculate the position of the magnifying glass based on the mouse position (relative to the document) was easy. All we had to do is deduct half of the width and height of the glass from the mouse positions and use those values as the left and top offsets of the glass.<\/p>\n

But the key part<\/strong> of this entire thing, is the formula to calculate the background’s position for the glass that places the large preview of the image properly, leading to the magnification. Let’s break that down:<\/p>\n

\r\n\/\/ Breakdown of:\r\n\/\/ var rx = Math.round(mouse.x\/cur_img.width()*native_width - ui.glass.width()\/2)*-1;\r\n\/\/ var ry = Math.round(mouse.y\/cur_img.height()*native_height - ui.glass.height()\/2)*-1;\r\n\/\/ var bg_pos = rx + "px " + ry + "px";\r\n\r\n\/\/ The background position of div.glass will be changed\r\n\/\/ according to the position of the mouse over img.magniflier\r\n\r\n\/\/ So, first lets get the ratio of the pixel\r\n\/\/ under the mouse pointer with respect to the image\r\nvar rx = mouse.x\/$("img.magniflier").width();\r\n\r\n\/\/ Corresponding pixel on the large image\r\nvar large_x = rx * native_width;\r\n\/\/ Centering the background inside the glass\r\nlarge_x = large_x - $('div.glass').width() \/ 2;\r\n\r\n\/\/ Finally, since we're specifying px offsets we'll\r\n\/\/ have to negate the x\/y offset\r\nlarge_x = large_x * -1;\r\n\r\n\/\/ Repeat the same for `large_y`\r\n\r\n\/\/ `large_x` will be the `left` offset\r\n\/\/ `large_y` will be the `top` offset\r\n<\/pre>\n

Read the comments<\/em> in the above code for proper understanding of the logic behind the formula.<\/p>\n

Boom! That’s all for our simple little feature. No one is stopping you from digging through the code in the Demo ;-).<\/p>\n

Usage<\/h2>\n
    \n
  1. Put the Javascript and CSS code in different files and load them using script<\/code> and link<\/code> tags.<\/li>\n
  2. Use the class magniflier<\/code> to enable the effect on any image you want to.<\/li>\n
  3. Use the data-large<\/code> attribute if you would like to use a different image for the zoomed version. So basically, the jquery code checks for img.attr('data-large')<\/code> with a fallback to img.attr('src')<\/code>.<\/li>\n<\/ol>\n

    and… we’re done! \\o\/<\/p>\n

    Final Words<\/h2>\n

    It was a quick work from me and I am sure the code can be improved massively. Feel free to use it and share any improvements that you make.<\/p>\n

    Now you have an uber simple-to-use magnifying glass plugin for your next project!<\/p>\n","protected":false},"excerpt":{"rendered":"

    Ever wanted to add a magnifying glass effect to your product images for an awesome zoom-in and a great user experience ? You’ll be surprised to know that using CSS3 and jQuery, it’s really easy to integrate this effect to your website or webapp.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[52,62,61],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/578"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=578"}],"version-history":[{"count":16,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/578\/revisions"}],"predecessor-version":[{"id":184675,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/578\/revisions\/184675"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}