单击其他导航栏

时间:2016-06-29 08:54:01

标签: javascript html css

我有一个导航栏,其中包含"合同"我想知道它是否可能,以及如何在标记此按钮时添加额外的导航栏以在其下展开(例如,在Apple Store网站上,当您单击某个产品时,这会添加另一个栏)

Apple Navigation Bar

如果需要,我可以提供我的整个CSS样式表!我认为这需要JavaScript,但我现在试图将它保持为纯CSS!

非常感谢所有帮助!

HTML代码:这是导航HTML

class="two"

创建此

My nav bar

CSS代码:我的整个样式表

<header>
    <div class="title">
        <img src="img/logo2.png"/>
    </div>
    <div class="navbar">
        <ul>
            <li style="float: left"><a href="#home">Home</a></li>
            <li><a href="#contact">Contracts</a></li>
            <li><a href="#about">About Us</a></li>
            <li><a href="#other">Other</a></li>
            <li> <a href="#release">Release Notes</a></li>
            <li> <a target="_blank" href="http://www.phpartnership.com">Pinnacle Health Partnership</a></li>
        </ul>
    </div>
</header>

这是我试图隐藏并在点击

上显示div的JavaScript
body {
    background-color: #fff;
    margin: 0;
    font-family: Arial;
    color: #333;
}

header {
    background-color: #333;
    position: fixed;
    top: 0;
    width: 100%;
}

.navbar {
    display: block;
    text-align: center;
}

.navbar ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
    overflow: hidden;
}

 .navbar li {
    display: inline-block;
    vertical-align: middle;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    box-shadow: 0 0 1px rgba(0, 0, 0, 0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -moz-osx-font-smoothing: grayscale;
    position: relative;
    overflow: hidden;
}
.navbar li:before {
    content: "";
    position: absolute;
    z-index: -1;
    left: 50%;
    right: 50%;
    bottom: 0;
    background: white;
    height: 4px;
    -webkit-transition-property: left, right;
    transition-property: left, right;
    -webkit-transition-duration: 0.3s;
    transition-duration: 0.3s;
    -webkit-transition-timing-function: ease-out;
    transition-timing-function: ease-out;
}
.navbar li:hover:before, navbar li:focus:before, .navbar li:active:before {
    left: 0;
    right: 0;
}

.navbar li a {
    padding: 25px;
    display: block;
    height: 100%;
    color: white;
    text-decoration: none;
}

.title {
    height: 80px;
    padding: 2px;
    background-color: #fff;
}

.container {
    margin-top: 150px;
    padding-top: 50px;
}

.home {
    margin-top: 10px;
    text-align: center;
    padding: 40px !important;
}
.wrap {
    width: 100%;
    margin: 0 auto;
}
.left_col {
    float: left;
    width: 50%;
}
.right_col {
    float: right;
   width: 50%;
}
.right_col img {
    width: 80%;
    margin-top: 50px;
    border: 2px solid black;
    border-radius: 5px;
}
.left_col img {
    width: 80%;
    margin-top: 50px;
    border: 2px solid black;
    border-radius: 5px;
}

1 个答案:

答案 0 :(得分:1)

猜你想要像这样的人: jsfiddle

首先将jQuery添加到您的本地环境 使用此<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

将其添加到html的head部分。有关详情,请查看how to install jQuery

.navbar

中添加了html
<ul class="aditional">
     <li>test1</li>
      <li>test2</li>
      <li>test3</li>
      <li>test4</li>
</ul>

添加了css:

.aditional {
position:absolute;
top:100%;
width:100%;
background:#000;
display:none;
}
.aditional li {
color:#fff;
}

添加了js:

$('.navbar ul li:nth-child(2) a').click(function() {
          $(".aditional").slideToggle()
});

或者如果您想要更具响应性的解决方案

检查一下:jsfiddle with target

data-target上使用li a,就像这样

<li><a href="#contact" data-target="contracts">Contracts</a></li>
<li><a href="#about" data-target="aboutus">About Us</a></li>

添加了html:

 <ul class="aditional contracts">
     <li>test1</li>
     <li>test2</li>
     <li>test3</li>
     <li>test4</li>
 </ul>
 <ul class="aditional aboutus">
      <li>about</li>
      <li>about</li>
      <li>about</li>
      <li>about</li>
 </ul>

jq补充道:

$('.navbar ul li a').click(function() {
  $(".aditional").slideUp()
  var target = '.' + $(this).data('target');
  $(target).slideDown();
})

您可以定位href的{​​{1}}。只需添加此

li a

和js:

<li><a href="#contract">Contracts</a></li>
<li><a href="#about">About Us</a></li>
------
<ul class="aditional" id ="contract">
....
<ul class="aditional" id ="about">
....

请参阅此处jsfiddle

其中一种解决方案应该适合您。让我知道