gpt4 book ai didi

javascript - 从缩略图切换新图像后,缩放仍显示原始图像

转载 作者:行者123 更新时间:2023-12-03 23:41:25 26 4
gpt4 key购买 nike

我使用来自 w3schools 的图像缩放,代码如下:

function imageZoom(imgID, resultID) {
var img, lens, result, cx, cy;
img = document.getElementById(imgID);
result = document.getElementById(resultID);
lens = document.createElement("DIV");
lens.setAttribute("class", "img-zoom-lens");
img.parentElement.insertBefore(lens, img);
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);
result.style.display = "none";

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;
y = e.pageY - a.top;
x = x - window.pageXOffset;
y = y - window.pageYOffset;
return {x : x, y : y};
}
}

imageZoom("myimage", "myresult");

我使用以下简单代码来切换图像:
function change_img(img_src) {
document.getElementsByName("goods_img")[0].src=img_src;
}
我的网址: https://cn.angelcorp.net/shop/goods.php?id=9
您可以单击带有标志的缩略图,但缩放仍然显示没有标志的原始图像。
谢谢你。

最佳答案

你必须改变myresult的背景到img_src也是。
把函数改成这个

function change_img(img_src) {
document.getElementsByName("goods_img")[0].src=img_src;
document.getElementById("myresult").style = `background-image: url("${img_src}"); background-size: 468.846px 468.846px; display: none; background-position: -256.846px -256.846px;`;
}

关于javascript - 从缩略图切换新图像后,缩放仍显示原始图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65486030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com