0

我正在尝试在 Drupal 中添加一个 JavaScript,用于 Zen 7.x-5.x 主题的移动导航。我要添加的 JavaScript 可以在http://codepen.io/micahgodbolt/pen/czwer上找到

我知道我需要使用此处描述的 JavaScript:https ://stackoverflow.com/a/14526812

但是作为 JavaScript 新手,我无法让它工作。我最近的尝试在下面的第 18 行给出了“未定义不是函数”(其中声明了 adjustMenu 变量):

(function($) {
    Drupal.behaviors.mybehavior = {
        attach: function () {
        var ww = $(window).width();

        $(document).ready(function() {
            $(".nav li a").each(function() {
                if ($(this).next().length > 0) {
                    $(this).addClass("parent");
                    };
                })

                $(".toggleMenu").click(function(e) {
                    e.preventDefault();
                    $(this).toggleClass("active");
                    $(".nav").toggle();
                });
                adjustMenu();
            })

            $(window).bind('resize orientationchange', function() {
                ww = $(window).width();
                adjustMenu();
            });

            var adjustMenu = function() {
                if (ww < 768) {
                // if "more" link not in DOM, add it
                if (!$(".more")[0]) {
                $('<div class="more">&nbsp;</div>').insertBefore($('.parent')); 
                }
                    $(".toggleMenu").css("display", "inline-block");
                    if (!$(".toggleMenu").hasClass("active")) {
                        $(".nav").hide();
                    } else {
                        $(".nav").show();
                    }
                    $(".nav li").unbind('mouseenter mouseleave');
                    $(".nav li a.parent").unbind('click');
                $(".nav li .more").unbind('click').bind('click', function() {

                        $(this).parent("li").toggleClass("hover");
                    });
                } 
                else if (ww >= 768) {
                // remove .more link in desktop view
                $('.more').remove(); 
                    $(".toggleMenu").css("display", "none");
                    $(".nav").show();
                    $(".nav li").removeClass("hover");
                    $(".nav li a").unbind('click');
                    $(".nav li").unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() {
                        // must be attached to li so that mouseleave is not triggered when hover over submenu
                        $(this).toggleClass('hover');
                });
            }
        }
    }
    };
})(jQuery);

我非常感谢任何帮助,因为默认情况下 Zen 7.x-5.x 主题中没有移动导航,并且添加不是为 Drupal 编写的 JavaScript 也不是很容易。

4

1 回答 1

0

我通过一些调整让它工作,包括将 rhteadjustMenu 变量的声明移到它被调用的位置上方。整个 javscript 现在看起来像这样:

(function ($) {
  Drupal.behaviors.mobile_menu_toggle = {
    attach: function (context, settings) {
        var ww = $(window).width();

        var adjustMenu = function() {
                if (ww < 768) {
                // if "more" link not in DOM, add it
                if (!$(".more")[0]) {
                $('<div class="more">&nbsp;</div>').insertBefore($('.parent')); 
                }
                    $(".toggleMenu").css("display", "inline-block");
                    if (!$(".toggleMenu").hasClass("active")) {
                        $(".menu").hide();
                    } else {
                        $(".menu").show();
                    }
                    $(".menu li").unbind('mouseenter mouseleave');
                    $(".menu li a.parent").unbind('click');
                $(".menu li .more").unbind('click').bind('click', function() {

                        $(this).parent("li").toggleClass("hover");
                    });
                } 
                else if (ww >= 768) {
                // remove .more link in desktop view
                $('.more').remove(); 
                    $(".toggleMenu").css("display", "none");
                    $(".menu").show();
                    $(".menu li").removeClass("hover");
                    $(".menu li a").unbind('click');
                    $(".menu li").unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() {
                        // must be attached to li so that mouseleave is not triggered when hover over submenu
                        $(this).toggleClass('hover');
                    });
                }
            }

        $(document).ready(function() {
            $(".menu li a").each(function() {
                if ($(this).next().length > 0) {
                    $(this).addClass("parent");
                    };
                })

                $(".toggleMenu").click(function(e) {
                    e.preventDefault();
                    $(this).toggleClass("active");
                    $(".menu").toggle();
                });
                adjustMenu();
            })

            $(window).bind('resize orientationchange', function() {
                ww = $(window).width();
                adjustMenu();
            });


        }
    };
})(jQuery);
于 2013-07-08T10:25:12.760 回答