JQuery Panzoom plugin right positioning image with contain: 'invert'

I'm trying to use the jQuery Panzoom plugin , which is mostly fine, except ...

I want the initially large image to be scaled to fit in the container and included contain: 'invert'

. I adapted this example to scale and position the image on first load.

This works great when the image is centered on the container (the size of the window). But if I use contain: 'invert'

Panzoom as a parameter, the image will be on the right. I can't figure out why.

You can see it in action here: https://jsfiddle.net/7ugm9z51/2/

Here's the HTML / CSS:

.zoom-container {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
}

<div class="zoom-container">
  <img class="zoom-img" src="https://farm8.staticflickr.com/7314/15854478283_d2667a7264_o.jpg">
</div>

      

And JavaScript:

$('.zoom-img').panzoom({
  // Causes image to appear aligned right:
  contain: 'invert',
});

// Get container dimensions
var container_height = $('.zoom-container').height();
var container_width = $('.zoom-container').width();

// Get image dimensions
var image_height = $('.zoom-img').height();
var image_width = $('.zoom-img').width();

// Calculate the center of image since origin is at x:50% y:50%
var image_center_left = image_width / 2.0;
var image_center_top = image_height / 2.0;

// Calculate scaling factor
var zoom_factor;

// Check to determine whether to stretch along width or height
if(image_height > image_width) {
    zoom_factor = container_height / image_height;
} else {
    zoom_factor = container_width / image_width;
};

// Zoom by zoom_factor
$('.zoom-img').panzoom('option', 'minScale', zoom_factor);
$('.zoom-img').panzoom('zoom', zoom_factor, {animate: false});

// Calculate new image dimensions after zoom
image_width = image_width * zoom_factor;
image_height = image_height * zoom_factor;

// Calculate offset of the image after zoom
var image_offset_left = image_center_left - (image_width / 2.0);
var image_offset_top = image_center_top - (image_height / 2.0);

// Calculate desired offset for image
var new_offset_left = (container_width - image_width) / 2.0;
var new_offset_top = (container_height - image_height) / 2.0;

// Pan to set desired offset for image
var pan_left = new_offset_left - image_offset_left;
var pan_top = new_offset_top - image_offset_top;

$('.zoom-img').panzoom('pan', pan_left, pan_top);

      

+2


source to share





All Articles