DailyCoding > Design

ImageLens – A jQuery plug-in for Lens Effect Image Zooming

Use this jQuery plug-in to add lens style zooming effect to an image
Author admin on Mar 28, 2011 67 Comments
Rate it    (Rated 3 by 35 people)
65,293 Views

I was searching a good jQuery plug-in for image zooming but couldn't find any which was matching my criteria. I need the lens effect while zooming images and it should work without much plumbing. So, I decided to create one and now sharing this with everyone.

What I need to use this?

  1. jQuery
  2. ImageLens plug-in – jquery.imagelens.js
  3. An image

How to use this?

Include jQuery and jquery.imageLens.js in you web page -

<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.imageLens.js" type="text/javascript"></script>

For default image lens just use imageLens as below. Note that it will automatically calculate actual size of image and start showing zooming -

$("#img_01").imageLens();

Note - If you are using IE 8 or lower than you will see a square lens

(hover on image to see the lens effect)

 

If you thumbnail  image is different from zoomed image then you can specify custom image source -

$("#img_03").imageLens({ imageSrc: "sample01.jpg" });

(hover on image to see the lens effect)

 

You can also change lens size -

$("#img_02").imageLens({ lensSize: 200 });

(hover on image to see the lens effect)

 

There are some more attributes you can adjust in the lens like border color and border size -

$("#img_04").imageLens({ borderSize: 8, borderColor: "#06f" });

(hover on image to see the lens effect)

Image courtesy http://www.smashingmagazine.com/

CSS | Html | Javascript | jQuery
 

Discussion

timoni On Mar 31, 2011 12:49 AM
This is awesome; thanks! I'd like to pair it with a before/after function, if possible. Would this be easy to do?

timoni On Mar 31, 2011 12:49 AM
This is awesome; thanks! I'd like to pair it with a before/after function, if possible. Would this be easy to do?

Mista Koo On Apr 5, 2011 07:17 AM
Square lens in Firefox 3.6.

Jess On Apr 5, 2011 07:58 AM
Wow!!!! Thank you. This is wonderful.


Ramakanth On Apr 5, 2011 09:02 AM
This is Awesome. Thanks for sharing.

Ibrahim Azhar Armar On Apr 5, 2011 01:00 PM
this is so brilliant, i was wondering if this could be possible using jQuery. and you have it developed, i am surely going to implement in my website. thanks a lot.

Andreas On Apr 5, 2011 01:26 PM
To bad it only works on elements that are inside a position relative element, cant think its a big problem to fix.

Karsten On Apr 6, 2011 03:41 AM
Hello,

that is a nice effect.

thank you!

Karsten from germany

Ramesh Soni On Apr 6, 2011 03:55 AM
@Mista, basically the browser should support CSS round corner (CSS3) for a circular lens else you will see a square lens.

Andreas On Apr 6, 2011 04:49 AM
@admin, removed some of your code ( 127 bytes smaller when packed ) and works the same: http://voigt.se/sand..../jquery.imageLens.js

also removed the image you create, feels that its not needed to keep the image after you got the Ratio from the image. Will also see if I cant get it to work with the problem I posted above.

Andreas On Apr 6, 2011 04:50 AM
Damn posted before I was done, you have a object you arent using: targetSize = target.size(); just as you know :)

Andy On Apr 6, 2011 06:23 AM
Could this be used in wordpress?

Andreas On Apr 6, 2011 06:52 AM
@Andy, yes its javascript ;)

ilana On Apr 6, 2011 12:32 PM
This is great! i'm just wondering how to turn zoom off? For instance, using the second example, the lens showing a second image, but not wanting the zoom feature....

thanks for your advice.

ilana

ilana On Apr 6, 2011 12:32 PM
This is great! i'm just wondering how to turn zoom off? For instance, using the second example, the lens showing a second image, but not wanting the zoom feature....

thanks for your advice.

ilana

ilana On Apr 6, 2011 12:51 PM
nevermind, i realized i just had to change the other image size, and adjust the positioning slightly (-4 px left and top)

Andy On Apr 6, 2011 02:51 PM
Thanks Andreas, but when I go to activate your plugin I get Wordpress > Error 'This plugin does not have a valid header' - I'm not a web-developer so I may be doing something wrong - wish I knew what as I'd love to use this amazing effect!

Andy On Apr 6, 2011 03:05 PM
Sorry, Ramesh not Andreas :)

ryan On Apr 6, 2011 11:45 PM
This is very cool. The round lens on this awesome, I wish that was supported in Firefox :(

@andreas brings up a good point, I have positioning issues too that I think are related to main image position not being relative.

@Andreas, what are you trying as a fix to that?

Andreas On Apr 7, 2011 02:15 AM
The quickest fix I can come up with is to set a position relative on the parent, or wrap a new element with a relative position, and change some of the code, done some tests: http://voigt.se/sandbox/imageLens/demo2.html but not fully happy with it.

Andreas On Apr 7, 2011 02:17 AM
ryan: Update to Firefox4, I have no problem with the result, but you can get it to work with -moz-border-radius and -webkit-border-radius(browser prefix)

stryju On Apr 8, 2011 01:26 AM
broken in chrome ( won't scroll the image within "lens" - pm me if You need help with that )

as for square lens thing - poor support, that's that.
You forgot about prefixes ( "-webkit-", "-moz-" ), so either add prefixes OR use jQuery cssHooks ( http://api.jquery.com/jQuery.cssHooks/ ) as it's a jQ plugin.


stryju On Apr 8, 2011 01:29 AM
oh, and in IE8 it's way too slow...

Jeppe On Apr 8, 2011 12:24 PM
Felt inspired, and recreated your imageLens plugin so that it has the round shape in IE7 and 8 as well.. Its not absolutely perfect yet, but feel free to fork it and what not.

http://goo.gl/KuIn4

Ramesh Soni On Apr 8, 2011 09:29 PM
@Jeppy, thanks for putting effort to make this plug-in better. I have included this in the updates.

Rian Ariona On Apr 10, 2011 09:49 AM
good work!! but when i tried to apply it, the lens is going to far from the cursor?? what should i do??

Jeppe On Apr 11, 2011 01:35 AM
@Ramesh
Updated my blog, the post is now located at http://blog.bobslaed....magelens-plugin.html

Andreas On Apr 11, 2011 02:20 AM
@Rian Ariona: Think its because you load dynamic content, put the offset code within the mousemove event so it reads everytime.

Rian Ariona On Apr 11, 2011 03:19 AM
Hhmm.. can you give me the example? :)

Ramesh Soni On Apr 11, 2011 04:38 AM
@Jeppe, new link modified in updates.

Andreas On Apr 11, 2011 06:02 AM
Ramesh, you can update my link also ;)

Rian Ariona look at my demo3 you will se the offset() code is called everytime, I had the same problem as you, thats why I moved the code into that event instead

http://voigt.se/sandbox/imageLens/demo3.html

Ramesh Soni On Apr 11, 2011 06:33 AM
@Andreas, your link has been updated. Thanks for your time and support :)

Ricardo Schmidt On Apr 20, 2011 06:04 AM
not working in Google Chrome 10.0.648.151. The lens is round, but it dont scroll... just the top left piece of image is showing anywhere the lens go.

Andreas On Apr 20, 2011 08:57 AM
@Ricardo: Can't recreate your problem not with 10.0.648.151 or with a later version of Chrome either.

fgh On Apr 24, 2011 11:05 PM
cvbcvb

jon On May 1, 2011 11:58 AM
Hi, love the effect. I am trying to add it to a site I am building but having problems, I have added all the code but the lens is not magnifying the image. I am new to jQuery/Javascript and struggling to understand why it's not working properly. Any suggestions would be appreciated. Thanks

Andreas On May 2, 2011 02:39 AM
@Jon have you resized the image you are trying to use the lens on you cant use it on a 100%-sized image, or you have to use the option "imageSrc" to say where the even bigger image exists.

jon On May 2, 2011 02:49 PM
@Andreas, that's where I was going wrong, obvious really! thanks, I appreciate you taking the time to reply.

aykak On May 3, 2011 12:40 AM
wow that is what I am in search and arrived here .. :)

Luke On May 6, 2011 11:29 PM
I'm also getting a square lens in FF 3.6 (Mac).

Andreas On May 8, 2011 05:49 AM
@Luke, look at the comments there are fixes for it

join On May 13, 2011 06:25 PM
????

Raghibsuleman On May 16, 2011 01:47 AM
a very good work thanks...

sadmicrowave On May 19, 2011 02:15 PM
This is great but my lens isn't zooming. The square (FF3.x) shows up and there is a slight offset but no zoom multiplier seems to be active (the 'zoomed' image is the same size as the original) what can I do? can I edit the source somewhere to change this manually? Please help as this is the best magnifier I have found so far!

Andreas On May 21, 2011 08:46 AM
@sadmicrowave look at my response to the @Jon a few comments above, you can also update the plugin to one of the other once to get a rounded lens on FF3.x

Michael On May 27, 2011 12:24 PM
@Andreas, great idea. It's not working on Google Chrome 11.0.696.71, Mac OS10.6 -- exact same problem description as @Ricardo Schmidt above. Safari 5 works brilliantly. in FF4 the "other lens properties" -- the 4th demo -- sometimes doesn't delete the lens on mouse out.

Michael On May 27, 2011 12:24 PM
@Andreas, great idea. It's not working on Google Chrome 11.0.696.71, Mac OS10.6 -- exact same problem description as @Ricardo Schmidt above. Safari 5 works brilliantly. in FF4 the "other lens properties" -- the 4th demo -- sometimes doesn't delete the lens on mouse out.

Djordje On May 27, 2011 05:46 PM
Awesome! Thank you :)

pityandfrida On Jun 3, 2011 03:39 AM
I have a problem with very big original images. IE8 is really slow and crashes after a while.. any suggestions?

Vi?t Coding On Jun 14, 2011 11:19 PM
That 's greate javascript effect ! I like it !

yassien On Jun 23, 2011 10:19 AM
MAn Thanks, But it's not Zooming......

Visual C# Kicks On Jul 13, 2011 02:50 PM
Recently had to implement something similar for work, definitely would have been a time saver

Jay-Arr On Jul 17, 2011 07:48 PM
Hello guyz. just want to ask how to disable the zooming if other button is being clicked. thanks

parkesepeti On Jul 20, 2011 02:09 AM
good job. Thanks

Yousef On Jul 22, 2011 02:52 AM
Thanks, work great.

in IE the lens and the original picture are not align correctly

Pankaj Rai On Aug 8, 2011 12:52 AM
Hi Experts,
If I am calling this effect using class than it will only working in IE. But not working with FF and Chrome.

$("#.zoom").imageLens({ lensSize: 200 });

<img src="pankaj.jpg" id="Img1" class="zoom" width="160" height="160" />

What I am doing wrong.

Andreas On Aug 15, 2011 06:01 AM
@Pankaj Rai: you are trying to find a id named ".zoom" youwant to change that to:

$(".zoom").imageLens({ lensSize: 200 });

Kathy On Aug 24, 2011 12:25 AM
Hi, wondering if anybody could help me out.

Some information about my website -
It contains 5 divs: a wrapper to center my site horizontally, a header, a left sidebar, a right content container, and a footer.
I'm trying to use the script on the right content container (float: right), if that has to do with anything.

Basically the image lens (the magnified part) does not show up where my cursor is. This changes depending on what size the browser window is or what resolution the screen of the computer I'm viewing it from is.

On my particular screen (1366 x 768), this is what I see - http://onzaracing.com/images/lens%20problem.png

Any ideas on how to fix this? Thanks in advance.

eirc On Sep 5, 2011 08:51 AM
A small suggestion about the way that the plugin gets initialized. Usually it's a good idea to add your scripts to the end of the page so they don't stall the page loading. So if you want to use a different image for the lens you have to either add ids and inline javascript or... you can use custom (valid) html5 data attributes. So your image tag looks like:

<img src="small_image.jpg" data-lens-url="big_image.jpg" />

and the script at the end:

$('img[data-lens-src]').each(function() {
$(this).imageLens({ imageSrc: $(this).data('lens-src') });
});

And the suggestion... this seems generic enough to be included in the script by either automatically selecting such an attribute for the lens image source or by including an "initializer" with the code above.

Also add the script to an online repo (github) for easier comments, issues and contributions.

iosif On Oct 14, 2011 03:04 AM
Hi guys,

What do you think about this plug-in?
http://plugins.jquery.com/project/iZoom

Alper A. On Oct 15, 2011 07:13 AM
Hello,

Can someone explain, how to integrate this js in to a wordpress theme?

I downloaded the code and uploaded jquery.imageLens.js in to js folder of my theme.
After that I added the codes in to header:
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.imageLens.js" type="text/javascript"></script>

But I dont know, where to put $("#img_02").imageLens({ lensSize: 200 }); code in my template.

Please, I need your help!
thanks!

sunny On Dec 9, 2011 09:42 AM
Can someone explain, how to integrate this js in to a wordpress theme?

I downloaded the code and uploaded jquery.imageLens.js in to js folder of my theme.
After that I added the codes in to header:
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.imageLens.js" type="text/javascript"></script>

But I dont know, where to put $("#img_02").imageLens({ lensSize: 200 }); code in my template.

Sophie On Dec 20, 2011 08:49 AM
Hi, when I draw this image inside a canvas, it doesn't work. Any workaround? Thank you for the code!

Javier On Dec 28, 2011 06:29 AM
Hi! Excellent and very useful effect as well. A query: You can zoom in on a background image defined in a .css? (eg -> background: #fff url(image.jpg) no-repeat;).

Thank you very much and good year 2012! Greetings from Argentina.

Kru On Feb 2, 2012 08:42 AM
Brilliant stuff!

If you want to stop the zoom because you have dynamically changed the picture or you have multiple pictures then you can do the following:

jquery.imageLens.js file > add id field

// Creating lens
var target = $("<div style='" + lensStyle + "' id='zoom_div' class='"

You can then stop the zoom by adding

$("#zoom_div").remove();

anon On Feb 4, 2012 03:29 AM
Hey,

Thanks for this great plugin. I have tried it on a background image and it doesn't seem to work - is it even possible to use it on a background image?

Thanks in advance,

non On Mar 2, 2012 11:05 AM
this is great but i have a question, will i be able to use this if i am applying it to a data binded image?

Leave a Comment

Name
Email Address
Web Site