使用onclick事件进行响应的下拉菜单

时间:2018-10-29 15:16:55

标签: javascript html css

我正在尝试使用CSS响应式布局,并且尝试使用onclick事件创建向下切换菜单。 我创建了这个菜单,您可以在下面找到它。 这是我的第一个带有响应菜单的项目,我当时正在考虑使用一些基本的Javascript代码来使其向下滚动和滚动。不幸的是,由于某些原因,当我再次尝试使其消失时,该按钮不起作用。

我遇到的另一个问题是,当导航栏处于打开状态时,我试图将窗口的大小从992px调整为完整大小,因为高度保持为450px,并且显示变回flex。我希望我能清楚地解释自己。

const { response } = require('express');

...

const spy = sinon.spy(response, 'render');
let toggleNavStatus= false;

function togglemenu(){

let getNavBar = document.querySelector(".navbar");
let getNavBarUl = document.querySelector(".navbar ul");

if(toggleNavStatus === false){
getNavBar.style.height ="450px";
    
getNavBarUl.style.height="400px";
let toggleNavStatus= true;
}

if(toggleNavStatus === true){
getNavBar.style.height ="0px";
    
getNavBarUl.style.height="0px";
let toggleNavStatus= false;
}
}
*{
box-sizing: border-box;
padding:0px;
margin:0px;
}
body{
background-color:#ffffff;
}



/*---------------------------------------------NAVBAR-----------------------------------------*/

.navbar{

background-color:#1e3581;
width:100%;
max-width: auto;
box-shadow: 5px ;
display:flex;
justify-content: space-between;
position:relative;


}
.togglemenu i {
color:white;
font-size:27px;
margin:10px 30px; ;
border: 2px solid white;
padding:5px 8px;
border-radius: 5px;
position:absolute;
z-index: 1;
top:0px;
left:0px;
text-align: center;
display:none;
}
.navbar ul{
display:flex;
list-style: none;
height:30px;
text-align: center;
margin:14px 0 14px 0;
opacity:1;
}
.navbar ul li{

border-right:1px white solid;
padding: 3px 20px 3px;
}

.navbar ul li:hover a {
color:yellow;
}
.navbar ul li:last-child{
border-right: none;
}

.button{
width: 90px;
border-radius: 5px;
height:35px;
margin-top:11px;
font-size:18px;
background-color:#1e3581;
border: 1px white solid;
color:white;
margin-right:20px;
}

button:hover{
color:yellow;
border: 1px yellow solid;

}

.navbar ul li a i{
padding-left:10px;
transition:0.5s;

}
.navbar ul li a .icons .fa{

width:100%;
height:100%;
font-size: 25px;

    
}
.navbar ul li a .icons{
overflow:hidden;
height:25px;
width:50px;

}
.navbar ul li a:hover i{
transform: translateY(-100%);
color:yellow;

}
/*------------------------navbar text-------------------------*/
.navbar ul li a{
color:white;
text-decoration: none;
font-family: helvetica;
font-weight: 500;
font-size:15px;
display:flex;
align-items:center;
}


.navbar ul li a .Name{
position:relative;
height:100%;
overflow: hidden;
white-space: nowrap;


display:block;
}

.navbar ul li a span:before{
content: attr(data-text);
position:absolute;
left:0;
top:-100%;
width:100%;
height: 100%;
}
.navbar ul li a span{
display:block;
transition:0.5s;



}
.navbar ul li a:hover span{
transform: translateY(15px);
}

/*---------------MEDIA Q-------------------*/   
@media(max-width: 992px){
.foto{
flex:1 0    49%;
}
#map{
display:none; 
}  
.navbar{    
display:block;
height:60px;
overflow: hidden;
transition:all 0.3s ease-in-out;

}
.navbar ul{
display:block;
height:100%;
padding:10px;
margin:0px;
padding-top:60px;
width:100%
opacity:0;
}
.navbar ul li{
border-bottom:1px white solid;
padding: 20px 0px 3px;
border-right:none;

overflow: hidden;

}
.navbar button{    
margin:10px 30px;;

}
.navbar ul li a{
color:white;
text-decoration: none;
font-family: helvetica;
font-weight: 500;
}
.togglemenu i {
color:white;
font-size:27px;
margin:10px 30px; ;
border: 2px solid white;
padding:5px 8px;
border-radius: 5px;
position:absolute;
z-index: 1;
top:0px;
left:0px;
text-align: center;
display:inline;

 
}
}

2 个答案:

答案 0 :(得分:1)

您不需要任何Javascript。 最佳实践是使用CSS类,而不是直接弄乱height值。并且已经有一种切换类的方法。 注意,我在UL中删除了display:flex,因为它只会弄乱显示屏。

function togglemenu() {

  let NavBar = document.querySelector(".navbar");
  
  NavBar.classList.toggle("open");
}
* {
  box-sizing: border-box;
  padding: 0px;
  margin: 0px;
}

body {
  background-color: #ffffff;
}


/*---------------------------------------------NAVBAR-----------------------------------------*/

.navbar {
  background-color: #1e3581;
  width: 100%;
  max-width: auto;
  box-shadow: 5px;
  display: flex;
  justify-content: space-between;
  position: relative;
}

.togglemenu i {
  color: white;
  font-size: 27px;
  margin: 10px 30px;
  ;
  border: 2px solid white;
  padding: 5px 8px;
  border-radius: 5px;
  position: absolute;
  z-index: 1;
  top: 0px;
  left: 0px;
  text-align: center;
  display: none;
}

.navbar ul {
  list-style: none;
  height: 30px;
  text-align: center;
  margin: 44px 0 14px 0;
  opacity: 1;
}

div.navbar.open {
  height: 450px;
}

div.navbar.open ul {
  height: 400px;
}

.navbar ul li {
  border-right: 1px white solid;
  padding: 3px 20px 3px;
}

.navbar ul li:hover a {
  color: yellow;
}

.navbar ul li:last-child {
  border-right: none;
}

.button {
  width: 90px;
  border-radius: 5px;
  height: 35px;
  margin-top: 11px;
  font-size: 18px;
  background-color: #1e3581;
  border: 1px white solid;
  color: white;
  margin-right: 20px;
}

button:hover {
  color: yellow;
  border: 1px yellow solid;
}

.navbar ul li a i {
  padding-left: 10px;
  transition: 0.5s;
}

.navbar ul li a .icons .fa {
  width: 100%;
  height: 100%;
  font-size: 25px;
}

.navbar ul li a .icons {
  overflow: hidden;
  height: 25px;
  width: 50px;
}

.navbar ul li a:hover i {
  transform: translateY(-100%);
  color: yellow;
}


/*------------------------navbar text-------------------------*/

.navbar ul li a {
  color: white;
  text-decoration: none;
  font-family: helvetica;
  font-weight: 500;
  font-size: 15px;
  display: flex;
  align-items: center;
}

.navbar ul li a .Name {
  position: relative;
  height: 100%;
  overflow: hidden;
  white-space: nowrap;
  display: block;
}

.navbar ul li a span:before {
  content: attr(data-text);
  position: absolute;
  left: 0;
  top: -100%;
  width: 100%;
  height: 100%;
}

.navbar ul li a span {
  display: block;
  transition: 0.5s;
}

.navbar ul li a:hover span {
  transform: translateY(15px);
}


/*---------------MEDIA Q-------------------*/

@media(max-width: 992px) {
  .foto {
    flex: 1 0 49%;
  }
  #map {
    display: none;
  }
  .navbar {
    display: block;
    height: 60px;
    overflow: hidden;
    transition: all 0.3s ease-in-out;
  }
  div.navbar.open {
    height: 450px;
  }
  div.navbar.navbar-open ul {
    height: 400px;
  }
  .navbar ul li {
    border-bottom: 1px white solid;
    padding: 20px 0px 3px;
    border-right: none;
    overflow: hidden;
  }
  .navbar button {
    margin: 10px 30px;
    ;
  }
  .navbar ul li a {
    color: white;
    text-decoration: none;
    font-family: helvetica;
    font-weight: 500;
  }
  .togglemenu i {
    color: white;
    font-size: 27px;
    margin: 10px 30px;
    ;
    border: 2px solid white;
    padding: 5px 8px;
    border-radius: 5px;
    position: absolute;
    z-index: 1;
    top: 0px;
    left: 0px;
    text-align: center;
    display: inline;
  }

}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="stylesheet.css">
  <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

</head>

<body>
  <div class="togglemenu" onclick="togglemenu()"><i class="fa fa-bars" aria-hidden="true"></i></div>
  <div class="navbar">

    <ul>
      <li>
        <a href="#">
          <div class="icons"><i class="fa fa-home" aria-hidden="true"></i><i class="fa fa-home" aria-hidden="true"></i></div>
          <div class="Name"><span data-text="Home">Home</span></div>
        </a>
      </li>
      <li>
        <a href="#">
          <div class="icons"><i class="fa fa-h-square" aria-hidden="true"></i><i class="fa fa-h-square" aria-hidden="true"></i></div>
          <div class="Name"><span data-text="Hotel">Hotel</span></div>
        </a>
      </li>
      <li>
        <a href="#">
          <div class="icons"><i class="fa fa-plane" aria-hidden="true"></i><i class="fa fa-plane" aria-hidden="true"></i></div>
          <div class="Name"><span data-text="Flight">Flight</span></div>
        </a>
      </li>
      <li>
        <a href="#">
          <div class="icons"><i class="fa fa-bed" aria-hidden="true"></i><i class="fa fa-bed" aria-hidden="true"></i></div>
          <div class="Name"><span data-text="Hotel and Flight">Hotel and Flight</span></div>
        </a>
      </li>
      <li>
        <a href="#">
          <div class="icons"><i class="fa fa-sun-o" aria-hidden="true"></i><i class="fa fa-sun-o" aria-hidden="true"></i></div>
          <div class="Name"><span data-text="Special Offer">Special Offer</span></div>
        </a>
      </li>
      <li>
        <a href="#">
          <div class="icons"><i class="fa fa-globe" aria-hidden="true"></i><i class="fa fa-globe" aria-hidden="true"></i></div>
          <div class="Name"><span data-text="Thinks to do">Things to do</span></div>
        </a>
      </li>
      <li>
        <a href="#">
          <div class="icons"><i class="fa fa-arrow-down" aria-hidden="true"></i><i class="fa fa-arrow-down" aria-hidden="true"></i></div>
          <div class="Name"><span data-text="More">More</span></div>
        </a>
      </li>

    </ul>

    <button class="button">Login</button>
  </div>

  <script src="javascript.js"></script>
</body>

</html>

答案 1 :(得分:0)

getNavBar.style.height ="60px";的高度应为60px。 对于切换,您只需执行toggleNavStatus = !toggleNavStatus;

是您所期望的行为吗?

let toggleNavStatus= false;

function togglemenu(){

let getNavBar = document.querySelector(".navbar");
let getNavBarUl = document.querySelector(".navbar ul");

  if(toggleNavStatus === false){
    getNavBar.style.height ="450px";

    getNavBarUl.style.height="400px";
  }

  if(toggleNavStatus === true){
    getNavBar.style.height ="60px";

    getNavBarUl.style.height="60px";
  }
  toggleNavStatus = !toggleNavStatus;
}
*{
box-sizing: border-box;
padding:0px;
margin:0px;
}
body{
background-color:#ffffff;
}



/*---------------------------------------------NAVBAR-----------------------------------------*/

.navbar{

background-color:#1e3581;
width:100%;
max-width: auto;
box-shadow: 5px ;
display:flex;
justify-content: space-between;
position:relative;


}
.togglemenu i {
color:white;
font-size:27px;
margin:10px 30px; ;
border: 2px solid white;
padding:5px 8px;
border-radius: 5px;
position:absolute;
z-index: 1;
top:0px;
left:0px;
text-align: center;
display:none;
}
.navbar ul{
display:flex;
list-style: none;
height:30px;
text-align: center;
margin:14px 0 14px 0;
opacity:1;
}
.navbar ul li{

border-right:1px white solid;
padding: 3px 20px 3px;
}

.navbar ul li:hover a {
color:yellow;
}
.navbar ul li:last-child{
border-right: none;
}

.button{
width: 90px;
border-radius: 5px;
height:35px;
margin-top:11px;
font-size:18px;
background-color:#1e3581;
border: 1px white solid;
color:white;
margin-right:20px;
}

button:hover{
color:yellow;
border: 1px yellow solid;

}

.navbar ul li a i{
padding-left:10px;
transition:0.5s;

}
.navbar ul li a .icons .fa{

width:100%;
height:100%;
font-size: 25px;

    
}
.navbar ul li a .icons{
overflow:hidden;
height:25px;
width:50px;

}
.navbar ul li a:hover i{
transform: translateY(-100%);
color:yellow;

}
/*------------------------navbar text-------------------------*/
.navbar ul li a{
color:white;
text-decoration: none;
font-family: helvetica;
font-weight: 500;
font-size:15px;
display:flex;
align-items:center;
}


.navbar ul li a .Name{
position:relative;
height:100%;
overflow: hidden;
white-space: nowrap;


display:block;
}

.navbar ul li a span:before{
content: attr(data-text);
position:absolute;
left:0;
top:-100%;
width:100%;
height: 100%;
}
.navbar ul li a span{
display:block;
transition:0.5s;



}
.navbar ul li a:hover span{
transform: translateY(15px);
}

/*---------------MEDIA Q-------------------*/   
@media(max-width: 992px){
.foto{
flex:1 0    49%;
}
#map{
display:none; 
}  
.navbar{    
display:block;
height:60px;
overflow: hidden;
transition:all 0.3s ease-in-out;

}
.navbar ul{
display:block;
height:100%;
padding:10px;
margin:0px;
padding-top:60px;
width:100%
opacity:0;
}
.navbar ul li{
border-bottom:1px white solid;
padding: 20px 0px 3px;
border-right:none;

overflow: hidden;

}
.navbar button{    
margin:10px 30px;;

}
.navbar ul li a{
color:white;
text-decoration: none;
font-family: helvetica;
font-weight: 500;
}
.togglemenu i {
color:white;
font-size:27px;
margin:10px 30px; ;
border: 2px solid white;
padding:5px 8px;
border-radius: 5px;
position:absolute;
z-index: 1;
top:0px;
left:0px;
text-align: center;
display:inline;

 
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="stylesheet.css">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

</head>
    
<body>
    <div class="togglemenu" onclick="togglemenu()"><i class="fa fa-bars" aria-hidden="true"></i></div>
<div class="navbar">

<ul>
    <li><a href="#"><div class="icons"><i class="fa fa-home" aria-hidden="true"></i><i class="fa fa-home" aria-hidden="true"></i></div><div class="Name"><span data-text="Home">Home</span></div></a></li>
<li><a href="#"><div class="icons"><i class="fa fa-h-square" aria-hidden="true"></i><i class="fa fa-h-square" aria-hidden="true"></i></div><div class="Name"><span data-text="Hotel">Hotel</span></div></a></li>
<li><a href="#"><div class="icons"><i class="fa fa-plane" aria-hidden="true"></i><i class="fa fa-plane" aria-hidden="true"></i></div><div class="Name"><span data-text="Flight">Flight</span></div></a></li>
<li><a href="#"><div class="icons"><i class="fa fa-bed" aria-hidden="true"></i><i class="fa fa-bed" aria-hidden="true"></i></div><div class="Name"><span data-text="Hotel and Flight">Hotel and Flight</span></div></a></li>
    <li><a href="#"><div class="icons"><i class="fa fa-sun-o" aria-hidden="true"></i><i class="fa fa-sun-o" aria-hidden="true"></i></div>
<div class="Name"><span data-text="Special Offer">Special Offer</span></div></a></li>
<li><a href="#"><div class="icons"><i class="fa fa-globe" aria-hidden="true"></i><i class="fa fa-globe" aria-hidden="true"></i></div>
<div class="Name"><span data-text="Thinks to do">Things to do</span></div></a></li>
    <li><a href="#"><div class="icons"><i class="fa fa-arrow-down" aria-hidden="true"></i><i class="fa fa-arrow-down" aria-hidden="true"></i></div><div class="Name"><span data-text="More">More</span></div></a></li>

</ul>

<button class="button">Login</button>
</div>

<script src="javascript.js"></script>
</body>
</html>