gpt4 book ai didi

javascript - 使用 onclick 事件的响应式下拉菜单

转载 作者:行者123 更新时间:2023-11-28 14:40:18 25 4
gpt4 key购买 nike

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

我遇到的另一个问题是当我尝试将窗口从 992px 调整为全尺寸时打开导航栏,因为高度保持 450px 并且显示变回 flex。我希望我解释清楚了。

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;


}
}
<!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>

最佳答案

您不需要任何 Javascript。最佳做法是使用 CSS 类而不是直接修改高度值。并且已经有一种切换类的方法。请注意,我在 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>

关于javascript - 使用 onclick 事件的响应式下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53048577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com