質問

I have this code that I am trying to get to work with mobile devices. I have a wordpress menu with sublevels, .sub-menu, that get built for the menu. I am trying to get the main menu to toggle a drop down of the first level and then all proceeding levels have a class of .sub-menu. This code doesn't seem to be working at all. I've had it work where i could toggle the sub-menu's but then the menu would disappear and i would have to re-click to open it. Where am I going wrong with this?

$(function() {

        $('#menu-menu li').bind("touchstart", function() {
            $(this).siblings('.sub-menu').toggle();
        });
        $('.sub-menu').bind("touchstart", function() {
            $(this).nextUntil('.sub-menu').toggle();
        });
});
役に立ちましたか?

解決

Ok, I think I got this worked out. I'll post this here in-case anyone else ever looks no need it in the future.

~This requires the mobile jquery library~

This took me almost two days to figure out, but I'm new to jquery/javascript so it may be a little sloppy. I'm always open to simplifying this if I can. This is great for wordpress as it builds off the rebuilt menu's

//this is the touch screen mainMenu function
$(function touchNav() {
$(function mainMenu () {
    $(document).on('vmousedown', 'li', function(e) {
        var jthis = $(this);
        var jchildren = jthis.children('ul.sub-menu');
        if (e.target !== this) {
            return;
        } else {
            jthis.siblings().find('ul.sub-menu').slideUp('fast');
            jchildren.slideToggle('slow');
        }
        setTimeout(function() {
            if (jchildren.css("display") == "none") {
                jthis.find(".sub-menu").css("display", "none");
            };
        }, 700);
    });
});
//if click is not on mainMenu closeNav
$(document).on('vmousedown', function (e) {
    var jcontainer = $("#mainMenu");
    if (!jcontainer.is(e.target) && jcontainer.has(e.target).length === 0)
    {  jcontainer.find("ul.sub-menu").slideUp();
    }
}); 
});

Fiddle Here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top