Jump to content

Image resize


bigheadedd

Recommended Posts

Hi,

 

I'm having a little trouble with an image scaling code.

What the code does, is whenever the user scales their browser, the images on the page, scale proportionally to the size of the window.

This all works fine, however what i'm after is to have text (two arrows for navigation) on the right hand side, just underneath the image. (Bottom right).

 

I've tried wrapping the image in a div, putting the text next to the image, everything but to no avail. It always just bumps to the right hand side of the screen.

 

Heres the code:

<html>
<head>
<title>Test resize</title>
<style type="text/css">
* { margin:0; padding:0; }
#imgId {
width:100%;
max-width:1500px;
max-height:1000px;
height:auto;
}
</style>
</head>
<body>
<img id="imgId" src="images/index.jpg" />
<script type="text/javascript">
var img = document.getElementById('imgId');
window.onresize = function() {adjustRatio(img);}
//document.onload = function(){ adjustRatio(img);}
var imageratio = img.height / img.width;

function adjustRatio(img)
{
var winheight = (document.body.clientHeight === undefined)?window.innerHeight:document.body.clientHeight;
var winwidth = (document.body.clientWidth === undefined)?window.innerWidth:document.body.clientWidth;
winratio = winheight / winwidth;

if(winratio <= imageratio)
{
img.style.height = '100%';
img.style.width = 'auto';
}
else
{
img.style.width = '100%';
img.style.height = 'auto';
}
}
setTimeout("adjustRatio(img)",1);
</script>
</body>
</html> 

 

Please let me know if it doesn't make any sense!

 

Any reply would be amazing!

 

Thanks

 

Edd

Link to comment
Share on other sites

I can't seem to see anything in your code about two arrows? Perhaps a demo or screen shot may help.

 

What I will add looking at the code though, setTimeout("adjustRatio(img)",1) - you're firing the time out every millisecond? That's a thousand times a second! You're already using the resize event, why do you need the time-out?

Link to comment
Share on other sites

Hi guys,

 

Still really stuck on this?!

I've tried encapsulating the image within a div, but the scaling seems to focus only on the height, and not the width, so I can't just put right align.

 

Really scratching my head on this, so any help would be massively appreciated!

 

Thanks

 

Edd

Link to comment
Share on other sites

Unfortunately you're not going to get this always flush to the edge of the image, because you're resizing based on the aspect ratio. That means whenever the screen is wider than the height, the div will be stretched wider than the image. You would need to use JS to work out the gap on the right and set a right margin on the arrows.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.