views:

1041

answers:

1

I'm writing some rather basic jQuery code to create a slideshow from a set of images in a DIV. The CSS ensures that all images are stacked on top of each other. The script looks at the active image in the set, then moves the NEXT image's z-index up (via CSS class) and fades it in from 0.0 opacity. Rinse, repeat. It's actually based on some code I found at John Raasch's blog.

I'm trying to tweak the code so that when the opacity fade is complete, it looks to the next slideshow DIV on the page, advances it by one image, and continues down the page until all DIVs have sequentially moved forward one frame. Then, I'd like it to wait some interval, and do it all over again.

I've tried producing the following code, but it's breaking on me, telling me that target.next is not a function...

jQuery

$(document).ready(function(){
 setInterval( "slideSwitch('div:first')", 5000 );
});

function slideSwitch(target) {
 var nextTarget = target.next();
 var active = $(target+' img.active');

 if (active.length == 0)
  active = $(target+' img:last');

 var next = active.next().length ? active.next() : $(target+' img:first');

 active.addClass('last');

 next.css({opacity: 0.0})
  .addClass('active')
  .animate({opacity: 1.0}, 500, function() {
   active.removeClass('active last');
   slideSwitch(nextTarget);
  });
};
+1  A: 

your "target" is not a jquery object. you need to do $(target).next()

Jason
Jason, thanks for jumping in! I changed the nextTarget variable like so:var nextTarget = $(target).next();Now I'm having a new issue. When the JS executes slideSwitch(nextTarget) in the .animate callback, I get the following error in my Firebug console:uncaught exception: Syntax error, unrecognized expression: [object Object]Any ideas?
Chris C.
i think it's because you're now passing a jquery object through recursion back to your function, so you're effectively trying to get the jquery object of a jquery object, so it's kind of confused. if you pass the element or the cass as the target in your animation function, it should work because it will set the next target based on what you're sending it.
Jason