如何创建:图像缩放
- पिछला पृष्ठ फ़िल्टर किया गया कॉलेक्शन
- अगला पृष्ठ इमेज ज़ूम
学习如何创建图像缩放。
图像缩放
请将鼠标悬停在图像上:

放大预览:
创建图像缩放
第一步 - 添加 HTML:
<div class="img-zoom-container"> <img id="myimage" src="img_girl.jpg" width="300" height="240" alt="Girl"> <div id="myresult" class="img-zoom-result"></div> </div>
第二步 - 添加 CSS:
容器必须具有“相对”定位。
* {box-sizing: border-box;} .img-zoom-container { position: relative; } .img-zoom-lens { position: absolute; border: 1px solid #d4d4d4; /*set the size of the lens:*/ width: 40px; height: 40px; } .img-zoom-result { border: 1px solid #d4d4d4; /*set the size of the result div:*/ width: 300px; height: 300px; }
तीसरा - JavaScript जोड़ें:
function imageZoom(imgID, resultID) { var img, lens, result, cx, cy; img = document.getElementById(imgID); result = document.getElementById(resultID); /* lens बनाएं: */ lens = document.createElement("DIV"); lens.setAttribute("class", "img-zoom-lens"); /* lens को जोड़ें: */ img.parentElement.insertBefore(lens, img); /* result DIV और lens के बीच की अनुपात की गणना करें: */ cx = result.offsetWidth / lens.offsetWidth; cy = result.offsetHeight / lens.offsetHeight; /* result DIV के लिए बैकग्राउंड प्रोपर्टी सेट करें: */ 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(); /* बत्ती के x और y स्थान प्राप्त करना: */ 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; /* छवि के x और y स्थान प्राप्त करना: */ a = img.getBoundingClientRect(); /* बत्ती के तुल्य छवि के प्रति x और y स्थान गणना: */ x = e.pageX - a.left; y = e.pageY - a.top; /* किसी भी पृष्ठ रोलिंग को ध्यान में रखें: */ x = x - window.pageXOffset; y = y - window.pageYOffset; return {x : x, y : y}; } }
चौथा - विस्तार क्रमांक:
<script> imageZoom("myimage", "myresult"); </script>
- पिछला पृष्ठ फ़िल्टर किया गया कॉलेक्शन
- अगला पृष्ठ इमेज ज़ूम