admin管理员组文章数量:1278986
I am trying to get some JavaScript
to programmatically adjust a HTML img
tag's width to display various sized images correctly.
I have a fixed width img
tag at 800px
to display an image, this is the max width.
If the image is wider then 800px
I want to display it at 800px
wide;
If the image is less than 800px
wide I want to preserve its width to avoid stretching it.
I use this html/javacript code to get a partial solution:
function resize_image(id) {
var img = document.getElementById(id);
var normal_width = img.width;
img.removeAttribute("width");
var real_width = img.width;
if (real_width < normal_width) {
img.width = real_width;
} else {
img.width = normal_width;
}
}
<img id="myimage" onload="resize_image(self.id);" src="" width="800" />
I am trying to get some JavaScript
to programmatically adjust a HTML img
tag's width to display various sized images correctly.
I have a fixed width img
tag at 800px
to display an image, this is the max width.
If the image is wider then 800px
I want to display it at 800px
wide;
If the image is less than 800px
wide I want to preserve its width to avoid stretching it.
I use this html/javacript code to get a partial solution:
function resize_image(id) {
var img = document.getElementById(id);
var normal_width = img.width;
img.removeAttribute("width");
var real_width = img.width;
if (real_width < normal_width) {
img.width = real_width;
} else {
img.width = normal_width;
}
}
<img id="myimage" onload="resize_image(self.id);" src="https://via.placeholder./350x150" width="800" />
The above code seems to work on all browsers I have tested except Safari
(images don't display unless you refresh the page).
I know I can use CSS max-width
but that wont work on IE
< 7 which is a show stopper.
How can I get this working for all browsers? Many thanks in advance.
Share Improve this question edited Sep 14, 2018 at 7:43 ponury-kostek 8,0705 gold badges25 silver badges32 bronze badges asked Dec 16, 2008 at 14:29 QAZQAZ 4,9307 gold badges38 silver badges52 bronze badges4 Answers
Reset to default 3I have never seen a safari in work, but you can try changing your onload event to this:
onload="resize_image(self.id);return true"
It could be that without a return value, safari thinks that this object should not be loaded.
Use the IE6 css+javascript hack:
.dynamic_img {
width: expression(document.body.clientWidth <= 800? "auto" : "800px");
max-width: 800px; //For normal browsers
}
Without id:
...
function resize_image( img )
{
//var img = document.getElementById( id );
...
<img onload="resize_image(this);" src="IMAGE.JPG" width="800" />
Have you tried monkey with img.style.width
? You could also try having 2 CSS classes for each of the 2 conditions and programmaticly change them.
本文标签: HTMLJavascript Dynamic Image ResizeStack Overflow
版权声明:本文标题:HTML + Javascript: Dynamic Image Resize? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741299622a2371016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论