使用不同尺寸的图像更改图像源,不会在 IE 中调整大小

发布于 2024-10-10 15:45:37 字数 1146 浏览 2 评论 0原文


我正在尝试使用 javascript 创建一个非常简单的画廊。有缩略图,当单击它们时,大图像的源就会更新。一切工作正常,除了当我在 IE 中尝试时,图像的大小保持与初始图像的大小相同。假设初始图像为 200x200,我单击 100x100 图像的缩略图,该图像会显示,但会拉伸到 200x200。我没有设置任何宽度或高度值,所以我猜浏览器应该使用图像的正常大小,例如 FF 也是如此。

这里有一些代码:

function showBigImage(link)
{
 var source = link.getAttribute("href");
 var bigImage = document.getElementById("bigImage");
 bigImage.setAttribute("src", source);
 return false; /* prevent normal behaviour of <a> element when clicked */
}

html 看起来像这样:

<ul id="gallery">
 <li>
  <a href="images/gallery/1.jpg">
   <img src="images/gallery/1thumb.jpg">
  </a>
 </li>
    (more <li> elements ...)
</ul>

大图像是动态创建的:

function createBigImage()
{
 var bigImage = document.createElement("img");
 bigImage.setAttribute("id", "bigImage");
 bigImage.setAttribute("src", "images/gallery/1.jpg");

 var gal = document.getElementById("gallery");
 var gal_parent = gal.parentNode;
 gal_parent.insertBefore(bigImage, gal);
}

还有一些代码设置链接上的 onclick 事件,但我认为它与这种情况无关。正如我所说,问题仅出在 IE 上。提前致谢!

I'm trying to create a very simple gallery using javascript. There are thumbnails, and when they're clicked the big image's source gets updated. Everything works fine, except when I try it in IE the images' size stays the same as the inital image's size was. Let's say initial image is 200x200 and I click on a thumbnail of a 100x100 image, the image is displayed but it is streched to 200x200. I don't set any width or height values, so I guess the browser should use image's normal size, and so does for example FF.

here's some code:

function showBigImage(link)
{
 var source = link.getAttribute("href");
 var bigImage = document.getElementById("bigImage");
 bigImage.setAttribute("src", source);
 return false; /* prevent normal behaviour of <a> element when clicked */
}

and html looks like this:

<ul id="gallery">
 <li>
  <a href="images/gallery/1.jpg">
   <img src="images/gallery/1thumb.jpg">
  </a>
 </li>
    (more <li> elements ...)
</ul>

the big image is created dynamically:

function createBigImage()
{
 var bigImage = document.createElement("img");
 bigImage.setAttribute("id", "bigImage");
 bigImage.setAttribute("src", "images/gallery/1.jpg");

 var gal = document.getElementById("gallery");
 var gal_parent = gal.parentNode;
 gal_parent.insertBefore(bigImage, gal);
}

There's also some code setting the onclick events on the links, but I don't think it's relevant in this situaltion. As I said the problem is only with IE. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放手` 2024-10-17 15:45:37

听起来 IE 在创建 #bigImage 时计算了 widthheight 属性,然后在 src< 时不更新它们/代码> 已更改。其他浏览器可能注意到它们必须自己计算图像尺寸,因此当 src 更改时它们会重新计算它们。这两种做法都是足够合理的。

你知道showBigImage()中图像的正确尺寸吗?如果这样做,则在更改 src 时显式设置 widthheight 属性:

function showBigImage(link) {
    var source   = link.getAttribute("href");
    var bigImage = document.getElementById("bigImage");
    bigImage.setAttribute("src",    source);
    bigImage.setAttribute("width",  the_proper_width);
    bigImage.setAttribute("height", the_proper_height);
    return false;
}

如果您不知道新尺寸,则更改showBigImage() 删除 #bigImage 并创建一个新的:

function createBigImage(src) {
    var bigImage = document.createElement("img"); 
    bigImage.setAttribute("id",  "bigImage");
    bigImage.setAttribute("src", src || "images/gallery/1.jpg");

    var gal = document.getElementById("gallery");
    gal.parentNode.insertBefore(bigImage, gal);
}

function showBigImage(link) {
    var bigImage = document.getElementById("bigImage");
    if(bigImage)
        bigImage.parentNode.removeChild(bigImage);
    createBigImage(link.getAttribute("href"););
    return false;
}

Sounds like IE is computing the width and height attributes for #bigImage when it is created and then not updating them when the src is changed. The other browsers are probably noting that they had to compute the image dimensions themselves so they recompute them when the src is changed. Both approaches are reasonable enough.

Do you know the proper size of the image inside showBigImage()? If you do, then set the width and height attributes explicitly when you change the src:

function showBigImage(link) {
    var source   = link.getAttribute("href");
    var bigImage = document.getElementById("bigImage");
    bigImage.setAttribute("src",    source);
    bigImage.setAttribute("width",  the_proper_width);
    bigImage.setAttribute("height", the_proper_height);
    return false;
}

If you don't know the new dimensions then change showBigImage() to delete #bigImage and create a new one:

function createBigImage(src) {
    var bigImage = document.createElement("img"); 
    bigImage.setAttribute("id",  "bigImage");
    bigImage.setAttribute("src", src || "images/gallery/1.jpg");

    var gal = document.getElementById("gallery");
    gal.parentNode.insertBefore(bigImage, gal);
}

function showBigImage(link) {
    var bigImage = document.getElementById("bigImage");
    if(bigImage)
        bigImage.parentNode.removeChild(bigImage);
    createBigImage(link.getAttribute("href"););
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文