Magnify

Magnify is a jQuery plugin to view images just like in Windows.

View on GitHub Download

Examples

All the pictures come from Flickr website, copyright to the original author. You can play the following example by changing themes and options in the playground. Besides, examples collection can be found on CodePen.

Options Generator
{...}
{...}
{...}

Usage

The usage of magnify is very simple.

1.Include files

<link href="/path/to/magnify.css" rel="stylesheet">

<script src="/path/to/jquery.js"></script>
<script src="/path/to/jquery.magnify.js"></script>

The icons in magnify use svg default, you can customize them in options.

2.Html structure

The default structure as below:

<a data-magnify="gallery" href="big-1.jpg">
  <img src="small-1.jpg">
</a>
<a data-magnify="gallery" href="big-2.jpg">
  <img src="small-2.jpg">
</a>
<a data-magnify="gallery" href="big-3.jpg">
  <img src="small-3.jpg">
</a>

or

<img data-magnify="gallery" data-src="big-1.jpg" src="small-1.jpg">
<img data-magnify="gallery" data-src="big-2.jpg" src="small-2.jpg">
<img data-magnify="gallery" data-src="big-3.jpg" src="small-3.jpg">

All structures above have optional attributes as below:

3.Call plugin

If you add a data-magnify attribute, you can write none of js.

Of course, you can use selector to call the plugin as following code:

$('[data-magnify=gallery]').magnify(options);

Options

Name Type Default Description
draggable boolean true If ture, it allow modal dragging
resizable boolean true If ture, it allow modal resizing.
movable boolean true If ture, it allow image moving.
keyboard boolean true If ture, it allow keyboard control. It is similar to Windows photo viewer.

prev image

next image

+ zoom in image

- zoom out image

ctrl + alt + 0 image autual size

ctrl + , rotate image left

ctrl + . rotate image right

Q close the Magnify modal

title boolean true If ture, it will show image title on header.
fixedModalSize boolean false

If false, the modal init size will fit to image size.

If true, the modal init size will be set with modalWidth and modalHeight.

modalWidth number 320 The modal min width.
modalHeight number 320 The modal min height.
gapThreshold number 0.02 There will have a gap if modal too big to beyond the browser.
ratioThreshold number 0.01 Image zoom ratio threshold
minRatio number 0.05 (5%) The min ratio to show image.
maxRatio number 16 (1600%) The max ratio to show image.
icons: {
minimize string svg The minimize icon.
maximize string svg The maximize icon.
close string svg The close icon.
zoomIn string svg The zoomIn icon.
zoomOut string svg The zoomOut icon.
prev string svg The prev icon.
next string svg The next icon.
fullscreen string svg The fullscreen icon.
actualSize string svg The actualSize icon.
rotateLeft string svg The rotateLeft icon.
rotateRight string svg The rotateRight icon.
}
headerToolbar array [
'maximize',
'close'
]
The buttons display in header toolbar.
footerToolbar array [
'zoomIn',
'zoomOut',
'prev',
'fullscreen',
'next',
'actualSize',
'rotateRight'
]
The buttons display in footer toolbar.
fixedContent boolean true If true, the content will be fixed.
i18n: {
minimize string minimize The minimize button title text.
maximize string maximize The maximize button title text.
close string close The close button title text.
zoomIn string zoom-in The zoomIn button title text.
zoomOut string zoom-out The zoomOut button title text.
prev string prev The prev button title text.
next string next The next button title text.
fullscreen string fullscreen The fullscreen button title text.
actualSize string actual-size The actualSize button title text.
rotateLeft string rotate-left The rotateLeft button title text.
rotateRight string rotate-right The rotateRight button title text.
}
initMaximized boolean false

If false, the modal size will be set of image size or what you set.

If true, the modal size will be set maximized when init.

multiInstances boolean true

If true, it allow multiple instances.

initEvent string click

The event to init plugin. Another value is dblclick.

initAnimation boolean true

If false, it will not have animation at plugin's init.

fixedModalPos boolean false

If true, the modal position will be fixed when change images.

zIndex number 1090

The modal style of z-index, it is useful with multiple instances.

dragHandle string ' '

The handle of draggable.

progressiveLoading boolean true

If true, the image will be rendered progressively.

customButtons object {}
$('[data-magnify=gallery]').magnify({
  footerToolbar: [
    ...
    'myCustomButton'
  ],
  customButtons: {
    myCustomButton: {
      text: 'custom!',
      title: 'custom!',
      click: function (context, e) {
        alert('clicked the custom button!');
      }
    }
  }
});

Each customButton entry accepts the following properties:

  • text - the text to be display on the button itself.
  • title - the title to be display when hover the button.
  • click - a callback function that is called when the button is clicked.

Events

You can define callbacks in callbacks option. In each callback you can get the Magnify instance with this or context.

$("[data-magnify=gallery]").magnify({
  callbacks: {
    beforeOpen: function(context){
      // Will fire before modal is opened
    },
    opened: function(context){
      // Will fire after modal is opened
    },
    beforeClose: function(context){
      // Will fire before modal is closed
    },
    closed: function(context){
      // Will fire after modal is closed
    },
    beforeChange: function(context, index){
      // Will fire before image is changed
      // The argument index is the current image index of image group
    },
    changed: function(context, index){
      // Will fire after image is changed
      // The argument index is the next image index of image group
    }
  }
});

Bugs & Contributing

Please report bugs via GitHub if you think you might have found a bug or if you have a feature suggestion.