繁体   English   中英

如何在单个页面上的多个图像上应用我的放大镜?

[英]How do I apply my magnifier on multple images on a single page?

 // magnify hover function magnify(imgID, zoom) { var img, glass, w, h, bw; img = document.getElementById(imgID); /*create magnifier glass:*/ glass = document.createElement("DIV"); glass.setAttribute("class", "img-magnifier-glass"); /*insert magnifier glass:*/ img.parentElement.insertBefore(glass, img); /*set background properties for the magnifier glass:*/ 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; /*execute a function when someone moves the magnifier glass over the image:*/ glass.addEventListener("mousemove", moveMagnifier); img.addEventListener("mousemove", moveMagnifier); /*and also for touch screens:*/ glass.addEventListener("touchmove", moveMagnifier); img.addEventListener("touchmove", moveMagnifier); function moveMagnifier(e) { var pos, x, y; /*prevent any other actions that may occur when moving over the image*/ e.preventDefault(); /*get the cursor's x and y positions:*/ pos = getCursorPos(e); x = pos.x; y = pos.y; /*prevent the magnifier glass from being positioned outside the image:*/ 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;} /*set the position of the magnifier glass:*/ glass.style.left = (x - w) + "px"; glass.style.top = (y - h) + "px"; /*display what the magnifier glass "sees":*/ 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; /*get the x and y positions of the image:*/ a = img.getBoundingClientRect(); /*calculate the cursor's x and y coordinates, relative to the image:*/ x = e.pageX - a.left; y = e.pageY - a.top; /*consider any page scrolling:*/ x = x - window.pageXOffset; y = y - window.pageYOffset; return {x : x, y : y}; } } /* Initiate Magnify Function with the id of the image, and the strength of the magnifier glass:*/ magnify("mag", 5);
 * {box-sizing: border-box;} .img-magnifier-container { position:relative; } .img-magnifier-glass { position: absolute; z-index: 3; border: 1px solid #000; border-radius: 20%; cursor: none; /*Set the size of the magnifier glass:*/ width: 120px; height: 130px; opacity:0; pointer-events:none; } a:hover .img-magnifier-glass{ opacity:1; pointer-events:initial; }
 <table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"><tr><td width="115" style="text-align: center"><center><a href="https://www.imdb.com/title/tt0478970/" title="Ant-Man (2015)" target="_blank"><img class="cover" width="115" height="133" border="0" id="mag" class="pstrart" id="pstr" src="https://images2.static-bluray.com/movies/covers/238380_front.jpg" style=""></a></center><center><input type="checkbox" name="movieboxes" value="Ant-Man" style="width: 15px; height: 15px; margin: 0px;"> <img title="United Kingdom" border="2" class="flag" id="flgborder" src="https://cdn.discordapp.com/attachments/530906893443399683/665554365897244693/union-jack-26119_960_720.png" style="width: 25px; height: 17px;"> </center></td></tr></table>&nbsp; &nbsp; <table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"><tr><td width="115" style="text-align: center"><center><a href="https://www.imdb.com/title/tt6644200/" title="A Quiet Place (2018)" target="_blank"><img class="cover" width="115" height="133" border="0" id="mag" class="pstrart" id="pstr" src="https://images2.static-bluray.com/movies/covers/202637_front.jpg" style=""></a></center><center><input type="checkbox" name="movieboxes" value="A Quiet Place" style="width: 15px; height: 15px; margin: 0px;"> <img title="United Kingdom" border="2" class="flag" id="flgborder" src="https://cdn.discordapp.com/attachments/530906893443399683/665554365897244693/union-jack-26119_960_720.png" style="width: 25px; height: 17px;"> </center></td></tr></table>&nbsp; &nbsp;

你好,

我最近使用本网站上人们的帮助制作了悬停放大镜。 但是,我不知道如何制作它,因此我可以在所有图像上使用放大镜,而不仅仅是第一个。 我在这篇文章的片段中提供了两张图片,所以你可以明白我的意思。

感谢您提供任何信息!

进一步阐述我的评论:主要问题是:

  1. 您的页面上的多个元素中有重复的 ID 和重复的属性。 你应该总是在一个文档中使用唯一的 ID,而不是每个元素有多个class属性,你应该将它们组合成一个。
  2. 您正在单个元素上初始化放大逻辑。 您缺乏迭代逻辑来逐步浏览您想要放大的所有图像。

我的建议是:

  1. 如上所述修复 DOM 错误。 此外,为您想要放大的图像元素提供一个通用类,以便我们可以选择所有这些元素。 示例是class="magnify-image" ,但您可以使用任何您想要的任意类名。
  2. 您更新magnify()方法以接受图像元素而不是 ID:这意味着我们的迭代逻辑可以直接传入您想要放大的图像元素。 将其重写为function magnify(img, zoom) { ... }然后删除var img = ...赋值就足够了。

  3. 你给每张你想要放大的图像一个类,然后选择所有这些图像来创建一个 HTMLCollection。 迭代这个集合,并在每次迭代中简单地将图像元素作为第一个参数传递。

迭代逻辑示例:

// Iterate through all images you want to magnify
const images = document.querySelectorAll('.magnify-image');
Array.from(images).forEach((image) => {
    // We pass the actual image element into the function
    magnify(image, 5);
});

如果你绝对要支持 ES5,那么我们可以这样做:

var images = document.getElementsByClassName('magnify-image');
Array.prototype.forEach.call(images, function(image) {
    // We pass the actual image element into the function
    magnify(image, 5);
});

请参阅概念验证:

 // magnify hover function magnify(img, zoom) { var glass, w, h, bw; /*create magnifier glass:*/ glass = document.createElement("DIV"); glass.setAttribute("class", "img-magnifier-glass"); /*insert magnifier glass:*/ img.parentElement.insertBefore(glass, img); /*set background properties for the magnifier glass:*/ 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; /*execute a function when someone moves the magnifier glass over the image:*/ glass.addEventListener("mousemove", moveMagnifier); img.addEventListener("mousemove", moveMagnifier); /*and also for touch screens:*/ glass.addEventListener("touchmove", moveMagnifier); img.addEventListener("touchmove", moveMagnifier); function moveMagnifier(e) { var pos, x, y; /*prevent any other actions that may occur when moving over the image*/ e.preventDefault(); /*get the cursor's x and y positions:*/ pos = getCursorPos(e); x = pos.x; y = pos.y; /*prevent the magnifier glass from being positioned outside the image:*/ 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; } /*set the position of the magnifier glass:*/ glass.style.left = (x - w) + "px"; glass.style.top = (y - h) + "px"; /*display what the magnifier glass "sees":*/ 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; /*get the x and y positions of the image:*/ a = img.getBoundingClientRect(); /*calculate the cursor's x and y coordinates, relative to the image:*/ x = e.pageX - a.left; y = e.pageY - a.top; /*consider any page scrolling:*/ x = x - window.pageXOffset; y = y - window.pageYOffset; return { x: x, y: y }; } } // Iterate through all images you want to magnify const images = document.querySelectorAll('.magnify-image'); Array.from(images).forEach((image) => { magnify(image, 5); });
 * { box-sizing: border-box; } .img-magnifier-container { position: relative; } .img-magnifier-glass { position: absolute; z-index: 3; border: 1px solid #000; border-radius: 20%; cursor: none; /*Set the size of the magnifier glass:*/ width: 120px; height: 130px; opacity: 0; pointer-events: none; } a:hover .img-magnifier-glass { opacity: 1; pointer-events: initial; }
 <table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"> <tr> <td width="115" style="text-align: center"> <center> <a href="https://www.imdb.com/title/tt0478970/" title="Ant-Man (2015)" target="_blank"><img width="115" height="133" border="0" class="cover pstrart magnify-image" src="https://images2.static-bluray.com/movies/covers/238380_front.jpg" style=""></a> </center> <center><input type="checkbox" name="movieboxes" value="Ant-Man" style="width: 15px; height: 15px; margin: 0px;"> <img title="United Kingdom" border="2" class="flag" src="https://cdn.discordapp.com/attachments/530906893443399683/665554365897244693/union-jack-26119_960_720.png" style="width: 25px; height: 17px;"> </center> </td> </tr> </table>&nbsp; &nbsp; <table class="img-magnifier-container" width="115" border="0" cellpadding="0" cellspacing="0" style="display: inline-block"> <tr> <td width="115" style="text-align: center"> <center> <a href="https://www.imdb.com/title/tt6644200/" title="A Quiet Place (2018)" target="_blank"><img width="115" height="133" border="0" class="cover pstrart magnify-image" src="https://images2.static-bluray.com/movies/covers/202637_front.jpg" style=""></a> </center> <center><input type="checkbox" name="movieboxes" value="A Quiet Place" style="width: 15px; height: 15px; margin: 0px;"> <img title="United Kingdom" border="2" class="flag" src="https://cdn.discordapp.com/attachments/530906893443399683/665554365897244693/union-jack-26119_960_720.png" style="width: 25px; height: 17px;"> </center> </td> </tr> </table>&nbsp; &nbsp;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM