简体   繁体   中英

Need help changing menu from hover to click

--- Original Hover code ---

function bind_dropdown() {
    $('div#quick_nav div.search_dropdown').on('hover', function() {
        var thisid = $(this).attr('id').split('_');
        var this_action = thisid[1];
        var this_width = 950;
        var add_class = 'dropdown_full';

        if(this_action == 4) {
            this_width = 200;
            add_class = 'dropdown_small';
        }

        if($('div#quick_nav div#dropdown_'+escape(this_action)).is(':visible')) {
            $('div#quick_nav div#dropdown_'+escape(this_action)).addClass(add_class).slideUp(100);
        }
        else{
            $('div#quick_nav div.dropdown').hide();
            $('div#quick_nav div#dropdown_'+escape(this_action)).addClass(add_class).slideDown(100).width(this_width);
        }
        return false;
    });
}

--- End Original Code ---

I did try and change hover to click. It works however to close the menu you now have to click on the menu icon again. Any help would be greatly appreciated. I would like to be able to click to show menu and when the mouse moves off the menu it would close automatically.

Thank you in advance

J

Use the below code

function bind_dropdown() {
    $('div#quick_nav div.search_dropdown').on('click', function() {
        var thisid = $(this).attr('id').split('_');
        var this_action = thisid[1];
        var this_width = 950;
        var add_class = 'dropdown_full';

        if(this_action == 4) {
            this_width = 200;
            add_class = 'dropdown_small';
        }

        if($('div#quick_nav div#dropdown_'+escape(this_action)).is(':visible')) {
            $('div#quick_nav div#dropdown_'+escape(this_action)).addClass(add_class).slideUp(100);
        }
        return false;
    }).on("mouseleave", function() {
        var thisid = $(this).attr('id').split('_');
        var this_action = thisid[1];
        var this_width = 950;
        var add_class = 'dropdown_full';

        if(this_action == 4) {
            this_width = 200;
            add_class = 'dropdown_small';
        }
        $('div#quick_nav div.dropdown').hide();
        $('div#quick_nav div#dropdown_'+escape(this_action)).addClass(add_class).slideDown(100).width(this_width);
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM