Text Size
Wednesday, February 22, 2012
Get a look down
JavaScript >> How to create a very simple accordion using mootools
Tuesday, 07 June 2011 15:41

How to create a very simple accordion using mootools

Written by Nabil Sadki
Rate this item
(0 votes)

We gonna see in this article how it is easy to create our own simple accordion system using the Mootools library. Of course there are lot of Mootools plugins that allow us to create such system in our websites, but sometimes we need really a simple way to do the job, and in this case, this tutorial can make your life easier. So take your keyboards and let's do it!

This is the HTML part of our demo:

 

1
2
3
4
5
6
7
8
9
10
11
12
<h3 class="title">My personal blog!</h3>
<div class="zone">
    Visit <a href="http://www.nabilsadki.com" target="_blank">my blog</a> and find lot of usefull tips and tricks about Joomla!, Drupal, Mootools, JQuery and all web stuff!
</div>
<h3 class="title">Best places in Morocco</h3>
<div class="zone">
    If you are interested about visiting Morocco, take a look at <a href="http://www.coins.ma" target="_blank">Coins.ma</a>, A nice website where you can find all the best places that you can visit in Morocco!
</div>
<h3 class="title">KeniClan Family</h3>
<div class="zone">
    Wanna listen to a Moroccan rap guru's? So check this out! Visit <a href="http://www.keniprod.com" target="_blank">the official website</a> of the KeniClan Family and viber your woofer with Twifly, Sintao, Masty and K-nibal's voices!
</div>

 

Note that the <h3> tags with class "title", contains the titles of the tabs, and the <div> tags with class "zone" contains the content of each tab. This structure h3.title>div.zone is very important to make this little script work.

The CSS part:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
h3{
    width: 365px;
    height: 30px;
    background: url(../images/next.png) 5px 3px no-repeat #666;
    margin: 0 0 5px 0;
    padding: 5px 5px 5px 40px;
    cursor: pointer;
    color: #fff;
    border-radius: 5px;
    text-align: left;
}
h3.active{
    background: url(../images/down.png) 5px 3px no-repeat #666;
}
.zone{
    margin: 5px 0 5px 0;
    padding: 5px;
    border: 1px solid #ccc;
    width: 398px;
    border-radius: 5px;
}

 

You can download the full demonstration package in the bottom of the article.

And finaly the most important step, the JS part :

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Implement isHidden/isVisible methods into Element class
Element.implement({
    //Method to check if an element is hidden
    isHidden: function(){
        var w = this.offsetWidth, h = this.offsetHeight,
        force = (this.tagName === 'TR');
        return (w===0 && h===0 && !force) ? true : (w!==0 && h!==0 && !force) ? false : this.getStyle('display') === 'none';
    },
 
    // Method to check if an element is visible
    isVisible: function(){
        return !this.isHidden();
    }
});
 
window.addEvent('domready', function() {
    //Close all content blocks
    $('.zone').setStyle('display', 'none');
    //Add class 'active to the first tab, and show his content
    $('.accordion').getFirst('.title').addClass('active').getNext().setStyle('display', 'block');
 
    //Init the title click
    $('.title').addEvent('click', function(e){
        // Check if the content of the clicked title is hidden, if yes :
        if ( $(this).getNext().isHidden() ) {
            //Close content of the active title and remove the active class
            $('.title').removeClass('active').getNext().setStyle('display', 'none');
            //Add class 'active to the clicked title, and show his content
            $(this).toggleClass('active').getNext().setStyle('display', 'block');
        }
    });
});

 

As you can see, the first section in the script implement a little method called "isHidden", which allow us to check if an element is hidden or not. This should be a native method in the Mootools core library, but i think it's implemented in the version 1.3
More infos : This script is based on version 1.2 of Mootools, there is not much difference with version 1.1 of Mootools for this script.
PreviewView demo page                Download sourcesDownload
Last modified on Wednesday, 08 June 2011 09:17
Nabil Sadki

Nabil Sadki

Hello, I am Nabyl, a Freelance web developer and a joomla addict. I have made many sites using the latter, but also patches for the core of this CMS. I like HipHop culture and world of warcraft. You can find find me on Twitter when i'm free...

Website: www.coins.ma E-mail: This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Add comment



Usefull tricks