|
6 | 6 | var modalImg = $('#modalImage'); |
7 | 7 | var captionText = $('#caption'); |
8 | 8 | var closeBtn = $('.close'); |
| 9 | + |
| 10 | + // Transform state |
9 | 11 | var scale = 1; |
| 12 | + var translateX = 0; |
| 13 | + var translateY = 0; |
10 | 14 | var minScale = 0.5; |
11 | 15 | var maxScale = 5; |
| 16 | + |
| 17 | + // Dragging state |
| 18 | + var isDragging = false; |
| 19 | + var lastX = 0; |
| 20 | + var lastY = 0; |
| 21 | + |
| 22 | + function updateTransform() { |
| 23 | + modalImg.css('transform', `scale(${scale}) translate(${translateX}px, ${translateY}px)`); |
| 24 | + } |
| 25 | + |
| 26 | + function resetTransform() { |
| 27 | + scale = 1; |
| 28 | + translateX = 0; |
| 29 | + translateY = 0; |
| 30 | + updateTransform(); |
| 31 | + } |
12 | 32 |
|
13 | 33 | // Add click event to all images |
14 | 34 | $('.image.fit img, .image.main img').on('click', function() { |
15 | 35 | modal.show(); |
16 | 36 | modalImg.attr('src', this.src); |
17 | 37 | captionText.text($(this).attr('alt')); |
18 | | - scale = 1; // Reset zoom |
19 | | - modalImg.css('transform', 'scale(' + scale + ')'); |
| 38 | + resetTransform(); |
20 | 39 | }); |
21 | 40 |
|
22 | 41 | // Mouse wheel zoom |
|
26 | 45 | var zoomSpeed = 0.1; |
27 | 46 |
|
28 | 47 | if (delta < 0) { |
29 | | - // Zoom in |
30 | 48 | scale = Math.min(maxScale, scale + zoomSpeed); |
31 | 49 | } else { |
32 | | - // Zoom out |
33 | 50 | scale = Math.max(minScale, scale - zoomSpeed); |
34 | 51 | } |
35 | 52 |
|
36 | | - $(this).css('transform', 'scale(' + scale + ')'); |
| 53 | + updateTransform(); |
| 54 | + }); |
| 55 | + |
| 56 | + // Mouse drag events |
| 57 | + modalImg.on('mousedown', function(e) { |
| 58 | + if (scale > 1) { |
| 59 | + isDragging = true; |
| 60 | + lastX = e.clientX; |
| 61 | + lastY = e.clientY; |
| 62 | + $(this).css('cursor', 'grabbing'); |
| 63 | + e.preventDefault(); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + $(document).on('mousemove', function(e) { |
| 68 | + if (isDragging) { |
| 69 | + var deltaX = e.clientX - lastX; |
| 70 | + var deltaY = e.clientY - lastY; |
| 71 | + |
| 72 | + translateX += deltaX; |
| 73 | + translateY += deltaY; |
| 74 | + |
| 75 | + lastX = e.clientX; |
| 76 | + lastY = e.clientY; |
| 77 | + |
| 78 | + updateTransform(); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + $(document).on('mouseup', function() { |
| 83 | + if (isDragging) { |
| 84 | + isDragging = false; |
| 85 | + modalImg.css('cursor', scale > 1 ? 'grab' : 'pointer'); |
| 86 | + } |
37 | 87 | }); |
38 | 88 |
|
39 | | - // Double-click to reset zoom |
| 89 | + // Touch events for mobile |
| 90 | + var lastTouchX = 0; |
| 91 | + var lastTouchY = 0; |
| 92 | + |
| 93 | + modalImg.on('touchstart', function(e) { |
| 94 | + if (scale > 1 && e.touches.length === 1) { |
| 95 | + isDragging = true; |
| 96 | + lastTouchX = e.touches[0].clientX; |
| 97 | + lastTouchY = e.touches[0].clientY; |
| 98 | + e.preventDefault(); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + modalImg.on('touchmove', function(e) { |
| 103 | + if (isDragging && e.touches.length === 1) { |
| 104 | + var deltaX = e.touches[0].clientX - lastTouchX; |
| 105 | + var deltaY = e.touches[0].clientY - lastTouchY; |
| 106 | + |
| 107 | + translateX += deltaX; |
| 108 | + translateY += deltaY; |
| 109 | + |
| 110 | + lastTouchX = e.touches[0].clientX; |
| 111 | + lastTouchY = e.touches[0].clientY; |
| 112 | + |
| 113 | + updateTransform(); |
| 114 | + e.preventDefault(); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + modalImg.on('touchend', function() { |
| 119 | + isDragging = false; |
| 120 | + }); |
| 121 | + |
| 122 | + // Double-click to reset |
40 | 123 | modalImg.on('dblclick', function() { |
41 | | - scale = 1; |
42 | | - $(this).css('transform', 'scale(' + scale + ')'); |
| 124 | + resetTransform(); |
| 125 | + }); |
| 126 | + |
| 127 | + // Update cursor based on zoom level |
| 128 | + modalImg.on('load', function() { |
| 129 | + $(this).css('cursor', scale > 1 ? 'grab' : 'pointer'); |
43 | 130 | }); |
44 | 131 |
|
45 | 132 | // Close modal events |
|
0 commit comments