tags:

views:

65

answers:

7

I have multiple images that have the same class attribute and I want to resize all the images uniformly even though they all have different original widths - how can I do this using jQuery?

<img id=img1 class="ResizeMe" src="img1.gif" width="100" height="100" /><br />

<img id=img2 class="ResizeMe" src="img2.gif" width="200" height="200" /><br />

<img id=img3 class="ResizeMe" src="img3.gif" width="150" height="150" /><br />

<input id="Button1" type="button" value="Shrink Images by 10%" />
<input id="Button2" type="button" value="Grow Images by 10%" />

TIA!

UPDATE: Here's the final working code I used for anyone hitting this in the future...

        function OnClientValueChanging(sender, args) 
        {
            var zoomPercentage = args.get_newValue() / 100;

            $(".ApplyZoomEffect").each(function () {
                var newWidth = parseFloat($(this).attr('OriginalWidth')) * zoomPercentage;
                var newHeight = parseFloat($(this).attr('OriginalHeight')) * zoomPercentage;
                $(this).css('width', newWidth + "px").css('height', newHeight + "px");
            });
        }
A: 

$('.ResizeMe').attr("width",value)

luca
The example above increases or decreases the size by a percentage, your solution is great for an absolute width for all images but if they have different initial sizes then it fails.
Lazarus
A: 
$(".ResizeMe").attr('width', '100');
David Morton
As luca, you didn't read the *whole* question :)
Lazarus
A: 

$("img").each(function(i){ this.width *= 1.1; this.width *= 1.1; })

Sergej Andrejev
One too many ".attr("height", this.width * 1.1)" perhaps?
Lazarus
Yes, I didn't check whether it was correct. I have fixed the code now
Sergej Andrejev
+1  A: 
$('#Button2').click(function() {
    $('.ResizeMe').each(function() {
        $(this).attr('width', parseInt($(this).attr('width')) * 110 / 100)
    });
});

Button1 is left as an exercise for the reader.

kevingessner
You could just multiply by 1.1 rather than 110/100 ;)
Lazarus
+3  A: 

Try this style:

$("#Button1").click(function() {
  $(".ResizeMe").width(function(i, w) { return w * 0.9 })
                .height(function(i, h) { return h * 0.9 });
});

$("#Button2").click(function() {
  $(".ResizeMe").width(function(i, w) { return w * 1.1 })
                .height(function(i, h) { return h * 1.1 });
});

Since jQuery 1.4.1, you can pass a function to .width() and .height() to accomplish this more cleanly.

Nick Craver
Woops, had the buttons backwards, fixed now.
Nick Craver
Very nice Nick!
Doug Neiner
Nick, I'm using telerik RadControls which uses v1.3.2. So does this mean i cannot use this technique?
EdenMachine
@EdenMachine - Not on your current version, however Service Pack 2 for RadControls is out, just install that upgrade and you can move to jQuery 1.4.
Nick Craver
@Nick - It's actually the Q1 2010 and it's still in beta at the moment so I'll upgrade after the final release: http://www.telerik.com/help/aspnet-ajax/combobox-what-is-new.htmlhttp://www.telerik.com/community/labs/radcontrols-for-asp-net-ajax-q1-2010-beta.aspx
EdenMachine
A: 

Like this:

$('#Button1').click(function() {
    $('.ResizeMe').width(function(index, oldWidth) { return oldWidth * .9; })
                  .height(function(index, oldHeight) { return oldHeight * .9; });
});
SLaks
+1  A: 

try this:

  $('.ResizeMe').each(function(){
       var w=$(this).width;
       var newWidth=//calculate new width depending on which button pressed
       $(this).attr('width',newWidth);
  });
TheVillageIdiot