当子页面处于活动状态时,保持手风琴菜单打开

时间:2015-07-30 13:04:25

标签: javascript css menu accordion

我有手风琴菜单,其中包含用于折叠和点击href链接的字段,当我点击父链接并将我带到另一个页面时,它会保持打开状态,但是当我点击菜单中的任何子页面时它会关闭。 当我点击任何子页面链接时,如何保持菜单也打开?



(function ($) {
  $(document).ready(function () {
    $('#cssmenu li.has-sub').prepend('<span class="holder"></span>');
    $('#cssmenu li.has-sub > .holder').on('click', this, function() {
      var element = $(this).parent('li');
      if (element.hasClass('open')) {
        element.removeClass('open');
        element.find('li').removeClass('open');
        element.find('ul').slideUp();
      }
      else {
        element.addClass('open');
        element.children('ul').slideDown();
        element.siblings('li').children('ul').slideUp();
        element.siblings('li').removeClass('open');
        element.siblings('li').find('li').removeClass('open');
        element.siblings('li').find('ul').slideUp();
      }
    });

    // Opens "active" Menu Item(s)
    $('#cssmenu li.active').addClass('open').children('ul').slideDown();
  });
})(jQuery);
&#13;
#cssmenu, #cssmenu ul, #cssmenu ul li, #cssmenu ul li a {
    margin: 0;
    padding: 0;
    border: 0;
    list-style: none;
    line-height: 1;
    display: block;
    position: relative;
}
#cssmenu {
    width: 200px;
    font-family: Helvetica, Arial, sans-serif;
    color: #ffffff;
}
#cssmenu ul ul {
    display: none;
}
.align-right {
    float: right;
}
#cssmenu > ul > li > a {
    padding: 15px 20px;
    border-top: 1px solid #1682ba;
    cursor: pointer;
    font-size: 14px;
    font-weight: bold;
    text-decoration: none;
    color: #ffffff;
    background: #36aae7;
}
.has-sub > a:after {
    content:"";
    width: 0;
    height: 0;
    float: right;
    border-right: 10px solid transparent;
    border-left: 10px solid transparent;
    margin: 10px 10px 0 0;
    border-top: 6px solid #5F5F5F;
}
.holder{
border: 1px solid transparent;
padding: 11px 26px;
z-index: 3;
position: absolute;
margin: 11px 12px 0 0;
right: 0;
background: rgba(255, 65, 65, 0.24);
	}
#cssmenu > ul > li.open > a {
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.15), 0 1px 1px rgba(0, 0, 0, 0.15);
    border-bottom: 1px solid #1682ba;
}
#cssmenu > ul > li:last-child > a, #cssmenu > ul > li.last > a {
    border-bottom: 1px solid #1682ba;
}
#cssmenu ul ul li a {
    cursor: pointer;
    border-bottom: 1px solid #32373e;
    border-left: 1px solid #32373e;
    border-right: 1px solid #32373e;
    padding: 10px 20px;
    z-index: 1;
    text-decoration: none;
    font-size: 13px;
    color: #eeeeee;
    background: #49505a;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}
#cssmenu ul ul li:first-child > a {
    box-shadow: none;
}
#cssmenu ul ul ul li:first-child > a {
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}
#cssmenu ul ul ul li a {
    padding-left: 30px;
}
#cssmenu ul ul li.has-sub > a::after {
    content:"";
    width: 0;
    height: 0;
    float: right;
    border-right: 10px solid transparent;
    border-left: 10px solid transparent;
    margin: 10px 10px 0 0;
    border-top: 6px solid #5F5F5F;
}
&#13;
<type="text/javascript">
    </script>
    <script src="http://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript"></script>
    <div id='cssmenu'>
        <ul>
            <li><a href='#'><span>Home</span></a>
            </li>
            <li class='active has-sub'><a href='www.DontHideMePlease.com'><span>Products</span></a>

                <ul>
                    <li class='has-sub'><a href='#'><span>Product 1</span></a>

                        <ul>
                            <li><a href='#'><span>Sub Product</span></a>
                            </li>
                            <li class='last'><a href='#'><span>Sub Product</span></a>
                            </li>
                        </ul>
                    </li>
                    <li class='has-sub'><a href='#'><span>Product 2</span></a>

                        <ul>
                            <li><a href='#'><span>Sub Product</span></a>
                            </li>
                            <li class='last'><a href='#'><span>Sub Product</span></a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li><a href='#'><span>About</span></a>
            </li>
            <li class='last'><a href='#'><span>Contact</span></a>
            </li>
        </ul>
    </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

试试这个

(function ($) {
  $(document).ready(function () {
if( $('#cssmenu li li').hasClass('active') ) { $("#cssmenu ul ul").toggleClass('dblock'); }
    $('#cssmenu li.has-sub').prepend('<span class="holder"></span>');
    $('#cssmenu li.has-sub > .holder').on('click', this, function() {
      var element = $(this).parent('li');
      if (element.hasClass('open')) {
        element.removeClass('open');
        element.find('li').removeClass('open');
        element.find('ul').slideUp();
      }
      else {
        element.addClass('open');
        element.children('ul').slideDown();
        element.siblings('li').children('ul').slideUp();
        element.siblings('li').removeClass('open');
        element.siblings('li').find('li').removeClass('open');
        element.siblings('li').find('ul').slideUp();
      }
    });


  });
})(jQuery);

#cssmenu .dblock {
  display:  block;
}