Question

I am using the accordion for navigation and i am using jquery. My problem is that when the page loads all the headers should be closed by default, when i mouseover any header its siblings will open,when i mouseout its automatically closes.

<ul id="accordion">
    <li>
        <a href="#" class="history_heading" rel="history_heading">HISTORY</a>
        <ul>
            <li><a href="#">Link One</a></li>
            <li><a href="#">Link Two</a></li>
            <li><a href="#">Link Three</a></li>
            <li><a href="#">Link Four</a></li>
            <li><a href="#">Link Five</a></li>
        </ul>
    </li>

    <li>
        <a href="#" class="geography_heading" rel="geography_heading">GEOGRAPHY</a>
        <ul>
            <li><a href="#">Link One</a></li>
            <li><a href="#">Link Two</a></li>
            <li><a href="#">Link Three</a></li>
            <li><a href="#">Link Four</a></li>
            <li><a href="#">Link Five</a></li>
        </ul>
    </li>
</ul>

Please help me
The jquery code which is given below is for the click function but i want as per my requirements
jquery code

$(document).ready(function () {

    $('#accordion li').children('ul').hide();
    $('.history_heading').parent().addClass('active').find('ul').show();

    $('#accordion a').click(function () {

        $(this).parent().siblings('.active').removeClass('active').find('ul').slideUp('fast');

        if ($(this).parent().hasClass('active')) {
            $(this).next('ul').slideUp('fast');
            $(this).parent().removeClass('active');
        } else {
            $(this).next('ul').slideDown('fast');
            $(this).parent().addClass('active');
        }

    });




});
Was it helpful?

Solution

Simple

jQuery(function ($) {
    $('#accordion li').hover(function () {
        $(this).find('ul').stop(true, true).slideDown()
    }, function () {
        $(this).find('ul').stop(true, true).slideUp()
    }).find('ul').hide()

})

Demo: Fiddle

Or

jQuery(function ($) {
    $('#accordion li').hover(function () {
        $(this).find('ul').stop(true, true).slideToggle()
    }).find('ul').hide()
})

OTHER TIPS

$('#accordion > li').each(function(){
    var accHolder = $(this),
        acc = accHolder.find('> ul');

    accHolder.hover(function(){
        acc.slideDown();
    }, function(){
        acc.slideUp();
    });
});

Hi posted a fiddle for u hope it helps...http://jsbin.com/aKIpEZu/2/edit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top