-
- var newTop = Math.min(0, Math.max(imageOrigin.top + delta.y, imageDimensions.galleryHeight - imageDimensions.height));
- var newLeft = Math.min(0, Math.max(imageOrigin.left + delta.x, imageDimensions.galleryWidth - imageDimensions.width));
- image.css({position: 'absolute', top: newTop, left: newLeft});
+ console.log('load', imageDimensions)
+ var position = normalizePosition(
+ image.position().left,
+ image.position().top,
+ imageDimensions.galleryWidth,
+ imageDimensions.galleryHeight,
+ imageDimensions.width,
+ imageDimensions.height
+ );
+ image.css({left: position.x, top: position.y, width: $(this).width() * zoomFactor, height: $(this).height() * zoomFactor});
+ });
+
+ $(window).resize(function() {
+ imageDimensions.galleryWidth = image.parent().width();
+ imageDimensions.galleryHeight = image.parent().height();
+ });
+
+ function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight) {
+ return {
+ maxX: 0,
+ maxY: 0,
+ minX: galleryWidth - imageWidth,
+ minY: galleryHeight - imageHeight
+ }
+ }
+
+ function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight) {
+ var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
+ return {
+ x: Math.min(b.maxX, Math.max(b.minX, x)),
+ y: Math.min(b.maxY, Math.max(b.minY, y))
+ }
+ }
+
+ function onMouseMove(event) {
+ var position = normalizePosition(
+ event.clientX - origin.x + imageOrigin.left,
+ event.clientY - origin.y + imageOrigin.top,
+ imageDimensions.galleryWidth,
+ imageDimensions.galleryHeight,
+ imageDimensions.width,
+ imageDimensions.height
+ );
+ image.css({position: 'absolute', top: position.y, left: position.x});