使用JQuery显示子菜单

时间:2017-03-02 08:59:37

标签: javascript jquery html

我需要一些帮助,在页面加载后,子菜单在2s内显示,而不是在用户点击它时。我正在使用 JQuery 。该文件是网站的核心。我需要它保持开放。

这是我目前的代码,我试图改变它。点击事件,但它没有用。

handleSidenarAndContentHeight();功能会在子菜单出现后调整菜单项的大小。

jQuery('.page-sidebar li > a').on('click', function (e) {
    if ($(this).next().hasClass('sub-menu') === false) {
        return;
    }
    var parent = $(this).parent().parent();
    parent.children('li.open').children('a').children('.arrow').removeClass('open');
    parent.children('li.open').children('a').children('.arrow').removeClass('active');
    parent.children('li.open').children('.sub-menu').slideUp(350);
    parent.children('li').removeClass('open');
    parent.children('li').removeClass('active');

    var sub = jQuery(this).next();
    if (sub.is(":visible")) {
        jQuery('.arrow', jQuery(this)).removeClass("open");
        jQuery(this).parent().removeClass("active");
        sub.slideUp(350, function () {
            handleSidenarAndContentHeight();
        });
    } else {
        jQuery('.arrow', jQuery(this)).addClass("open");
        jQuery(this).parent().addClass("open");
        sub.slideDown(350, function () {
            handleSidenarAndContentHeight();
        });
    }

    e.preventDefault();
});

2 个答案:

答案 0 :(得分:3)

使用2秒超时应该可以解决问题!

jQuery(document).ready(function(){
  // Open Parent here

  setTimeout(function(){
    // Open Child here
  }, 2000)
});

答案 1 :(得分:1)

您可以使用简单的javascript函数setTimeout函数。

代码如下:

setTimeout(function() {yourFunctyion();}. delayTimeInMiliseconds);

这将在秒数(以毫秒为单位)后调用您的函数。

我还使用了一个插件。它有oneTime和everyTime方法。

jQuery timers plugin