The Panzoom library (https://timmywil.com/panzoom/demo/) allows you to move and zoom an image in a div:
var panzoom1 = Panzoom(document.querySelector(".zoom-area1"), {
maxScale: 6
});
document.querySelector(".zoom-wrapper1").addEventListener("wheel", panzoom1.zoomWithWheel);
var panzoom2 = Panzoom(document.querySelector(".zoom-area2"), {
maxScale: 6
});
document.querySelector(".zoom-wrapper2").addEventListener("wheel", panzoom2.zoomWithWheel);
.main-container {
display: flex;
}
.zoom-wrapper1 {
width: 300px;
height: 300px;
border: 5px solid #1C6EA4;
}
.zoom-wrapper2 {
width: 300px;
height: 300px;
border: 5px solid #1C6EA4;
}
<script src="https://timmywil.com/panzoom/demo/panzoom.js"></script>
<div class="main-container">
<div class="zoom-wrapper1">
<div class="zoom-area1">
<img src="https://ironcodestudio.com/wp-content/uploads/2015/03/css-remove-horizontal-scrollbar.jpg" />
</div>
</div>
<div class="zoom-wrapper2">
<div class="zoom-area2">
<img src="https://image.freepik.com/free-photo/vertical-view-eiffel-tower-paris-france_1258-3169.jpg" />
</div>
</div>
</div>
Everything is working properly.
Now I would like these images to fill their divs but:
- the image has to be centered vertically and horizontally
- if the image has a horizontal orientation, its height cannot be higher than the height of the div
- if the image has a vertical orientation, then its width cannot be greater than the div's width. Exactly like here: https://drive.google.com/file/d/1znFq-EJVE4Dv2j9sc3xZUZuMH0bhRsfz/view?usp=sharing
How to do it? I'm trying different ways and I'm already running out of ideas.
I'm not sure, but I think this is ok:
Definitely the problem is that the zoom escapes to the sides, it is not directed to the point pointed by the mouse. This needs improvement.