Code Monkey home page Code Monkey logo

magnify's Introduction

jQuery Magnify

Magnify is a simple, lightweight jQuery plugin that adds a magnifying glass style zoom functionality to images. It is a useful feature to have for product images on ecommerce websites, or if you just want people to be able to zoom into an image without spawning additional overlays or popup windows that may cover your content. Magnify is based on this tutorial.

If you don't use jQuery, then you can use TrySound's vanilla JS version.

See a demo »

See a demo with mobile plugin »

See a demo inside an accordion »

See a demo with background image »

See a demo with CSS animation »

See a demo with an image map »

Getting Started

Step 1: Link the required files

<link rel="stylesheet" href="/css/magnify.css">
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="/js/jquery.magnify.js"></script>
<!-- Optional mobile plugin (uncomment the line below to enable): -->
<!-- <script src="/js/jquery.magnify-mobile.js"></script> -->

You have complete control over the style and size of the lens by modifying magnify.css. Magnify has support for touch devices, but for a better zoom experience you can load the optional mobile plugin by uncommenting the last line above. It is recommended to load the JavaScript files at the bottom just before the closing </body> tag if possible.

UPDATE: You can now load Magnify from CDNJS.

Step 2: Specify the large image

The URI to the large image can be placed in the data-magnify-src attribute as shown below, or passed as the src option when calling the .magnify() function(see Options).

<img src="/images/product.jpg" class="zoom" data-magnify-src="/images/product-large.jpg">

If the data-magnify-src attribute or src option is not used, then Magnify will try to grab the large image from the parent <a> tag. Example:

<a href="/images/product-large.jpg">
  <img src="/images/product.jpg" class="zoom">
</a>

NOTE: The large image needs to have the same aspect ratio as the main image.

Step 3: Call the .magnify() function

Make sure this comes after the two required JavaScript files from Step 1 are loaded.

<script>
$(document).ready(function() {
  $('.zoom').magnify();
});
</script>

Calling the .magnify() function with options:

<script>
$(document).ready(function() {
  $('.zoom').magnify({
    speed: 200,
    src: '/images/product-large.jpg'
  });
});
</script>

Options

The options below can be set in a JavaScript object when calling .magnify().

Name Type Default Description
src string '' URI of the large image that will be shown in the magnifying lens.
speed number 100 Fade-in/out animation speed in ms when the lens moves on/off the image.
timeout number -1 Wait period in ms before hiding the magnifying lens on touch devices. Set to -1 to disable.
touchBottomOffset number 0 Vertical touch point offset. Set this to something like 90 if you want to avoid your finger getting in the way of the magnifying lens on smartphones and tablets.
finalWidth number Width of the main image. Set this only if the image animates into view and has a different initial width. If the image doesn't animate, then you should set the image width in CSS or via the width attribute.
finalHeight number Height of the main image. Set this only if the image animates into view and has a different initial height. If the image doesn't animate, then you should set the image height in CSS or via the height attribute.
magnifiedWidth number Width of the image displayed inside the magnifying lens. Set this only if you want to override the large image's native width.
magnifiedHeight number Height of the image displayed inside the magnifying lens. Set this only if you want to override the large image's native height.
limitBounds boolean false Set this to true to keep the edge of the image within the magnifying lens.
mobileCloseEvent string 'touchstart' Custom event to fire when you tap on the mobile close button. Set this to 'click' or 'touchend' if it's conflicting with another event handler. This option is only applicable when the mobile plugin (jquery.magnify-mobile.js) is used.
afterLoad function Anonymous callback function to execute after magnification is loaded.

Options can also be set directly in the <img> tag by adding the following data attributes, which will take precedence over the corresponding options set inside an object:

  • data-magnify-src - equivalent to src
  • data-magnify-speed - equivalent to speed
  • data-magnify-timeout - equivalent to timeout
  • data-magnify-touchbottomoffset - equivalent to touchBottomOffset
  • data-magnify-finalwidth - equivalent to finalWidth
  • data-magnify-finalheight - equivalent to finalHeight
  • data-magnify-magnifiedwidth - equivalent to magnifiedWidth
  • data-magnify-magnifiedheight - equivalent to magnifiedHeight
  • data-magnify-limitbounds - equivalent to limitBounds
  • data-magnify-mobilecloseevent - equivalent to mobileCloseEvent
  • data-magnify-afterload - equivalent to afterLoad, except the value must be a declared function name

Methods

Name Description
destroy() Disable zoom and reset to the original state.

To use a public method, you need to assign the element that you called .magnify() on to a variable. Example:

<script>
$(document).ready(function() {
  // Enable zoom
  var $zoom = $('.zoom').magnify();
  // Disable zoom
  $zoom.destroy();
});
</script>

Events

Magnify triggers two custom events on the html element: magnifystart when you enter zoom mode and magnifyend when you exit zoom mode. Example:

$('html').on({
  magnifystart: function() {
    console.log('magnifystart event fired');
  },
  magnifyend: function() {
    console.log('magnifyend event fired');
  }
});

When in zoom mode, the magnifying class is also added to the <html> tag, so you can change the style while zooming.

Lens Style

The lens style can be altered by overriding .magnify > .magnify-lens. Example:

/* Shrink the lens to half size */
.magnify > .magnify-lens {
  width: 100px;
  height: 100px;
}

Installation

Choose from one of the following methods:

  • git clone [email protected]:thdoan/magnify.git
  • git clone https://github.com/thdoan/magnify.git
  • bower install magnify
  • npm install magnify
  • Download ZIP

magnify's People

Contributors

crydotsnake avatar drsdavidsoft avatar gpetrioli avatar makc avatar maruilian11 avatar superelement avatar thdoan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

magnify's Issues

Angular Data Binding is not working

Am trying this magnify in angular web application. this is not working for angular data-binding in large image

this is my code (not working)

        <div class="g-spr-feat">
            <img id="sprFeatImg" src="{{currentImage}}" alt="hob" class="zoom" data-magnify-src="{{currentImage}}">
        </div>

If i give direct path in data-magnify-src, Its working

changing magnifiedWidth/Height causes image in lens to move out of view

I have a function that allows the user to adjust the zoom using the mouse wheel - it updates the magnifiedWidth and magnifiedHeight properties. The magnify-lens bg-size property updates on the fly, but the background position does not, so the image in the lens moves diagonally out of view when increasing the zoom. Simply moving the lens a tiny bit causes the background position to update correctly, but it would be nice if it would update while the bg-size is being changed.

Re-initialize zoom after destroy

Hello ;)

Is there a way/method to reactivate the zoom after it has been destroyed?
I looking to implement for an off/on option.

Thank you so much!

Not works in flexbox

Hi!

I installed your script as you described. I am using flexbox for my products.
When I hover the image, nothing happens. But when I click on my login button, and a popup appeared, and move the mouse at the right side of the image, then magnifier starts to work but at bad position of the page.

I am using jQuery Mobile for my site, this is why I mentioned the popup thing.

jQuery not defined when using with Create React App

Hey, cool stuff.

Any tip on getting this to work in a create-react app?

I've installed the Magnify package via NPM and am trying to use imports. I'm seeing this error when the build crashes, and thinking I may need to be attaching jQuery to the window object or something:

./node_modules/magnify/dist/js/jquery.magnify.js
node_modules/magnify/dist/js/jquery.magnify.js:348
  345 |       init(this);
  346 |     });
  347 |   };
> 348 | })(jQuery);
``console

Not working on Android 7

Nice, configurable magnifier, easy to install and set up. Jan 2018 works great on desktop and iOS but not functioning on my Android 7. Draws the circle but the image within is not amplified.

VanillaJS

I would like to use magnify bower name. Create please new branch for revision my vanillajs version and I'll make PR

Magnify zoomIn is too big

Hi,
I have a problem with Chrome and Firefox. In safari it works just fine.
I have a Magnifier in use and have a img-container with fixed height of 350px. In there is a img with max-width and max-height of 100%. Then I magnify this image. The problem is now for Chrome and Firefox that the magnify class is bigger than my 350px img-container. Than I tried to also give the magnify class a width and height of 100%. But than the Lenses work as the image would still be bigger so it does not really fit.
Can you help here? Not sure how to fix that but I love the lenses :)
Here you can watch in different browsers: https://hyphyweb-development.herokuapp.com/products/31/adidasxyz

Show zoom div on different position of screen

Hello.
Is it possible to show the zoomed div in another position of the body of html?

for example upper right of the screen independently of where the user has began dragging his mouse or finger etc.

And also could it zoom any object or only img? For example any other div or text etc.
Could you point me out any solution? Thanks a lot!

limitBounds improvement request

Hey mate,

Thanks for the limitBounds feature a few weeks back. It works a treat! What I'm finding now is, trying to view the edge of an image has become this fine balancing act so the magnify glass doesn't hide. Would it be possible to have a flag that can keep the magnify glass in view until the mouse moves off the image completely?

If the magnify glass can stay in view with limitBounds but not disappear until the mouse leaves the image. I hope that makes sense.

Cheers

Resize or rotate - how to re-initialize?

This works great! But if I change the size of the window, or rotate (landscape to/from portrait orientation), the cursor and magnifier are offset.
I added a $(window).resize function:
<script>
$(window).resize(function() {
// Initiate magnification powers
$('.zoom').init();
$('.zoom').magnify();
});
</script>
which works fairly well, but often on mousemove/touchmove, the magnifier "blinks" as it moves (visible then invisible then visible), and it seems more "jerky" on an iPad when this happens.

Using on images that move based on div expanding etc issue

Great plugin!!!

I am using on an image that is below accordions and it gets pushed down if you open an accordion and then messes up the alignment of the loop or zoom and it is not aligned with the cursor anymore. Any ideas how to fix?

relative vs. absolute mouse position for elementFromPoint for usemap

We love this plugin, and are using it in the middle of a page, after a banner, menu bar, etc.
We found that the image map was not getting clicks like we'd expect.
I changed the lines in this block:
"$container.click(function(e) {
// Trigger click on image below lens at current cursor position"
to use clientX and clientY vs. pageX and pageY, and that solved the problem.
I wasn't too familiar with this but this note was helpful: http://www.zehnet.de/2010/11/19/document-elementfrompoint-a-jquery-solution/ .
Thank you!

limit bounds on magnify glass

Hi there,

Awesome library first up!

I wanted to ask if it was possible to restrict the 'bounds' of the magnify glass so i wouldn't go past the edge of an image. Example:

image

Preferred Result:

image

I wasn't 100% sure if this could be done with the recent update or not.

Cheers

Lens gets stuck if mouseleave is too fast

Hi there,

Great plugin, thanks for creating it. 👍

I noticed a small issue: if you move the mouse out of the container fast enough, the lens gets stuck (doesn't fade).

Thank you!

Contributing

Hi! Cool plugin.
Do you use some builder? Or write everything in dist?

Option to turn it off?

With multiple images on the page I would like to enable it and disable it for a given image. Is there a way to disable the magnifier after it's been initialized. Do you just set the timeout to -1 to turn it off?

Actually tested the timeout to -1 and it doesn't work. You need like a kill/destroy option.'

Ok, looked at source code and tried $(".magnify").off and seems to work but it doesn't completely remove itself as the checkbox I have positioned over the image hasn't reappeared, as I think it does something to manipulate z index.

magnify with owl carousel!

Dear thdoan,

i am using magnify.js on a slider, owl-carousel in shopify.
It works perfectly on the first image, but as soon as i slide to the second image, the lens doesn't appear.

i checked and .zoom & data-magnify-search="" are there, in all the images.

i attached the code in product page:

    {% for image in product.images %}
  • {{ image.alt | escape }}
  • {% endfor %}

and in theme:

{% if template == "product" %}
{{ 'jquery.magnify.js' | asset_url | script_tag }}
{{ 'jquery.magnify-mobile.js' | asset_url | script_tag }}

<script>

$(document).ready(function() {
$('.zoom').magnify({
speed: 200,
});
});
</script>

{% endif %}

Could you help?
How do i need to reinit the magnifier to work on every image?
I hope this is not something odd, what i am saying.
Thanks for your patience!

Andrea

screen shot 2016-09-06 at 09 30 47

I need your help please

Hello, can you explain to me how to make the plugin works only on a single image? Because the plugin load on all my images from my website.

Thank you, wishing you a pleasant day. Sorry for my bad English.

Initialize on click

Great Work, thank you for this library.

I have multiple images where they changes based on thumbnails clicking. The first image is working good since it is the selected by default and magnify() is initialized on window load. However, when you click on new image I try to reinitialize magnify but no luck.

Any help would be appreciated.

Thanks

Not working as expected

Hi

First of all very beautiful plugin. I really liked it.

I am integrating this plugin with the woocommerce so that user can see product with magnify effect.

For that, I have added one more field in to the back end to insert Large Image and then adding data-magnify-src to the <a> and it is working perfect.

But issue occurs when Large image is not uploaded.

ISSUE

Issue is when Large image is not uploaded at that time,it should take the image SRC of the parent <a> as mentioned but if I inspect element, I can see that it is taking the permalink from the URL. What might be the isuse here ?

temporary1

Doesn't work with Bootstrap 4

Magnify with Bootstrap 4

Hi thdoan.
Unfortunately magnify-mobile.js doesn't work with boostratp 4.

Can you have any tips?

best regards
Daniel

Question not a bug

Mobile only offset touch point or in this case the user finger to be at the bottom not in the center so the zoom viewable area is clear from obstruction.

I hope this makes sense to anyone.

Strange issue

Hello,

I am using your package for over a year now and everything works perfect!
But the things i just started with capturing client js errors in Sentry.

Now i get almost every hour the following errors:

TypeError
Unable to get property '0' of undefined or null reference

"magnifying").trigger("magnifyend")})},k=function(t){if(p){if(t?(t.preventDefault(),nPosX=t.pageX||t.originalEvent.touches[0].pageX,nPosY=t.
TypeError
undefined is not an object (evaluating 't.originalEvent.touches[0]')

?(t.preventDefault(),nPosX=t.pageX||t.originalEvent.touches[0].pageX,nPosY=t.pageY||t.originalEvent.touches[0].pageY,o.data("lastPos",{x:nPo

Do you have any idea where to look?

Tried in every modern browser but can't replicate it :(

imagemap demo question

Hello. On the new image map ability demo, I see I the click being sent to the image map and then we head to say the Google site like we should. Then if I hit the back button, the lens is in the position where I'd left it (until I place the mouse on the image again) and the page no longer registers clicks unless I refresh the page. This behavior is on Safari.
On Firefox, the click on the image map actually doesn't seem to register at all.
On Chrome, the click on the image map does send me to the proper links, and after hitting the Back button, the image map still responds to clicks normally. So in summary:
Chrome - works well including after leaving the page and returning via Back button
Safari - works on first click to new page, but not after returning
Firefox - doesn't register the first click
Thank you.

Cant scroll down when magnify is on full screen image.

Im using magnify on large images on my site. It works well, but on mobile, the lens appears when i try and drag the page down, instead of the page scrolling down. Id rather just have a tap to enlarge on mobile without the lens, which Im hoping will sort the scrolling issue. Is that possible?

Slick slider overflow hidden cuts the lens in the edge of the active slide.

Hey guys,

i like your plugin and it works so far really great.
But now i tried to use this in a slick slider and having a problem that the lens gets cut at the edge because slick needs to use "overflow: hidden" for the slick-list to hide all other slides which are not active.
It would be great if the plugin could create the elements under the body element to prevent overflow hidden problems.

lens cutted

What do you think?

best regards

Possible to integrate with a "usemap" behavior?

Hello, we love this plugin, but are stumped now trying to add a usemap behavior:

<img id="magnify-pace" src="images/pace_interactive_v1_smaller.png" alt="Sample image" class="zoom img-responsive" data-magnify-src="images/pace_interactive_v1_larger.png" usemap="#interactive" width="3000" height="1536"> <map name="interactive"> <area shape="circle" coords="195,520,130" href="/learn_v2.cgi#aerosols"> <area shape="circle" coords="2410,950,130" href="/learn_v2.cgi#aerosoloptical"> </map>

We'd like to go to a second page when clicking in the map shapes, but aren't able to figure it out so far.
We've tried to add some code in jQuery.magnify.js like:

$lens.attr('usemap','#interactive');

but it doesn't "enable" the usemap behavior. I tried to add an onclick on the lens, sending the click on to the unmagnified img with the usemap:

$("#magnify-pace").click();

but that isn't effective either. Any advice would be appreciated! Thank you.

Firefox Image Map Click Through - Possible Fix

Currently, the image map demo click through doesn't work in Firefox..it requires two clicks. I had the need to do a click through and found a method that works in Firefox (as well as Chrome and Edge) to fire a click at a specific x,y:

function clickxy(x, y) {
    var ev = new MouseEvent('click', {
        'view': window,
        'bubbles': true,
        'cancelable': true,
        'clientX': x,
        'clientY': y
    });
    var el = document.elementFromPoint(x, y);
    el.dispatchEvent(ev);
} 

The close button on mobile uses touchstart event

I have a problem with the close button on mobile. Because it gets handled by "touchstart" event, when I "touchend" (lift my finger off the screen) it triggers an event for the element beneath the magnified image (in my case the burger menu).
It would be good if the event could be overridden with another event, that way I could use "touchend" or "click" to solve this problem.

Can't Zoom Images using HandlebarsJs Templating

I would like to know if it is possible to zoom on pictures that are being passed to client side using HandlebarsJS template system. I have an image I passed using a product's id and would like for it to zoom when hovered over. So far I have not been successful and followed the instructions which cover if the images are explicitly .jpg in a folder.

Any guidance would help!

Click Through w/o Image Map - Enhancement Idea

It would be nice to have an option to turn on click through without using an image map. I was able to do it myself with an afterLoad function, but it would have been useful to have an a clickThrough option that could be set to true.

The mobile zoom is not working in iOS

Strangely enough in the demo about mobile zoom of magnify on iOS devices is working, but on my website it isn't. And I don't think I did it wrong bc on Android devices the mobile zoom IS working.

I already read about the problem on iOS and the click or the touchstart, but if you know a way to fix this (or to tell me what's the problem) I'll be very grateful

Add image background to lens

Hi,

I'm wondering if there's a chance to add a transparent image to the magnify-lens or magnify div, in order to re-create a "magnify lens" look. Maybe adding it through background-image ?

Thanks!

How to disable/destroy the magnifier?

I want to create a UI for toggling the magnify functionality off and on. Turning it on is easy enough, since that's what the magnify function does. What's the best way to turn it off?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.