为什么这个手风琴菜单中的子菜单不能保持打开状态?

时间:2022-12-01 14:57:56

I adapted a menu from CSS MenuMaker, and I've been hunting for why it behaves differently. It is here. When you click on the menu item, the sub-menu opens, but if the mouse remains over the clicked menu item, the rest doesn't move down to make room, and the sub-menu doesn't use the styling it is supposed to. Instead it uses the styling of a top-level menu item.

我改编了一个来自CSS MenuMaker的菜单,我一直在寻找它为什么表现不同。是这里。当您单击菜单项时,子菜单会打开,但如果鼠标仍然在单击的菜单项上,其余部分不会向下移动以腾出空间,子菜单不会使用它所假定的样式至。相反,它使用*菜单项的样式。

HTML

HTML

<div class="col-2 col-m-2 accordian darkYellow roundLeft">
    <p class="title">Melt-in-Place Station (MIP)
    </p>
    <ul>
        <li class="has-sub"><a><span>Reference Docs</span></a>
            <ul>
            <li><a href="http://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/20150000305.pdf">
                    <span>Melted regolith construction</span></a></li>
            </ul>
        </li>
        <li class="has-sub"><a><span>Forum Threads</span></a></li>
        <li class="has-sub"><a><span>Models</span></a></li>
    </ul>
</div>

CSS

CSS

.accordian,
.accordian ul,
.accordian li,
.accordian a {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  text-decoration: none;
  line-height: 1;
  font-size: 1em;
  position: relative;
  color: yellow;
}
p.title {
    font-weight: 600;
    padding: 16px 6px;
}
.accordian a {
  line-height: 1.3;
  cursor: pointer;
}
.accordian {
    font-weight: 400;
    color: rgba(255, 255, 0, 1);
    text-align: right;
}
.accordian > ul > li {
  margin: 0 0 2px 0;
}
.accordian > ul > li:last-child {
  margin: 0;
}
.accordian > ul > li > a {
  font-size: 1.1em;
  display: block;
  background: rgba(50, 50, 50, 0.6);
}
.accordian > ul > li > a > span {
  display: block;
  padding: 6px 10px;
}
.accordian > ul > li > a:hover {
  text-decoration: none;
}
.accordian > ul > li.active {
  border-bottom: none;
}
.accordian > ul > li.active > a {
    background: #333;
}
.accordian > ul > li.has-sub > a span {
  background: url(http://www.moonwards.com/img/iconPlus.png) no-repeat left center; 
}
.accordian > ul > li.has-sub.active > a span {
  background: url(http://www.moonwards.com/img/iconMinus.png) no-repeat left center; 
}
/* Sub menu */
.accordian ul ul {
  padding: 5px 12px;
  display: none;
}
.accordian ul ul li {
  padding: 3px 0;
}
.accordian ul ul a {
  display: block;
  font-size: 0.9em;
  font-weight: 400italic;
}

jQuery

jQuery的

( function( $ ) {
$( document ).ready(function() {
$('.accordian > ul > li > a').click(function() {
  $('.accordian li').removeClass('active');
  $(this).closest('li').addClass('active'); 
  var checkElement = $(this).next();
  if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
    $(this).closest('li').removeClass('active');
    checkElement.slideUp('normal');
  }
  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
    $('.accordian ul ul:visible').slideUp('normal');
    checkElement.slideDown('normal');
  }
  if($(this).closest('li').find('ul').children().length == 0) {
    return true;
  } else {
    return false;   
  }     
});
});
} )( jQuery );

I assume this is some dumb thing, but any help appreciated.

我认为这是一些愚蠢的事情,但任何帮助表示赞赏。

2 个解决方案

#1


4  

After inspecting I think I found the culprit. Try edit your CSS like this:

经过检查,我认为我找到了罪魁祸首。尝试像这样编辑你的CSS:

li:hover ul {
  display: block;
  position: relative; /* relative, not absolute */
  top: 100%;
  left: -10px;
  background-color: rgba(50, 50, 0, 1);
  font-size: 13px;
}

#2


0  

Your problem is not entirely CSS. I just figured out that the LI's height is covering the dropdown. However, if you reduce the height of the LI, the next LI doesn't open the amount it needs to. A quick patch would be to apply this CSS below however, I would much rather get the JQuery guys here to help you out.

你的问题不完全是CSS。我只知道李的高度正在覆盖下拉列表。但是,如果减小LI的高度,则下一个LI不会打开它所需的量。一个快速的补丁将是在下面应用这个CSS,但我更愿意让JQuery人员在这里帮助你。

  .accordian ul ul {
    padding: 5px 12px;
    display: none;
    margin-top: 0px;
    z-index: 99999;
    position: relative;
    background: none;
 }

p.s. This solves the problem you asked, but you need to tweak the font and size so that there is no difference

附:这解决了您提出的问题,但您需要调整字体和大小,以便没有区别

#1


4  

After inspecting I think I found the culprit. Try edit your CSS like this:

经过检查,我认为我找到了罪魁祸首。尝试像这样编辑你的CSS:

li:hover ul {
  display: block;
  position: relative; /* relative, not absolute */
  top: 100%;
  left: -10px;
  background-color: rgba(50, 50, 0, 1);
  font-size: 13px;
}

#2


0  

Your problem is not entirely CSS. I just figured out that the LI's height is covering the dropdown. However, if you reduce the height of the LI, the next LI doesn't open the amount it needs to. A quick patch would be to apply this CSS below however, I would much rather get the JQuery guys here to help you out.

你的问题不完全是CSS。我只知道李的高度正在覆盖下拉列表。但是,如果减小LI的高度,则下一个LI不会打开它所需的量。一个快速的补丁将是在下面应用这个CSS,但我更愿意让JQuery人员在这里帮助你。

  .accordian ul ul {
    padding: 5px 12px;
    display: none;
    margin-top: 0px;
    z-index: 99999;
    position: relative;
    background: none;
 }

p.s. This solves the problem you asked, but you need to tweak the font and size so that there is no difference

附:这解决了您提出的问题,但您需要调整字体和大小,以便没有区别