0

I'm struggling with this javascript at the moment.

$(document).ready(function () {
        var visible = false;
        var body = false;

        $("body").mouseup(function () {
            if (visible) {
                $(this).parent().find("ul.subnav").slideUp('slow');
                visible = false;
                $(this).removeClass("clicked-background");
                body = true;
            }
        });

        $("ul.topnav li a").click(function () { //When trigger is clicked...
            var menu = $(this).parent().find('ul.subnav');

            if (!visible && !body) {
                $(this).parent().find("ul.subnav").slideDown('fast').show();
                visible = true;
                $(this).addClass("clicked-background");
            }
            // else if (visible) 
            //{
            //   $(this).parent().find("ul.subnav").slideUp('slow');
            //   visible = false;
            //   $(this).removeClass("clicked-background");
            // }

            body = false;
        });

    });

I wanted to add the feature, so if you clicked outside the menu/navigation the dropdown would hide. The current problem with this code is, that if you click the menu and then click outside the menu - you have to double click the menu again to get it showen. This is caused by the body variable is set too 'True' ofc.

I made the body variable trying to fix the problem if you clicked the menu - and then clicked the same link again. The menu would first open correctly, and then close and open again. Soo main problem is. My navigation open -> closes -> open

4

2 回答 2

3

不要使用全局变量。通过检查检查实际元素是否可见

.is(':visible');

您可以在现有的各种选择器上使用它。

于 2012-04-13T20:00:56.983 回答
0

我很想使用“现在可见”菜单的 onmouseout 作为选择事件..

我不认为在 body 标签之外运行事件是一个好方法。

流量应该是..

click (menu button or link)
show menu
set onmouseout for button and menu on click
onmouseout, remove onmouseout events
于 2012-04-13T20:03:36.047 回答