【问题标题】:Close responsive menu after click点击后关闭响应式菜单
【发布时间】:2017-05-16 20:27:11
【问题描述】:

我有以下代码,可在切换单击图标 (#burger) 时打开和关闭响应式菜单 (#burger-nav)。菜单选项跳转到同一 HTML 页面中的锚点,但菜单保持打开状态。

如何在单击选项时关闭响应式菜单,以便当用户返回页面顶部时,菜单会以原始状态关闭?

$(document).ready(function(){

// ----- Responsive menu -----

    ( function( $ ) {
     /* Run this code when the #mob-nav-toggle link has been tapped or clicked */
     $( '#burger' ).on( 'touchstart click', function(e) {
      e.preventDefault();

      var $body = $( 'body' ),
          $page = $( '#wrapper' ),
          $menu = $( '#burger-nav' ),

          /* Cross browser support for CSS "transition end" event */
          transitionEnd = 'transitionend webkitTransitionEnd otransitionend MSTransitionEnd';

      /* When the toggle menu link is clicked, animation starts */
      $body.addClass( 'animating' );

      /* Determine the direction of the animation and add the correct direction class depending on whether the menu was already visible. */
      if ( $body.hasClass( 'menu-visible' ) ) {
       $body.addClass( 'up' );
      } else {
       $body.addClass( 'down' );
      }

      /* When the animation (technically a CSS transition) has finished, remove all animating classes and either add or remove the "menu-visible" class depending whether it was visible or not previously. */
      $page.on( transitionEnd, function() {
       $body
        .removeClass( 'animating down up' )
        .toggleClass( 'menu-visible' );

       $page.off( transitionEnd );
      } );
     } );
    } )( jQuery );

HTML:


<div id="wrapper">
    <header>            
        <nav id="burger-nav">
            <ul>
                <li><a href="" title="">parent link</a>
                    <ul>
                        <li><a href="#bookmark" title="">link</a></li>
                        <li><a href="#bookmark" title="">link</a></li>
                        <li><a href="#bookmark" title="">link</a></li>
                        <li><a href="#bookmark" title="">link</a></li>
                    </ul>
                </li>
                <li><a href="" title="">parent link</a></li>
                <li><a href="" title="">parent link</a></li>
                <li><a href="" title="">parent link</a></li>
            </ul>
        </nav>
    </header>
</div>

【问题讨论】:

  • 张贴html!!
  • 请添加你的plunker代码
  • @bRIMOs - 完成:)
  • 无法弄清楚问题出在哪里!请您在plunker 中创建一个工作示例

标签: javascript jquery


【解决方案1】:

将您的匿名事件处理函数更改为命名函数: 然后,您可以在单击汉堡按钮或菜单项链接时调用它

(function ($) {

        function menuToggle(e) {
                e.preventDefault();

                var $body = $('body'),
                        $page = $('#wrapper'),
                        $menu = $('#burger-nav'),
                        /* Cross browser support for CSS "transition end" event */
                        transitionEnd = 'transitionend webkitTransitionEnd otransitionend MSTransitionEnd';

                /* When the toggle menu link is clicked, animation starts */
                $body.addClass('animating');

                /* Determine the direction of the animation and add the correct direction class depending on whether the menu was already visible. */
                if ($body.hasClass('menu-visible')) {
                        $body.addClass('up');
                } else {
                        $body.addClass('down');
                }

                /* When the animation (technically a CSS transition) has finished, remove all animating classes and either add or remove the "menu-visible" class depending whether it was visible or not previously. */
                $page.on(transitionEnd, function () {
                        $body
                                .removeClass('animating down up')
                                .toggleClass('menu-visible');

                        $page.off(transitionEnd);
                });

                $(document).ready(function () {
                        // ----- Responsive menu -----
                        /* Run this code when the #mob-nav-toggle link has been tapped or clicked */
                        $('#burger').on('touchstart click', menuToggle);
                        $('#burger-nav').on('touchstart click', 'a', menuToggle);

                });
        }}
)(jQuery);

【讨论】:

  • 那 2 个 sn-ps 的代码去哪儿了?我无法让您的解决方案正常工作? javascript 保存在 HTML 头部引用的 functions.js 文件中,其中 html sn-p 显然在页面内。你的两个sn-ps需要放在哪里?并且 HTML 中没有任何更改可以使用您的代码?
  • 对不起,我介绍了几个语法错误,我现在已经编辑了我的答案
  • 我用你上面的 javascript sn-p 替换了上面的,但它仍然有效吗?我错过了什么吗? (顺便说一句,没有 html 更改)
  • 可能,原来你原来的js有语法错误,但我的回答是基于它有效的假设
  • 那么你有没有在你的最终伙伴那里工作?我不能把它带到这里:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多