悬停时显示下拉菜单 - 不工作

时间:2018-01-07 05:19:32

标签: javascript jquery html css drop-down-menu

简单问题:我有一个导航菜单,当点击' +'时会显示子菜单。在菜单项旁边。但是当它悬停时,它也应该显示下拉菜单。

我认为问题可能是:

a)我没有针对正确的元素

b)切换方法错误了吗?

javascript新手,所以我不确定这里有什么问题。

我相信我的剧本错了。不确定是什么问题,请帮忙



/* NAVIGATION ON CLICK */

// Primary menu drop down (mobile)
$(".dropdown-toggle").click(function() {
  $(this).parent().find(".menu-test-container #primary-menu").toggleClass("toggle-on");
});

// Sub menu drop down
$(".dropdown-toggle").click(function() {

  $(this).parent().find(".sub-menu:first").toggleClass("toggle-on");
});


/* NAVIGATION ON HOVER */
// Sub menu drop down 
$(".dropdown-toggle").hover(function() {

  $(this).parent().find(".menu-test-container ul li .sub-menu:first").toggleClass("toggle-on");
});

/*
	# HEADER
 */

*,
html {
  margin: 0;
  font-size: 16px;
}

.site-header {
  background-color: black;
  padding: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.main-navigation {
  padding: 2rem;
  background-color: red;
}


/*.menu-toggle, .main-navigation ul ul, .main-navigation ul ul ul {
 	display: none;
 }
*/


/* Menu styles */

.main-navigation .sub-menu {
  display: none;
}

.sub-menu.toggle-on {
  display: block;
}

.main-navigation ul,
.main-navigation ul ul,
.main-navigation ul ul ul {
  list-style: none;
}

.main-navigation ul li a {
  color: #fff;
  text-decoration: none;
}


/* First Highrarchy */

.main-navigation ul {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.main-navigation ul li {
  margin-right: 2rem;
}


/*.dropdown-toggle:hover {
	display: block;
}

*/

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header id="masthead" class="site-header">
  <div class="site-branding">
    <div class="site-branding-text">
      <p class="site-title"><a href="http://localhost/wordpress/" rel="home">LOGO</a></p>
    </div>
  </div>
  <!-- .site-branding -->

  <nav id="site-navigation" class="main-navigation">
    <button class="dropdown-toggle" aria-expanded="false">Primary Menu</button>
    <div class="menu-test-container">
      <ul id="primary-menu" class="menu" aria-expanded="true">
        <li id="menu-item-2035" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2035">
          <a href="http://localhost/wordpress/services/">Services</a>
          <button class="dropdown-toggle" aria-expanded="false">
							<span class="dropdown-symbol">+</span>
						</button>
          <ul class="sub-menu">
            <li id="menu-item-2076" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2076">
              <a href="http://localhost/wordpress/asd/">Commercial</a>
              <button class="dropdown-toggle toggled-on" aria-expanded="true">
									<span class="dropdown-symbol">-</span>
								</button>
              <ul class="sub-menu toggled-on">
                <li id="menu-item-2082" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2082">
                  <a href="http://localhost/wordpress/asd/">Rural</a>
                  <button class="dropdown-toggle toggled-on" aria-expanded="true">
											<span class="dropdown-symbol">-</span>
										</button>
                  <ul class="sub-menu toggled-on">
                    <li id="menu-item-2081" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2081">
                      <a href="http://localhost/wordpress/services/">Electrical</a>
                      <button class="dropdown-toggle" aria-expanded="false">
													<span class="dropdown-symbol">+</span>
												</button>
                      <ul class="sub-menu">
                        <li id="menu-item-2079" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2079">
                          <a href="http://localhost/wordpress/gallery/">Residential</a>
                          <button class="dropdown-toggle" aria-expanded="false">
															<span class="dropdown-symbol">+</span>
														</button>
                          <ul class="sub-menu">
                            <li id="menu-item-2083" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2083"><a href="http://localhost/wordpress/news-updates/">News Updates</a></li>
                          </ul>
                        </li>
                      </ul>
                    </li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
        </li>
        <li id="menu-item-2084" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2084">
          <a href="http://localhost/wordpress/asd/">Projects</a>
        </li>
        <li id="menu-item-2045" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2045">
          <a href="http://localhost/wordpress/blog/">Blog</a>
          <button class="dropdown-toggle" aria-expanded="false">
							<span class="dropdown-symbol">+</span>
						</button>
          <ul class="sub-menu">
            <li id="menu-item-2078" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2078">
              <a href="http://localhost/wordpress/news-updates/">About</a>
              <button class="dropdown-toggle" aria-expanded="false">
									<span class="dropdown-symbol">+</span>
								</button>
              <ul class="sub-menu">
                <li id="menu-item-2099" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2099">
                  <a href="http://localhost/wordpress/news-updates/">News Updates</a>
                </li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </nav>
  <!-- #site-navigation -->
  <link rel="stylesheet" type="text/css" href="style.css">
</header>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

只有将鼠标悬停在按钮上时才会打开菜单。因此,当甚至将鼠标悬停在菜单本身上时,菜单将关闭。您可以添加以下

 $(".sub-menu").hover(function() {
  $(this).addClass("toggle-on");
});

$(".sub-menu").mouseleave(function() {
  $(this).removeClass("toggle-on");
});

菜单不断跳跃并导致UI更改,因为它不是绝对的。您可以添加以下内容

.sub-menu {
  position: absolute;
  background-color: #000;
  margin-top: -26px;
  margin-left: -38px;
}

以下是更新的代码段。

/* NAVIGATION ON CLICK */

// Primary menu drop down (mobile)
$(".dropdown-toggle").click(function() {
  $(this).parent().find(".menu-test-container #primary-menu").toggleClass("toggle-on");
});

// Sub menu drop down
$(".dropdown-toggle").click(function() {

  $(this).parent().find(".sub-menu:first").toggleClass("toggle-on");
});


/* NAVIGATION ON HOVER */
// Sub menu drop down 
$(".dropdown-toggle").hover(function() {

  $(this).parent().find(".menu-test-container ul li .sub-menu:first").toggleClass("toggle-on");
});

$(".sub-menu").hover(function() {

  $(this).addClass("toggle-on");
});

$(".sub-menu").mouseleave(function() {

  $(this).removeClass("toggle-on");
});
/*
	# HEADER
 */

*,
html {
  margin: 0;
  font-size: 16px;
}

.site-header {
  background-color: black;
  padding: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.main-navigation {
  padding: 2rem;
  background-color: red;
}


/*.menu-toggle, .main-navigation ul ul, .main-navigation ul ul ul {
 	display: none;
 }
*/


/* Menu styles */

.main-navigation .sub-menu {
  display: none;
}

.sub-menu.toggle-on {
  display: block;
}

.main-navigation ul,
.main-navigation ul ul,
.main-navigation ul ul ul {
  list-style: none;
}

.main-navigation ul li a {
  color: #fff;
  text-decoration: none;
}


/* First Highrarchy */

.main-navigation ul {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.main-navigation ul li {
  margin-right: 2rem;
}

.sub-menu {
  position: absolute;
  background-color: #000;
  margin-top: -26px;
  margin-left: -38px;
}


/*.dropdown-toggle:hover {
	display: block;
}

*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header id="masthead" class="site-header">
  <div class="site-branding">
    <div class="site-branding-text">
      <p class="site-title"><a href="http://localhost/wordpress/" rel="home">LOGO</a></p>
    </div>
  </div>
  <!-- .site-branding -->

  <nav id="site-navigation" class="main-navigation">
    <button class="dropdown-toggle" aria-expanded="false">Primary Menu</button>
    <div class="menu-test-container">
      <ul id="primary-menu" class="menu" aria-expanded="true">
        <li id="menu-item-2035" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2035">
          <a href="http://localhost/wordpress/services/">Services</a>
          <button class="dropdown-toggle" aria-expanded="false">
							<span class="dropdown-symbol">+</span>
						</button>
          <ul class="sub-menu">
            <li id="menu-item-2076" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2076">
              <a href="http://localhost/wordpress/asd/">Commercial</a>
              <button class="dropdown-toggle toggled-on" aria-expanded="true">
									<span class="dropdown-symbol">-</span>
								</button>
              <ul class="sub-menu toggled-on">
                <li id="menu-item-2082" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2082">
                  <a href="http://localhost/wordpress/asd/">Rural</a>
                  <button class="dropdown-toggle toggled-on" aria-expanded="true">
											<span class="dropdown-symbol">-</span>
										</button>
                  <ul class="sub-menu toggled-on">
                    <li id="menu-item-2081" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2081">
                      <a href="http://localhost/wordpress/services/">Electrical</a>
                      <button class="dropdown-toggle" aria-expanded="false">
													<span class="dropdown-symbol">+</span>
												</button>
                      <ul class="sub-menu">
                        <li id="menu-item-2079" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2079">
                          <a href="http://localhost/wordpress/gallery/">Residential</a>
                          <button class="dropdown-toggle" aria-expanded="false">
															<span class="dropdown-symbol">+</span>
														</button>
                          <ul class="sub-menu">
                            <li id="menu-item-2083" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2083"><a href="http://localhost/wordpress/news-updates/">News Updates</a></li>
                          </ul>
                        </li>
                      </ul>
                    </li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
        </li>
        <li id="menu-item-2084" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2084">
          <a href="http://localhost/wordpress/asd/">Projects</a>
        </li>
        <li id="menu-item-2045" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2045">
          <a href="http://localhost/wordpress/blog/">Blog</a>
          <button class="dropdown-toggle" aria-expanded="false">
							<span class="dropdown-symbol">+</span>
						</button>
          <ul class="sub-menu">
            <li id="menu-item-2078" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-2078">
              <a href="http://localhost/wordpress/news-updates/">About</a>
              <button class="dropdown-toggle" aria-expanded="false">
									<span class="dropdown-symbol">+</span>
								</button>
              <ul class="sub-menu">
                <li id="menu-item-2099" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2099">
                  <a href="http://localhost/wordpress/news-updates/">News Updates</a>
                </li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </nav>
  <!-- #site-navigation -->
  <link rel="stylesheet" type="text/css" href="style.css">
</header>