So I have managed to create a recursive mootools accordion, which is something I have wanted to get done for a long time.
The secret ingredient to get this made up was some CSS3 selectors and a bit of recursion.
You basically just pass in a class for the toggles, a class for the elements, and a parent container to reference.
The usage ends up fairly simple:
new rAccordion('container', 'toggle', 'element');
In the example, container is the id of the parent that the classes are in, toggle is the class that each toggle has, and element is the class that each element has. A fourth argument can be passed, which is the options argument for the mootools Accordion class.
There are some kinks thatstill need working out. There are inherit problems with just jamming accordions inside of each other. The way mootools creates the accordion requires quite a bit of inline styles, which include defined heights. These heights become problematic when accordions inside accordions need to expand or contract, but their parent element has a defined height and overflow which will not allow the newly expanded portion to be seen.
I do have something of a solution in place, but it feels a little hacky, but I am not sure it will get any better unless I rewrite the accordion.
If anyone has any ideas on how to address this issue, please drop me a comment.
Here is the class:
var rAccordion = new Class({
initialize: function(container, toggleClass, elementClass, options){
this.container = container;
this.tClass = toggleClass;
this.eClass = elementClass;
this.options = options;
this.selector = '#' + this.container + ' > .';
this.makeAccordion();
},
makeAccordion: function(){
new Accordion(
$$(this.selector+this.tClass),
$$(this.selector+this.eClass),
this.options
).addEvents({
// The onActive and onComplete events added to the stack here to
// attempt to address some of the css issues.
'onActive': function(toggle){
if(toggle.getParent().getStyle('height') != 0)
toggle.getParent().setStyle('height', '');
},
'onComplete': function(a){
if ($defined(a)) {
var height = 0;
a.getParent().getChildren().each(function(e){
height = height + e.offsetHeight;
});
if(height != a.getParent().offsetHeight && a.getParent().offsetHeight != 0)
a.getParent().setStyle('height','');
}
}
});
this.selector += this.eClass + ' > .';
if($defined($$(this.selector)[0]))
this.makeAccordion();
}
});
May 13th, 2009 at 3:31 pm
awesome script! i’ve been looking for exactly this script!!!!! kudos!
May 13th, 2009 at 4:31 pm
@Jorge
this is one i made more recently, which is a tad better, and does gradients too:
http://github.com/re5et/trickyInc/blob/master/images/trickyImg.php
May 14th, 2009 at 8:50 pm
Pretty frikkin cool. Works like a charm.
Do you have examples for JPG and GIF too?
Thanks.
May 14th, 2009 at 9:43 pm
@DouG Molidor
i suspect that you are slightly confused. Neither JPG or GIF support alpha channel transparency.
June 23rd, 2009 at 12:00 pm
@atom i’ll try that out, thanks!
October 25th, 2009 at 2:26 am
Nice snippit! We are using it on our splash page for a new project coming up. Cheers!
November 28th, 2009 at 12:37 pm
博客写的很好,有很多值得学习的地方,给博主留个言,记得回访哦。
December 3rd, 2009 at 11:32 am
博主写的真的很好,在这里给你留个言了,我的网站比较前卫,博主不要介意啊,以后多多关照。
February 17th, 2010 at 5:59 am
Great code, thanks!
February 19th, 2010 at 9:22 am
青岛卓众(www.chinajoyzone.com)给博主拜年来了,新的一年,祝博主前途似锦。