all reset
☀ Lights
Netbax Online Editor Try it Yourself
Run
HTML
reset
copy
      
CSS
reset
copy
* {box-sizing: border-box;} .zoom { position:relative; overflow:auto; } .zoom1 { float:left; position:absolute; border:1px solid #d4d4d4; width:40px; height:40px; } .zoom2 { float:left; border:1px solid #d4d4d4; width:300px; height:300px; }
Js
JAVASCRIPT
reset
copy
function imageZoom(imgID, resultID) { var img, lens, result, cx, cy; img = document.getElementById(imgID); lens = document.createElement("DIV"); lens.setAttribute("class", "zoom1"); img.parentElement.insertBefore(lens, img); result = document.getElementById(resultID); cx = result.offsetWidth / lens.offsetWidth; cy = result.offsetHeight / lens.offsetHeight; result.style.backgroundImage = "url('" + img.src + "')"; result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px"; lens.addEventListener("mousemove", moveLens); img.addEventListener("mousemove", moveLens); lens.addEventListener("touchmove", moveLens); img.addEventListener("touchmove", moveLens); function moveLens(e) { var pos, x, y; e.preventDefault(); pos = getCursorPos(e); x = pos.x - (lens.offsetWidth / 2); y = pos.y - (lens.offsetHeight / 2); 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;} lens.style.left = x + "px"; lens.style.top = y + "px"; result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px"; } function getCursorPos(e) { var a, x = 0, y = 0; e = e || window.event; a = img.getBoundingClientRect(); x = e.pageX - a.left - window.pageXOffset; y = e.pageY - a.top - window.pageYOffset; return {x : x, y : y}; } } imageZoom("zoom3", "zoom4");