Forum Moderators: open
<img src="1024x768.jpg" onload="resize(this)" />
function resize(img)
{
if(img.width>800)
{
img.height = parseInt(img.height * 800 / img.width);
img.width = 800;
}
}
It works very well and the only problem is, when the page is loading (before execute the resize() function), the picture (in its original size) will enlarge my 800x600 page. Though the page will restore to normal after a few seconds... But I need a perfect solution, right?
<html>
<head>
<title>test</title>
<script type="text/javascript">
function resize(img)
{
if(img.width>800)
{
alert('now the page is enlarged by this big pic');
img.height = parseInt(img.height * 800 / img.width);
img.width = 800;
alert('now this layout is what I need');
}
}
</script>
</head>
<body>
<div style="border: 3px solid #0000ff; width: 800px; margin: auto;">
<img src="http://static.flickr.com/39/94067824_8d524b6a47_o.jpg" onload="resize(this)" />
</div>
</body>
</html>
function resize(img) {
if(img.width > 800) {
img.style.height = parseInt(img.height * 800 / img.width);
img.style.width = 800;
}
img.style.visibility = "visible";
}
This hides the image until it has loaded and been resized.
<img src="1024x768.jpg" onload="resize(this)" style="position: absolute; visibility: hidden;" />
function resize(img) {
if(img.width > 800) {
img.style.height = parseInt(img.height * 800 / img.width);
img.style.width = 800;
}
img.style.position = "static";
img.style.visibility = "visible";
}
This hides the image until it has loaded and been resized.
<img src="transparent.gif" onload="resize(this)" />
...
<script type="text/javascript">
function resize(which) {
img = new Image();
img.src = "someimage.jpg";
// alert("width: " + img.width + "\nheight: " + img.height);
if(img.width > 800) {
which.style.height = parseInt(img.height * 800 / img.width) + "px";
which.style.width = "800px";
}
which.src = img.src;
}
</script>
Downside: if JavaScript is disabled, no image will display.
Need to fix a bug to prevent loading img from time to time:
<img src="http://www.full-url-required.com/trun-on-your-javascript.gif" onload="resize(this, 'http://www.site.com/1024x768.jpg)" /><script type="text/javascript">
function resize(which, src)
{
if(which.src!= 'http://www.full-url-required.com/trun-on-your-javascript.gif')
{
return;
}
img = new Image();
img.src = src;
if(img.width > 800)
{
which.style.height = parseInt(img.height * 800 / img.width) + "px";
which.style.width = "800px";
}
which.src = img.src;
}
</script>
<img src="http://www.full-url-required.com/1024x768.jpg" onload="resize(this)" width="1" height="1" />