How can I prevent the lens from jumping when it is on the first image and show the result of the zoom correctly on the div of the zoom result and how can I make the lens appear on the other images, not just on the first one as it is in the gif if not, follow the cursor and the position of the other images, it seems that the result of the zoom with the position of the cursor is correct
I am using grid to display in a grid or ruler the images to which the zoom effect is applied.
this is my css code:
.imagen__cont__zoom .grid_contenedores {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}
.imagen__cont__zoom .cont__img {
width: 100%;
overflow: hidden;
}
.imagen__cont__zoom .cont__img img {
width: 300px;
height: 300px;
/*object-fit: cover;*/
}
.img__zoom__lente {
position: absolute;
border: 1px solid #d4d4d4;
width: 40px;
height: 40px;
}
.img__zoom__resultado {
border: 1px solid #d4d4d4;
width: 300px;
height: 300px;
}
In the html I am adding several images to which I am applying zoom
this is my html code:
<div class="imagen__cont__zoom">
<div class="grid_contenedores">
<div class="cont__img">
<img class="img__zoom" src="https://source.unsplash.com/random/?city,night" alt="Girl">
</div>
<div class="cont__img">
<img class="img__zoom" src="https://source.unsplash.com/random/?space,night" alt="Girl">
</div>
<div class="cont__img">
<img class="img__zoom" src="https://source.unsplash.com/random/?montain,night" alt="Girl">
</div>
<div class="cont__img">
<img class="img__zoom" src="https://source.unsplash.com/random/?night" alt="Girl">
</div>
<div class="cont__img">
<img class="img__zoom" src="https://source.unsplash.com/random/?beach" alt="Girl">
</div>
<div class="cont__img">
<img class="img__zoom" src="https://source.unsplash.com/random/?vector" alt="Girl">
</div>
<div class="cont__img">
<img class="img__zoom" src="https://source.unsplash.com/random/?winter" alt="Girl">
</div>
</div>
<div class="img__zoom__lente"></div>
<div class="img__zoom__resultado"></div>
</div>
I think that the way I do the calculations for the position of the lens is wrong, that is why it is not shown according to the image where the lens is positioned, that is, in the real position of the cursor.
this is my javascript code:
var imgs, lens, result, cx, cy;
imgs = document.querySelectorAll('.img__zoom');
result = document.querySelector('.img__zoom__resultado');
lens = document.querySelector('.img__zoom__lente');
// Función para mover la lente
function moveLens(e) {
var pos, x, y, img;
// Calcula la posición de la lente
pos = getCursorPos(e);
x = pos.x - (lens.offsetWidth / 2);
y = pos.y - (lens.offsetHeight / 2);
img = e.target;
// Evita que la lente se salga de la imagen
if (x > img.width - lens.offsetWidth) {x = img.width - lens.offsetWidth;}
if (x < 0) {x = 0;}
if (y > img.height - lens.offsetHeight) {y = img.height - lens.offsetHeight;}
if (y < 0) {y = 0;}
// Establece la posición de la lente
lens.style.left = x + "px";
lens.style.top = y + "px";
// Calcula la relación entre el resultado y la lente
cx = result.offsetWidth / lens.offsetWidth;
cy = result.offsetHeight / lens.offsetHeight;
// Ajusta el tamaño del resultado
result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";
// Muestra el resultado
result.style.backgroundImage = "url('" + img.src + "')";
result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";
}
// Función para obtener la posición del cursor
function getCursorPos(e) {
var a, x = 0, y = 0;
e = e || window.event;
a = e.target.getBoundingClientRect();
x = e.pageX - a.left;
y = e.pageY - a.top;
x = x - window.pageXOffset;
y = y - window.pageYOffset;
return {x : x, y : y};
}
// Agrega el evento de movimiento del mouse a cada imagen y a la lente
imgs.forEach(function(img) {
img.addEventListener("mousemove", moveLens);
});
lens.addEventListener("mousemove", moveLens);