Skip to content
WJunction - Webmaster Forum

[jquery] resizing images

Status
Not open for further replies.

Connor

Established Member
136
2011
63
[solved][jquery] resizing images

EDIT: This issue is resolved.

I'm trying to resize the thumbnail images on a page proportionally using this jquery. For some reason though this code is not working. Why?:
Code:
$(document).ready(function() {
	$("#gallery div.thumbnails img").load(function() {
		$("#gallery div.thumbnails img").each(function() {
			var width = $(this).width;
			var height = $(this).height;
			var max_width = $(this).css("max-width");
			
			if (width > max_width) {
				var ratio = width / height;
				$(this).width(width);
				$(this).height(max_width / ratio);
			}
		});
	});
});

Entire html markup is:
Code:
<!DOCTYPE html>
<html>
<head>
<title>image gallery</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$("#gallery div.thumbnails img").load(function() {
		$("#gallery div.thumbnails img").each(function() {
			var width = $(this).width;
			var height = $(this).height;
			var max_width = $(this).css("max-width");
			
			if (width > max_width) {
				var ratio = width / height;
				$(this).width(width);
				$(this).height(max_width / ratio);
			}
		});
	});
});
</script>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>
	<div id="gallery">
		<ul class="slider_navigation">
			<li><a href="#"><span><</span> Previous</a></li>
			<li> | </li>
			<li><a href="#">Next <span>></span></a></li>
		</ul>
		
		<div class="main_image">
			<img src="#" alt="MainImg" />
		</div>
		
		<div class="thumbnails">
			<img src="http://www.wjunction.com/images/cfg.png" alt="Image 1" />
			<img src="http://www.wjunction.com/images/cleangui.png" alt="Image 2" />
			<img src="http://www.wjunction.com/images/dislike.png" alt="Image 3" />
			<img src="http://www.wjunction.com/images/like.png" alt="Image 4" />
			<img src="http://www.wjunction.com/images/tempwatermark.png" alt="Image 5" />
		</div>
	</div> <!-- #gallery -->
</body>
</html>

The css is:
Code:
#gallery div.thumbnails img{
	max-width:200px;
	border:1px solid gray;
}

Help is much appreciated.
 
Last edited:

6 comments

Nevermind I figured it out. I needed to add a maxheight for the image and set the height based on that ratio too. Problem solved.
 
i always thought that when something was resolved it was closed?
my bad, maybe i won't report it to get closed then haha
 
Status
Not open for further replies.

About the author

C
Established Member · Joined
136
Messages
63
Reactions
28
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom