views:

33

answers:

2

Hello

I'm currently building a simple "jQuery Image Slider" but it does not work as i hoped. It's incredible slow and unresponsive, and the last image does not do anything.

URL: http://fusionmedia.dk/jquery/

What is the problem?

Thanks in advance

A: 

It depeneds on various things , are you loading all the images upfront and even size of the images matters alot.

All your subsequent requests should be cached in the browser.

you should use some caching mechanisims if possible.

gov
I can't see why the image size means something, when the only thing i am doing is change the left position
Mikkel
A: 

Specify a faster speed. Its defaulting to a slower speed.

$('#gallery').delegate('img', 'mouseover', function() {

                $this = $(this);

                for(var i = 0; i <= $this.siblings().size(); i++) {

                    if($this.index() > i) {

                        $this.siblings().eq(i).stop().animate({ left: (i * 50) + 'px' }, 300);

                    } else {

                        $this.siblings().eq(i).stop().animate({ left: ((i * 50) + 500) + 'px' }, 300);

                    }

                }

            });

EDIT:

You have 2 really bad problems for speed.

1: You are running a time costly loop every time they hover.

2: You are calling $this.siblings() too many times. Cache that.

Here is an example of how to better implement some of this, I still have you loop inside the hover event, you should try and get that moved out.

$(function(){

         $('#gallery').find('img').each(function(){

            $this = $(this);
            $this.css('left', $this.index() * 50 + 'px');

         });

         $('#gallery').delegate('img', 'mouseover', function(){

            $this = $(this);
            var $sibs = $this.siblings();
            for (var i = 0; i <= $sibs.size(); i++) {

               if ($this.index() > i) {

                  $sibs.eq(i).stop().animate({
                     left: (i * 50) + 'px'
                  });

               } else {

                  $sibs.eq(i).stop().animate({
                     left: ((i * 50) + 500) + 'px'
                  });
               }
            }
         });
      });
Dale
Well the problem isn't the animation transition time, rather the space between, it does not feel as snappy as i would like it. It should be like this: http://lars.wi12.ots.dk/jquery/galleri.html
Mikkel
Ah, I see. You need to stop all other animations before calling the next. Notice how on the snappy site it doesn't have to complete animating all the others when mousing over like yours does. I edited my answer
Dale
Now it almost works, but the last one still doesn't appear, and sometimes none of them work (when i hover over one the one beside it shows)
Mikkel
I revised my second bit of code. It seems to work pretty well for me.
Dale
It still doesn't work properly for me: http://www.fusionmedia.dk/jquery/fail.mov
Mikkel