views:

434

answers:

2

Hey everyone,

I have a couple of div elements contained in one container div, and I would like to slide through them, showing only one at a time, and after a certain timeout slide the currently visible div to the left using an animation, and slide in the next div from the right.

My HTML looks something like this:

<div id="topMaps" style="height: 225px; overflow: hidden;">
  <div>First slide - some content here</div>
  <div>Second slide - some content here</div>
  <div>... slide - some content here</div>
  <div>n'th slide - some content here</div>
</div>

I'm using the following script for this:

var currentSlide = 1;
var numSlides = -1;
var slideSpeed = 1000;
var timePerSlide = 3500;

function nextSlide() {
    $('#topMaps div:nth-child(' + currentSlide + ')').hide("slide", { direction: "left" }, slideSpeed);
    currentSlide += 1;
    if (currentSlide >= numSlides)
        currentSlide = 1;
    $('#topMaps div:nth-child(' + currentSlide + ')').show("slide", { direction: "left" }, slideSpeed);

    setTimeout(nextSlide, timePerSlide);
}

$(document).ready(function() {
    numSlides = $('#topMaps div').length;
    setTimeout(nextSlide, timePerSlide);
});

But it's not working. When the nextSlide function is called for the first time, Firefox reports the following error:

o.easing[this.options.easing || (o.easing.swing ? "swing" : "linear")] is not a function
http://localhost/templates/front/default/js/jquery.js
Line 19

And when I add $('#topMaps div:nth-child(' + currentSlide + ')') in the Watch panel in Firebug, it returns a collection of 10 elements? Reall not sure what's going on.

Maybe someone can help me out?

edit I figured out the problem with the selector, I needed to use $('#topMaps>div:nth-child(' + currentSlide + ')') (notice the >). It's still not working though.

+1  A: 

While not the answer you were wanting, have you considered using the Cycle Plugin?

Yacoby
I'm using the cycle plugin elsewhere, but this specific slideshow doesn't contain ``<img />`` elements, so that won't work.
Aistina
Why not? The Cycle site has examples not using images (Intermediate Demos (Part 2))
Yacoby
Well I'll be darned! Thank you :)
Aistina
A: 

I might recommend setting an ID for each div and calling it in the nextSlide() js function,test the currentSlide variable and choose the next div It's not a good programming thingy but you can at least make it work till you figure out what's wrong with $('#topMaps div:nth-child(' + currentSlide + ')')

Kheu
I fixed the problem with the selector (check my edit), but still getting the same error.
Aistina
could you debug and check where's the error? on hide, show, length or setTimeout?
Kheu
The error occurs when calling hide.
Aistina
ok good, now since you fixed the selector error, you should getting reference to the div right? try this div .hide() only and check if u get the same error
Kheu
Yes, it works when using plain hide() and show().
Aistina
so that's where your problem is! after a quick look at jQuery hide documentation are you sure about the parameters? it says hide() only takes speed and callback function, use slideUp(slideSpeed) and slideDown(slideSpeed)anyway its been a long time since i used jQuery maybe im wrong! reply please with the result
Kheu
I took it from here: http://docs.jquery.com/UI/Effects/Slide But what you're saying makes sense with the error message I'm getting... I think. Anyways, I know about slideUp and slideDown, but I'd prefer them to slide to the left.
Aistina