简体   繁体   English

如何创建像手风琴一样的移动设备的WordPress响应式菜单?

[英]how to create WordPress responsive menu working like accordion for mobile devices?

I have created my WP responsive menu and it has several level sub menus, and I want to only show or slideToggle the particular sub-menu when tap or click on it. 我创建了我的WP响应式菜单,它有几个级别的子菜单,我想只在点击或点击它时显示或滑动切换特定的子菜单。

I have put the span into the anchor tag to prevent redirecting to URL, so only click or tap on the span will slide the menu. 我已将跨度放入锚标记以防止重定向到URL,因此只需单击或点击跨度即可滑动菜单。

this is menu structure. 这是菜单结构。

<nav id="nav" class="menu-menu-container" style="display: block;">
    <ul id="menu" class="menu">
        <li id="menu-item-7" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-has-children menu-item-7"><a href="http://localhost/wordpress/">Home<span class="arrow"> </span></a>
            <ul class="sub-menu">
                <li id="menu-item-8" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8"><a href="http://localhost/wordpress/sample-page/">Sample Page</a></li>
                    <li id="menu-item-32" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-32"><a href="http://localhost/wordpress/activate/">activate<span class="arrow"> </span></a>
                    <ul class="sub-menu">
                        <li id="menu-item-33" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-33"><a href="http://localhost/wordpress/register/">register</a></li>
                        <li id="menu-item-34" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-34"><a href="http://localhost/wordpress/resetpass/">resetpass<span class="arrow"> </span></a>
                            <ul class="sub-menu">
                                <li id="menu-item-37" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-37"><a href="http://localhost/wordpress/forgotpass/">forgotpass</a></li>
                                <li id="menu-item-38" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-38"><a href="http://localhost/wordpress/activate/">activate</a></li>
                                <li id="menu-item-39" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-39"><a href="http://localhost/wordpress/logout/">logout</a></li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    <li id="menu-item-10" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-10"><a href="http://localhost/wordpress/member/">member</a></li>
    <li id="menu-item-36" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-36"><a href="http://localhost/wordpress/login/">login</a></li>
    </ul>
</nav>

This is jquery. 这是jquery。

$('.menu > li.menu-item-has-children > a').append('<span class="arrow"> </span>');
$('.sub-menu li.menu-item-has-children > a').append('<span class="arrow"> </span>');


$('.menu > li.menu-item-has-children > a > span.arrow').click(function(){
    var txt = $(this).text() == ' ' ? '  ' : ' ';
    $(this).text(txt);
    $('.menu > li > .sub-menu').slideToggle('fast');
    if ($(this).text() == ' ') {
        setTimeout(function(){
        $('.menu > li > .sub-menu').removeAttr('style');
        },1000);
    } else {
    }
    return false;
});

for 2nd sub-menu. 第二个子菜单。

$('.sub-menu li.menu-item-has-children > a > span.arrow').click(function(){
    var txt = $(this).text() == ' ' ? '  ' : ' ';
    $(this).text(txt);
    $('.sub-menu > li > .sub-menu').slideToggle('fast');
    if ($(this).text() == ' ') {
        setTimeout(function(){
        $('.sub-menu > li > .sub-menu').removeAttr('style');
        },1000);
    } else {
    }
    return false;
});

So for 3rd and so on..... 所以对于第3等......

$('.sub-menu .sub-menu li.menu-item-has-children > a > span.arrow').click(function(){..
...

例

you can see what I have tried using this method I have to make this rules for every single selector. 你可以看到我尝试使用这种方法我必须为每个选择器制定这个规则。

so I don't want this, is there any way I can do this all by only just in single click handler, so I when only user click or tap on the particular sub-menu the only that menu will slide or toggle. 所以我不想要这个,有没有办法我只能通过单击处理程序来完成这一切,所以当我只有用户点击或点击特定的子菜单时,只有那个菜单会滑动或切换。

Few solutions. 几个解决方案。

(About walkers: http://codex.wordpress.org/Function_Reference/wp_nav_menu ) (关于步行者: http//codex.wordpress.org/Function_Reference/wp_nav_menu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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