我正在尝试在Drupal中添加一个JavaScript,用于Zen 7.x-5.x主题的移动导航。我正在尝试添加的JavaScript可以在http://codepen.io/micahgodbolt/pen/czwer
上找到我知道我需要使用此处描述的JavaScript:https://stackoverflow.com/a/14526812
但是对JavaScript不熟悉我无法让它发挥作用。我的最新尝试在下面的第18行给出了“undefined is not a function”(其中声明了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"> </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并不是一件容易的事。
答案 0 :(得分:0)
我通过一些调整来实现它,包括将rhte adjustMenu变量的声明移到上面调用它的位置。整个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"> </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);