Skatox
Skatox

Reputation: 4284

Best way to zoom/scale/resize multiple images with Javascript?

I have a list of images inside divs and a jquery ui slider, when user slides the bar the images should be resized/zoomed (whatever you want to call it), i tried selecting all the images and changing the css's width and height using jquery. It works but it's very slow and not very fluid.

I read that browsers sucks resizing images, but there should be a way to improve performance or making it fluid, at least with the images that are being loading.

Here is my html markup:

<div class="file-list">
 <div class="file-cell">
    <div class="thumb-wrapper">
      <img width="100" height="100" title="mytitle" alt="myalt" src="image1.jpg">
    </div>
  </div>
  <div class="file-cell">
    <div class="thumb-wrapper">
      <img width="100" height="100" title="mytitle" alt="myalt" src="image1.jpg">
    </div>
  </div>
</div>

Here is the CSS code:

 div.file-cell {
    margin: 10px;
    float: left;
    padding: 8px;
    width: 100px;
    background: none;
    margin-bottom: 5px;
    cursor: pointer;
}

Here is the Javascript code:

jQuery().ready(function(){
var images = jQuery('.thumb-wrapper img'); //caches the query when dom is ready

jQuery( "#slider" ).slider({
    step: 13,
    min: 70,
    max: 200,
    value: 100,
    slide: function(event, ui){
              //i think if i return while resizing increases performace, 
              //confirm this please
              if(resizing) 
                     return;
               resizing = true;
               var size = CELL_WIDTH * ui.value / 100;
               size = size + 'em';
               images.css({
                     'width': size,
                     'height': size
               });
               resizing = false;
             }
    });                
    }

Note: should i remove the width and height attributes for the image? they are there because they generated by wordpress.

Upvotes: 0

Views: 5874

Answers (1)

jfriend00
jfriend00

Reputation: 707308

Your slider has a step value in it which specifically makes it NOT be fluid. The slider will only fire values in increments of your step. If you want to see smaller resize increments, then you will need to make the step value smaller.

And, you have no animation going between sizes so the change from one size to the next will be jerky at best. If you want a smoother change from one size to the next, you can use jQuery animation to animate from one size to the next. Even this animation isn't guaranteed to be smooth (it depends upon the capabilities of the host computer), but it is more likely to be smooth.

You can see a working example of that implementation with animation here: http://jsfiddle.net/jfriend00/eW53L/

The resizing flag you're using isn't helping you in any way. This piece of javascript is single threaded and not asynchronous so you won't get another slide callback until this one is done executing so the resizing flag doesn't actually accomplish anything.

That code with the animation is here:

jQuery(document).ready(function() {

    var images = jQuery('.thumb-wrapper img'); //caches the query when dom is ready
    var CELL_WIDTH = 5;
    var ASPECT = 1.5;

    jQuery( "#slider" ).slider({
        step: 5,
        min: 70,
        max: 200,
        value: 100,
        slide: function(event, ui) {
            var size = (CELL_WIDTH * ui.value / 100) + "em";
            images.stop(true).animate({width: size * ASPECT, height: size}, 250);
        }
    });

});​

If the images still won't animate size smoothly enough for you, then a work-around is to animate a border outline only (which animates with much less CPU) and then, when the user pauses moving the slider for some short time, you change the images to the new size of the border outline. This technique was often used in the days of less powerful computers.

Upvotes: 5

Related Questions