Cómo crear: Lupa de imagen
Aprende a crear una lupa de imagen.
Lupa de imagen
Coloque el ratón sobre la imagen:

Crear un lupa de imagen
Primer paso - Añadir HTML:
<div class="img-magnifier-container"> <img id="myimage" src="img_girl.jpg" width="600" height="400" alt="Girl"> </div>
Segundo paso - Añadir CSS:
El contenedor debe tener la posición relativa.
* {box-sizing: border-box;} .img-magnifier-container { position: relative; } .img-magnifier-glass { position: absolute; border: 3px solid #000; border-radius: 50%; cursor: none; /* Establecer el tamaño del amplificador de imagen */ width: 100px; height: 100px; }
Tercero - Añadir JavaScript:
function magnify(imgID, zoom) { var img, glass, w, h, bw; img = document.getElementById(imgID); /* Crear la lupa: */ glass = document.createElement("DIV"); glass.setAttribute("class", "img-magnifier-glass"); /* Insertar la lupa: */ img.parentElement.insertBefore(glass, img); /* Establecer las propiedades de fondo de la lupa: */ glass.style.backgroundImage = "url('" + img.src + "')"; glass.style.backgroundRepeat = "no-repeat"; glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px"; bw = 3; w = glass.offsetWidth / 2; h = glass.offsetHeight / 2; /* Función ejecutada cuando el usuario mueve el lupa sobre la imagen: */ glass.addEventListener("mousemove", moveMagnifier); img.addEventListener("mousemove", moveMagnifier); /* También se aplica a la pantalla táctil: */ glass.addEventListener("touchmove", moveMagnifier); img.addEventListener("touchmove", moveMagnifier); function moveMagnifier(e) {}} var pos, x, y; /* Prevenir cualquier otra operación que pueda ocurrir al moverse sobre la imagen */ e.preventDefault(); /* Obtener las posiciones x e y del cursor: */ pos = getCursorPos(e); x = pos.x; y = pos.y; /* Evitar que el lupa esté fuera de la imagen: */ if (x > img.width - (w / zoom)) {x = img.width - (w / zoom);} if (x < w / zoom) {x = w / zoom;} if (y > img.height - (h / zoom)) {y = img.height - (h / zoom);} if (y < h / zoom) {y = h / zoom;} /* Establecer la posición del lupa: */ glass.style.left = (x - w) + "px"; glass.style.top = (y - h) + "px"; /* Mostrar el contenido "visto" por el lupa: */ glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px"; } function getCursorPos(e) { var a, x = 0, y = 0; e = e || window.event; /* Obtener las posiciones x e y de la imagen: */ a = img.getBoundingClientRect(); /* Calcular las coordenadas x e y del cursor en relación con la imagen: */ x = e.pageX - a.left; y = e.pageY - a.top; /* Considerar cualquier desplazamiento de página: */ x = x - window.pageXOffset; y = y - window.pageYOffset; devolver {x : x, y : y}; } }
Paso 4 - Iniciar la función de ampliación:
<script> /* Ejecutar la función de ampliación: */ amplificar("miimagen", 3); /* 指定图像的 id 和放大镜的强度: */ </script>