首页 > 解决方案 > 当您向下移动鼠标单击时,下拉菜单会消失

问题描述

我正在使用 Dreamweaver CS6帮助一个组织建立他们的网站。为了节省菜单栏的时间,我在 Menu Maker 使用了一个。它比我想要的菜单按钮宽一点,但我进去并手动减少了填充,以便它们都适合菜单栏,并增加了字体。下拉菜单在站点预览中效果很好,但是当我将所有 html 代码插入到我想要的栏时,将 script.js 和 styles.css 复制到文件夹中,当你移动你的下拉菜单时消失了鼠标光标向下选择一个。

遇到困难后,我提交了支持票。然后我做了一些搜索,看到其他用户抱怨没有支持,没有联系。

我试图确定编码问题出在哪里,导致下拉菜单消失,现在在代码视图中变得盲目。;(我真的可以使用并希望得到一些建议。

HTML

<div id="cssmenu"> 
          <ul>
            <li class="active"><a href="#" target="_blank"><i class="fa fa-fw fa-home"></i> 
              Home</a></li>
            <li class="has-sub"><a href="about.htm"><i class="fa fa-fw fa-bars"></i> 
              About Us</a> 
              <ul>
                <li><a href="contact.htm">Contact Us</a></li>
                <li><a href="org.htm">Organization</a></li>
              </ul>
            </li>
            <li class="has-sub"><a href="mission.htm">Our Wild Bison Mission</a> 
              <ul>
                <li><a href="rationale.htm">Rationale</a></li>
                <li><a href="mtplans.htm">Montana Plans for Wild Bison</a></li>
                <li><a href="wood.htm">Wood Bison Example</a></li>
                <li><a href="supporters.htm">Supporters</a></li>
                <li><a href="polls.htm">Polls</a></li>
                <li><a href="fwpdocs.htm">FWP Documents</a></li>
                <li><a href="scientists.htm">Scientist's Letter of Support</a></li>
              </ul>
            </li>
            <li class="has-sub"><a href="wildness.htm">Why Wildness?</a> 
              <ul>
                <li><a href="biodef.htm">Biological Definitions</a></li>
                <li><a href="legaldef.htm">Legal Definitions</a></li>
              </ul>
            </li>
            <li class="has-sub"><a href="legal.htm">Legal Issues</a> 
              <ul>
                <li><a href="wbandlaw.htm">Wild Bison and the Law</a></li>
              </ul>
            </li>
            <li class="has-sub"><a href="habitat.htm">CMR/APR Bison Habitat</a> 
              <ul>
                <li><a href="cmr.htm">CMR</a></li>
                <li><a href="apr.htm">APR</a></li>
              </ul>
            </li>
            <li><a href="https://mtwildbison.blogspot.com/">News</a></li>
            <li><a href="contribute.htm">Contribute</a></li>
          </ul>
        </div>

爪哇

(function($){
$(document).ready(function(){

$('#cssmenu').prepend('<div id="menu-button">Menu</div>');
  $('#cssmenu #menu-button').on('click', function(){
    var menu = $(this).next('ul');
    if (menu.hasClass('open')) {
      menu.removeClass('open');
    } else {
      menu.addClass('open');
    }
});

});
})(jQuery);

CSS

@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700);
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a,
#cssmenu #menu-button , 
#cssmenu{
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  line-height: 1;
  display: block;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#cssmenu > ul:after , 
#cssmenu:after{
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}
#cssmenu #menu-button {
  display: none;
}
#cssmenu {
    z-index: 100;
    width: 1102px;
    font-family: 'Open Sans', Helvetica, sans-serif;
    background: #d5d69e;
    background: -moz-linear-gradient(top, #dedfb4 0%, #c9ca81 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #dedfb4), color-stop(100%, #c9ca81));
    background: -webkit-linear-gradient(top, #dedfb4 0%, #c9ca81 100%);
    background: -o-linear-gradient(top, #dedfb4 0%, #c9ca81 100%);
    background: -ms-linear-gradient(top, #dedfb4 0%, #c9ca81 100%);
    background: linear-gradient(to bottom, #dedfb4 0%, #c9ca81 100%);
}
#cssmenu > ul {
    background: url('images/bg.png');
    box-shadow: inset 0px -3px 0px rgba(0, 0, 0, 0.05);
}
#cssmenu.align-right > ul > li {
  float: right;
}
#cssmenu > ul > li {
  float: left;
  display: inline-block;
}
#cssmenu.align-center > ul {
  float: none;
  text-align: center;
  font-size: 0;
}
#cssmenu.align-center > ul > li {
  float: none;
}
#cssmenu.align-center ul ul {
  text-align: left;
}
#cssmenu > ul > li > a {
    border-right: 1px solid rgba(80, 80, 80, 0.12);
    text-decoration: none;
    font-size: 11px;
    font-weight: 700;
    color: #515223;
    text-transform: uppercase;
    letter-spacing: 1px;
    padding-top: 14px;
    padding-right: 10px;
    padding-bottom: 19px;
    padding-left: 10px;
}
#cssmenu > ul > li:hover > a,
#cssmenu > ul > li > a:hover,
#cssmenu > ul > li.active > a {
  color: #ffffff;
  background: #d0d193;
  background: rgba(0, 0, 0, 0.1);
}
#cssmenu > ul > li.has-sub > a {
    padding-right: 40px;
}
#cssmenu > ul > li.has-sub > a::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border: 6px solid transparent;
  border-top-color: #515223;
  right: 17px;
  top: 20.5px;
}
#cssmenu > ul > li.has-sub.active > a::after,
#cssmenu > ul > li.has-sub:hover > a {
  border-top-color: #ffffff;
}
#cssmenu ul ul {
  position: absolute;
  left: -9999px;
  top: 60px;
  z-index: 9999;
  padding-top: 6px;
  font-size: 13px;
  opacity: 0;
  -webkit-transition: top 0.2s ease, opacity 0.2s ease-in;
  -moz-transition: top 0.2s ease, opacity 0.2s ease-in;
  -ms-transition: top 0.2s ease, opacity 0.2s ease-in;
  -o-transition: top 0.2s ease, opacity 0.2s ease-in;
  transition: top 0.2s ease, opacity 0.2s ease-in;
}
#cssmenu.align-right ul ul {
  text-align: right;
}
#cssmenu > ul > li > ul::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border: 5px solid transparent;
  border-bottom-color: #ffffff;
  top: -4px;
  left: 20px;
}
#cssmenu.align-right > ul > li > ul::after {
  left: auto;
  right: 20px;
}
#cssmenu ul ul ul::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border: 5px solid transparent;
  border-right-color: #ffffff;
  top: 11px;
  left: -4px;
}
#cssmenu.align-right ul ul ul::after {
  border-right-color: transparent;
  border-left-color: #ffffff;
  left: auto;
  right: -4px;
}
#cssmenu > ul > li > ul {
  top: 120px;
}
#cssmenu > ul > li:hover > ul {
  top: 49px;
  left: 0;
  opacity: 1;
}
#cssmenu.align-right > ul > li:hover > ul {
  left: auto;
  right: 0;
}
#cssmenu ul ul ul {
  padding-top: 0;
  padding-left: 6px;
}
#cssmenu.align-right ul ul ul {
  padding-right: 6px;
}
#cssmenu ul ul > li:hover > ul {
  left: 180px;
  top: 0;
  opacity: 1;
}
#cssmenu.align-right ul ul > li:hover > ul {
  left: auto;
  right: 100%;
  opacity: 1;
}
#cssmenu ul ul li a {
  text-decoration: none;
  font-weight: 400;
  padding: 11px 25px;
  width: 180px;
  color: #777777;
  background: #ffffff;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1), 1px 1px 1px rgba(0, 0, 0, 0.1), -1px 1px 1px rgba(0, 0, 0, 0.1);
}
#cssmenu ul ul li:hover > a,
#cssmenu ul ul li.active > a {
  color: #333333;
}
#cssmenu ul ul li:first-child > a {
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
}
#cssmenu ul ul li:last-child > a {
  border-bottom-left-radius: 3px;
  border-bottom-right-radius: 3px;
}
#cssmenu > ul > li > ul::after {
  position: absolute;
  display: block;
}
#cssmenu ul ul li.has-sub > a::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border: 4px solid transparent;
  border-left-color: #777777;
  right: 17px;
  top: 14px;
}
#cssmenu.align-right ul ul li.has-sub > a::after {
  border-left-color: transparent;
  border-right-color: #777777;
  right: auto;
  left: 17px;
}
#cssmenu ul ul li.has-sub.active > a::after,
#cssmenu ul ul li.has-sub:hover > a::after {
  border-left-color: #333333;
}
#cssmenu.align-right ul ul li.has-sub.active > a::after,
#cssmenu.align-right ul ul li.has-sub:hover > a::after {
  border-right-color: #333333;
  border-left-color: transparent;
}
@media all and (max-width: 800px), only screen and (-webkit-min-device-pixel-ratio: 2) and (max-width: 1024px), only screen and (min--moz-device-pixel-ratio: 2) and (max-width: 1024px), only screen and (-o-min-device-pixel-ratio: 2/1) and (max-width: 1024px), only screen and (min-device-pixel-ratio: 2) and (max-width: 1024px), only screen and (min-resolution: 192dpi) and (max-width: 1024px), only screen and (min-resolution: 2dppx) and (max-width: 1024px) {
  #cssmenu {
    background: #d5d69e;
  }
  #cssmenu > ul {
    display: none;
  }
  #cssmenu > ul.open {
    display: block;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
  }
  #cssmenu.align-right > ul {
    float: none;
  }
  #cssmenu.align-center > ul {
    text-align: left;
  }
  #cssmenu > ul > li,
  #cssmenu.align-right > ul > li {
    float: none;
    display: block;
  }
  #cssmenu > ul > li > a {
    padding: 18px 25px 18px 25px;
    border-right: 0;
  }
  #cssmenu > ul > li:hover > a,
  #cssmenu > ul > li.active > a {
    background: rgba(0, 0, 0, 0.1);
  }
  #cssmenu #menu-button {
    display: block;
    text-decoration: none;
    font-size: 13px;
    font-weight: 700;
    color: #515223;
    padding: 18px 25px 18px 25px;
    text-transform: uppercase;
    letter-spacing: 1px;
    background: url('images/bg.png');
    cursor: pointer;
  }
  #cssmenu ul ul,
  #cssmenu ul li:hover > ul,
  #cssmenu > ul > li > ul,
  #cssmenu ul ul ul,
  #cssmenu ul ul li:hover > ul,
  #cssmenu.align-right ul ul,
  #cssmenu.align-right ul li:hover > ul,
  #cssmenu.align-right > ul > li > ul,
  #cssmenu.align-right ul ul ul,
  #cssmenu.align-right ul ul li:hover > ul {
    left: 0;
    right: auto;
    top: auto;
    opacity: 1;
    width: 100%;
    padding: 0;
    position: relative;
    text-align: left;
  }
  #cssmenu ul ul li {
    width: 100%;
  }
  #cssmenu ul ul li a {
    width: 100%;
    box-shadow: none;
    padding-left: 35px;
  }
  #cssmenu ul ul ul li a {
    padding-left: 45px;
  }
  #cssmenu ul ul li:first-child > a,
  #cssmenu ul ul li:last-child > a {
    border-radius: 0;
  }
  #cssmenu #menu-button::after {
    display: block;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    content: '';
    position: absolute;
    height: 3px;
    width: 22px;
    border-top: 2px solid #515223;
    border-bottom: 2px solid #515223;
    right: 25px;
    top: 18px;
  }
  #cssmenu #menu-button::before {
    display: block;
    content: '';
    position: absolute;
    height: 3px;
    width: 22px;
    border-top: 2px solid #515223;
    right: 25px;
    top: 28px;
  }
  #cssmenu > ul > li.has-sub > a::after,
  #cssmenu ul ul li.has-sub > a::after {
    display: none;
  }
}  

标签: javascriptjqueryhtmlcss

解决方案


正如所指出的,hover当鼠标移向子菜单时,菜单和子菜单之间的间隙导致菜单按钮失去其状态。

最简单的解决方案是调整top子菜单的属性以消除间隙。尝试将其添加到文件的最底部styles.css

#cssmenu > ul > li.has-sub:hover > ul {
    top: 37px;
}

编辑:

更好的解决方案是使用悬停意图来延迟子菜单的关闭,以便用户有足够的时间将指针移动到它的预期位置


推荐阅读