Menu
I think I’m going mad about this :S
I have a image : <img src=”{URL}” border=”0″ /> .
The image is placed in a table or a div.
I want a javascript so if the image is wider than the table the image will be autoresized to 100% of the table widht and then the image will be linked to it’s url. {URL}
I also want to constain the height – width propotions.
<i>
</i><html>
<head>
<title>HelloButton</title>
<script>
function wrapperAddEventListener(obj,type,callback){
if(obj.addEventListener!=undefined) obj.addEventListener(type,callback,false);
else if(obj.attachEvent!=undefined) obj.attachEvent("on"+type,callback);
else alert("Sorry, your browser is not supported");
}
<i> </i>function main(){
<i> </i> var resizeMe=function(evt){
<i> </i> if(document.getElementById("foo").clientWidth<document.getElementById("bar").naturalWidth){
<i> </i> var AR=document.getElementById("bar").naturalWidth/document.getElementById("bar").naturalHeight;
<i> </i> document.getElementById("bar").style.width=(document.getElementById("foo").clientWidth-2)+"px";
<i> </i> document.getElementById("bar").style.height=((document.getElementById("foo").clientWidth-2)/AR)+"px";
<i> </i> }else{
<i> </i> document.getElementById("bar").style.width=document.getElementById("bar").naturalWidth+"px";
<i> </i> document.getElementById("bar").style.width=document.getElementById("bar").naturalHeight+"px";
<i> </i> }
<i> </i> }
<i> </i> wrapperAddEventListener(window,"resize",resizeMe);
<i> </i> resizeMe();
<i> </i>}
</script>
</head>
<body onload="main()">
<i> </i> <div style="width:310px; overflow:hidden; padding:0px;" id="foo">
<i> </i> <img src="rose3.png" style="margin:0px;" id="bar" />
<i> </i> </div>
</body>
</html>
<i>
</i><html>
<head>
<title>HelloButton</title>
<script>
function wrapperAddEventListener(obj,type,callback){
if(obj.addEventListener!=undefined) obj.addEventListener(type,callback,false);
else if(obj.attachEvent!=undefined) obj.attachEvent("on"+type,callback);
else alert("Sorry, your browser is not supported");
}
<i> </i>function main(){
<i> </i> var resizeMe=function(evt){
<i> </i> if(document.getElementById("foo").clientWidth<document.getElementById("bar").naturalWidth){
<i> </i> var AR=document.getElementById("bar").naturalWidth/document.getElementById("bar").naturalHeight;
<i> </i> document.getElementById("bar").style.width=(document.getElementById("foo").clientWidth-2)+"px";
<i> </i> document.getElementById("bar").style.height=((document.getElementById("foo").clientWidth-2)/AR)+"px";
<i> </i> wrapperAddEventListener(document.getElementById("bar"),"click",(function(){document.location.href=document.getElementById("bar").src;}));
<i> </i> }else{
<i> </i> document.getElementById("bar").style.width=document.getElementById("bar").naturalWidth+"px";
<i> </i> document.getElementById("bar").style.width=document.getElementById("bar").naturalHeight+"px";
<i> </i> wrapperAddEventListener(document.getElementById("bar"),"click",(function(){}));
<i> </i> }
<i> </i> }
<i> </i> wrapperAddEventListener(window,"resize",resizeMe);
<i> </i> resizeMe();
<i> </i>}
</script>
</head>
<body onload="main()">
<i> </i> <div style="width:310px; overflow:hidden; padding:0px;" id="foo">
<i> </i> <img src="rose3.png" style="margin:0px;" id="bar" />
<i> </i> </div>
</body>
</html>
[code=html]<img src='{URL}' border='0' onload="javascript:if(this.width > screen.width-300){ this.height = Math.round(((screen.width-300)/this.width)*this.height); this.width = (screen.width-300); }" onclick="javascript:window.open('{URL}','','scrollbars=1,toolbar=0,resizable=1,menubar=0,directories=0,status =0')" alt="The img might be resized, klick to wiev fullscreen." title="The img might be resized, klick to wiev fullscreen." />[/code]
<i>
</i> <div style="width:310px; overflow:hidden; padding:0px;">
<img src="rose3.png" style="margin:0px;" onload='var bar=this; for(var foo=bar;foo.tagName!="DIV";foo=foo.parentNode){} var barw=bar.width; var barh=bar.height; var foocw=foo.clientWidth; var AR=barw/barh;if(foocw<barw){bar.style.width=(foocw-2)+"px";bar.style.height=((foocw-2)/AR)+"px";bar.onclick=(function(){window.open(this.src,"","scrollbars=1,toolbar=0,resizable=1,menubar=0,directories=0,status=0");});}else{bar.style.width=barw+"px";bar.style.width=barh+"px";}' />
</div>
[code=html]<div style="width:9000px; overflow:hidden; padding:0px; "><img src="http://wwwimages.adobe.com/www.adobe.com/images/icons/ps_splash_beta_labs_150x50.jpg" style="margin:0px;" border="0" onload='var bar=this; for(var foo=bar;foo.tagName!="DIV";foo=foo.parentNode){} var barw=bar.width; var barh=bar.height; var foocw=foo.clientWidth; var AR=barw/barh;if(foocw<barw){bar.style.width=(foocw-2)+"px";bar.style.height=((foocw-2)/AR)+"px";bar.onclick=(function(){window.open(this.src,"","scrollbars=1,toolbar=0,resizable=1,menubar=0,directories=0,status=0");});}else{bar.style.width=barw+"px";bar.style.width=barh+"px";}' />
</div>[/code]
<i>
</i><img src="http://wwwimages.adobe.com/www.adobe.com/images/icons/ps_splash_beta_labs_150x50.jpg" style="margin:0px;" onload='var bar=this; for(var foo=bar;foo.tagName!="DIV";foo=foo.parentNode){} var barw=bar.width; var barh=bar.height; var foocw=foo.clientWidth;var AR=barw/barh;if(foocw<barw){bar.style.width=(foocw-2)+"px";bar.style.height=((foocw-2)/AR)+"px";bar.onclick=(function(){window.open(this.src,"","scrollbars=1,toolbar=0,resizable=1,menubar=0,directories=0,status=0");});}' alt="The img might be resized, klick to wiev fullscreen." title="The img might be resized, klick to wiev fullscreen." />
0.1.9 — BETA 5.17