使用不同尺寸的图像更改图像源,不会在 IE 中调整大小
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来 IE 在创建
#bigImage
时计算了width
和height
属性,然后在src< 时不更新它们/代码> 已更改。其他浏览器可能注意到它们必须自己计算图像尺寸,因此当
src
更改时它们会重新计算它们。这两种做法都是足够合理的。你知道
showBigImage()
中图像的正确尺寸吗?如果这样做,则在更改src
时显式设置width
和height
属性:如果您不知道新尺寸,则更改
showBigImage()
删除#bigImage
并创建一个新的:Sounds like IE is computing the
width
andheight
attributes for#bigImage
when it is created and then not updating them when thesrc
is changed. The other browsers are probably noting that they had to compute the image dimensions themselves so they recompute them when thesrc
is changed. Both approaches are reasonable enough.Do you know the proper size of the image inside
showBigImage()
? If you do, then set thewidth
andheight
attributes explicitly when you change thesrc
:If you don't know the new dimensions then change
showBigImage()
to delete#bigImage
and create a new one: