Web Design Forum: Dynamically set width and height of image using javascript - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dynamically set width and height of image using javascript

#1 User is offline   Kanishk Dudeja 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 30-November 11
  • Reputation: 0

Posted 17 January 2012 - 11:21 AM

I want to dynamically set the height and width of an image using javascript so that the image looks proportionally fine according to different screen resolutions..

The original image is of 252*62 pixels..

I have used the following code..but it doesnt seem to work.

<script type="text/javascript">
function resizeheight() {
    var height = ((62*screen.height)/800);
 
	return height;
}
function resizewidth() {
    var width = ((252*screen.width)/1280);
 
	return width;
}


<img id="enter" src="enter.PNG" height="resizeheight()" width="resizewidth()" >

0

#2 User is online   Renaissance-Design 

  • Available for custom WordPress work
  • View blog
  • Group: Moderators
  • Posts: 3,595
  • Joined: 12-August 10
  • Reputation: 559
  • Gender:Male
  • Location:South Wales
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 17 January 2012 - 11:23 AM

You can't embed javascript calls in the width and height properties like that. You're best off attaching an event listener to DOM ready, and by the time you've done that you might as well use jQuery.
0

#3 User is offline   Kanishk Dudeja 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 30-November 11
  • Reputation: 0

Posted 17 January 2012 - 11:36 AM

Okay. Thank you :)
0

#4 User is offline   Kanishk Dudeja 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 30-November 11
  • Reputation: 0

Posted 18 January 2012 - 07:52 AM

I used another method. It worked. Thanks for your suggestion though :)

<img id="enter" src="enter.PNG" alt="enter" />

<script type="text/javascript">
target=document.getElementById('enter');
target.style.width=((252*screen.width)/1280)+'px';
target.style.height=((62*screen.height)/800)+'px';
</script>

0

#5 User is offline   hodephdesign 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 129
  • Joined: 18-January 12
  • Reputation: 10
  • Gender:Male
  • Location:Cardiff, Wales
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 18 January 2012 - 10:21 AM

It might be worth looking into adding some script to the resize function of the body, as if the window is not the full screen size it will be too large (depending on what you're trying to achieve).

Adding a jQuery resize function to the "onresize" function of the body can allow the user to resize the window and have the image scale proportionally...
0

#6 User is offline   Kanishk Dudeja 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 30-November 11
  • Reputation: 0

Posted 23 January 2012 - 06:55 AM

yeah thanks :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

3 User(s) are reading this topic
0 members, 3 guests, 0 anonymous users