【问题标题】:cannot create navigation menu with HTML/CSS and jQuery无法使用 HTML/CSS 和 jQuery 创建导航菜单
【发布时间】:2015-04-17 13:56:04
【问题描述】:

我刚刚在 codecademy 上完成了 HTML/CSS、JavaScript、jQuery 和 php 的基础知识。 我正在尝试在 codecademy 代码位中使用 html/css 和 jquery 创建一个网站。现在我的导航菜单表现得很奇怪。 主导航菜单(主要)似乎没问题。但是,当将鼠标悬停在具有子菜单的菜单之一上时,主菜单会与下拉菜单一起下降。 我一整天都在尝试解决它,但无济于事:( 我试过 display: inline, display: inline-block, display: block 但它们没有帮助。 我也尝试过将 position: 设置为不同的设置,但它们也无济于事。 我的 html 对我来说似乎没问题,但也许有一些错误。我怀疑我的css,但是在尝试了一整天之后,我已经筋疲力尽了:(

我的 html 脚本:

<!DOCTYPE html>
<html>
<head>
<title>A first complete project</title>
<link type = "text/css" rel = "stylesheet" href = "style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type = "text/javascript" src = "script.js"></script>
</head>

<body>
<div class = "header">  <!--Header Area -->
    <ul class = "mainMenu">  <!-- The main nav menu -->
        <li><a href = "#">Home</a></li>
        <li>About Us</li>
        <li>Contact Us</li>
        <li id = "services">Services 
        <!--Creating sub menu-->
            <ul class = "servicesSubMenu">
                <li>Basic Web Design</li>
                <li>Pro Web Design</li>
                <li>Advanced Web Design</li>
                <li id = "wordpressWebDesign">Wordpress Web 
                Design
                <!--Creating Sub-sub menu-->
                    <ul class = "wordpressSubMenu">
                        <li>Wordpress Installation</li>
                        <li>Wordpress customization</li>
                    </ul> 
                </li>     
            </ul>
        </li>    
    </ul>

</div>

<!--creating div class wrapper for sliderArea-->
<div class = "wrapper">
    <div class = "slideArea"> <!--Creating the slider-->

    </div>
</div>

<!--Creating the main footer-->
<div id = "mainFooter">
    <p>Copyright &copy; 2014 <a href = "https://www.hostbarrack.com">Hostbarrack</a></p>
</div>

</body>

</html>

我的 CSS:

/* ==== Settings for the main body
================================================= */
body {
    background-color: #454545;
}

/*======  Settings for the main nav menu
================================================== */
.mainMenu {
  border-radius: 10px; 
  background-color: #555555;
  font-family: seriff;
  font-weight: bold;
  color: #389803;
  font-size: 17px;
  height: 50px;
  line-height: 30px;
  padding-right: 80px;
}

.mainMenu li {
    position: relative;
    display: inline-block;
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 10px;
    list-style: none;
    padding: 0 20px;
}

.mainMenu a {
    text-decoration: none;
    display: block;
    color: #389803;
    height: 40px;
}

/* === Settings for the sub menu of the 'Services' Button
===================================================== */
.servicesSubMenu {
  font-family: seriff;
  font-weight: bold;
  color: #389803;
  font-size: 17px;
  margin-top: 10px;
}

.servicesSubmenu li {
    border: 1px solid red;
    border-radius: 8px;
    list-style: none;
    background-color: #454545;
    height: auto;
    width: 200px;
    padding: 10px;
    margin: 1px;
    display: block;

}

/* === =Settings for the sub menu of 'Wordpress Web design' which is a sub menu of 'Services'
=========================================================*/
.wordpressSubMenu li {
    background-color: #606060;
}

/* ======== Settings for the slider area of home-page.
==================================================== */
.slideArea {
    border-radius: 15px;
    background-color: #909090;
    height: 500px;
    width: auto;
    margin: 10px, 10px, 10px, 10px;
}

/* === Settings for the main footer area 
==================================================== */
#mainFooter {
    text-align: center;
    font-weight: bold;
}

#mainFooter a {
    color: #FEFE79;
/*does not work. check it again and remove this comment after finding the solution*/    
}

我的 jQuery 代码:

var main = function() {
    $(".servicesSubMenu").hide(); /* Hide sub menu when document
                                    loads */

    /* When hovering over 'Services' button in main nav menu */
    $("#services").hover(function() {
        $(".servicesSubMenu").slideToggle(80);
        $(".wordpressSubMenu").hide(); /* Hide sub-sub menu */
    });

    /* When hovering over sub-sub menu 'Wordpress Web Design'
      inside 'Services button. */
    $("#wordpressWebDesign").hover(function() {
      $(".wordpressSubMenu").slideToggle(80);
    });
}

$(document).ready(main);

您还可以在http://www.codecademy.com/sunoy14/codebits/r316c 上查看我的代码和测试页面 请在全屏和小屏幕中查看代码位。显示不同的有问题的布局。 非常感谢您的帮助。

谢谢。

【问题讨论】:

  • 请始终在此站点上添加您的代码,并在此处添加您收到的错误消息。欢迎提供指向您的问题示例的外部链接,但不可靠,因为它们可能会在一段时间后变得不可用。
  • 我现在已经添加了代码。我担心代码会使我的帖子太长,所以我在发布线程时没有添加它。
  • 嗨。在看到 Persijin 的帖子后,我刚刚尝试添加代码 sn-p。但我无法将其添加到我的帖子中。当我单击插入发布时,没有任何反应。有人可以帮我弄这个吗?谢谢。

标签: jquery css


【解决方案1】:

我在网站上查看了您的代码。 您尚未设置位置值。 位置会更改它要显示的位置,默认情况下,它设置为相对。通过将其更改为绝对,该元素不会受到周围元素的影响。

在 CSS 文件中试试这个代码。

.servicesSubMenu {
  font-family: seriff;
  font-weight: bold;
  color: #389803;
  font-size: 17px;
  margin-top: 10px;
  position:absolute;
}

【讨论】:

  • 成功了!!我曾尝试在 .mainMenu、.mainMenu li 和 .servicesMenu li 中将位置设置为绝对。但不知何故,我从未尝试将其设置在 .servicesMenu 中。我在想它必须在子菜单 div :D 中设置。非常感谢你让我免于头痛:)
  • 又出现了一个问题 :( 。当鼠标悬停在服务菜单上时,下拉菜单现在看起来很好。但是它会摇晃或颤抖一段时间并且不稳定。片刻之后,它会变得稳定.可能是什么问题?我是否可以将子菜单直接对齐到服务菜单下方?Persijin提供的代码输出直接在父菜单下方对齐的子菜单。但他只使用CSS。我想知道同样可以应用于我的。谢谢:)
【解决方案2】:

这是您要求的导航栏的一些代码示例。
我不知道这是否是您想要的方式,因为我不使用任何 Javascript。

.navigation {
  background-color: gray;
  list-style-type: none;
  height: 50px;
}
.menu-item {
  display: inline-block;
  width: 80px;
  text-align: center;
  height: 100%;
}
.menu-item {
  padding-top: 15px;
}
.menu-item:hover {
  background-color: #555;
}
.menu-item .sub-item {
  display: none;
}
.menu-item:hover .sub-item {
  position: absolute;
  width: 80px;
  display: block;
    border-bottom: 1px solid black;
  background-color: red;
}
.sub-item: hover {
  display: block;
}
.menu-item .sub-item a {
  display: block;
  height: 20px;
  border-top: 1px solid black;
}
<nav>
  <div class="navigation">
    <div class="menu-item">
      <div class="name">Home</div>
      <div class="sub-item">
        <a>link</a>
        <a>link</a>
      </div>
    </div>
    <div class="menu-item">
      <div class="name">Contact</div>
      <div class="sub-item">
        <a>link</a>
        <a>link</a>
      </div>
    </div>
  </div>
</nav>

【讨论】:

  • 谢谢。我也打算寻找一些css方法来做这件事。这会很有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多