Search code examples
jquerymenubindunbindfading

Jquery fadeIn fadeOut list of divs with active divs


What I'm trying to achieve is a list of links with some separate divs. at the moment the fading is working but is "jumping on click" and the active in both <ul id="links"> and #description doesn't work properly. Could you help me please?

here is my eg.:

http://jsfiddle.net/mdamC/100/


Solution

  • H, I suggest you do this.

    Also changed from descriptions to classes as the ideal code requires unique ids per page

    http://jsfiddle.net/mdamC/104/

    html

    <ul id="links">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul><br/><br/>
    <div id="description">
    <ul class="descriptions">
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
    </ul>
    <ul class="descriptions">
        <li>2</li>
        <li>2</li>
        <li>2</li>
        <li>2</li>
        <li>2</li>
    </ul>
    
    <ul class="descriptions">
        <li>3</li>
        <li>3</li>
        <li>3</li>
        <li>3</li>
        <li>3</li>
    </ul>
    <ul class="descriptions">
        <li>4</li>
        <li>4</li>
        <li>4</li>
        <li>4</li>
        <li>4</li>
    </ul>
    <ul class="descriptions">
        <li>5</li>
        <li>5</li>
        <li>5</li>
        <li>5</li>
        <li>5</li>
    </ul></div>
    

    CSS

    .active{color:red}
    
    #description{position:relative}
    .descriptions{position:absolute; top:0px;}
    

    JS

    $("ul#links li").unbind().bind('click', function(e) {
        if (e)
            e.preventDefault();
        $(this).toggleClass('active');
        $("ul.descriptions").fadeOut();
    
        var index = $(this).index();;
        $("ul.descriptions").eq(index).fadeIn();
    
        if (!e) e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    }, function(e) {
        $("ul.descriptions").stop().fadeOut();
    });
    
    
    
    $("ul.descriptions").hide();
    if ($(this).next().is(':hidden')) {
        $(this).next().removeClass('active');
    }
    
    $('ul#links li').click(function(e) {
        e.preventDefault();
        $('ul#links li').removeClass('active');
        $(this).addClass('active');
    });