I ended up putting a lot of work into this one, chaining was a lot trickier than I thought at first glance. I've never used it in quite this manner before :D
You can definitely loop through the slideElements
array. Chaining the element events to each other is the tricky part. I ended up with a loop closure function that loops through each slideElement
and chains the previous item in the list to the current item:
<!-- Working example of chained sliding divs -->
<div class="mySlideElements" style="background: red" id="slide1">Slide 1</div>
<div class="mySlideElements" style="background: yellow">Slide 2</div>
<div class="mySlideElements" style="background: green">Slide 3</div>
<div class="mySlideElements" style="background: purple">Slide 4</div>
<script type="text/javascript">
var slideElements = $$('.mySlideElements');
slideElements.set('slide');
for(var i = 1; i < slideElements.length; i++) (function(c1,c2){
c1.get('slide').chain(function(){c2.slide()});
})(slideElements[i-1],slideElements[i]);
</script>
<input type="button" onclick="$('slide1').slide()" value="slide"/>
Calling get('slide')
returns the Fx.Slide object that was set on each element, which you can then chain to a function. My Mootools-fu wasn't strong enough to figure out how to chain it directly to the slide
property of the next element (which should be a function, but it didn't seem to work), so I ended up with the anonymous function you see inside the chain()
call, which is almost as good. The loop closure function(c1,c2)
was necessary to prevent the local i
loop variable from being out of scope by the time the chained anonymous function got called.
It's set up as a chain reaction for when the first element's slide
function gets triggered. If you needed the whole thing to cascade whenever any element is clicked, you'd probably want to set every element's onClick
event to fire the first element's slide()
and not their own.
Anyway, the code above is a working example. I hope the results are what you were looking for.