gpt4 book ai didi

javascript - JS不会将导航显示属性从none改为flex

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:23 27 4
gpt4 key购买 nike

JS 不会将 nav-hidden 显示属性从 none 更改为 flex。我尝试了 !important,这在某些情况下似乎有效,但我想知道为什么这不起作用,以便我能更好地理解这一点。当我按下按钮时,会为该元素分配一个新类,但属性保持为无。

我想做的是:当屏幕小于 700px 时,只有当我点击整个屏幕上的按钮时才会出现 nav-hidden,当屏幕大于 700px 时,nav-line 与里面的标志一致.

function myFunction() {
var fullnavigation = document.getElementById("nav-hidden");
fullnavigation.classList.toggle("open-nav");
}
body{
margin: 0;
font-family: 'Roboto', sans-serif;
}
header{
background-color: #009933;
width:100%;
position: fixed;
top: 0;
left: 0;
right: 0;
display: flex;
justify-content: space-evenly;
align-items: center;
}
.logo img{
display: block;
height: 4em;
}
@media screen and (max-width:700px){
.nav-line{
display: none;
}
.open-nav{
display: flex;
}
.nav-hidden{
display: none;
position: fixed;
top: 0;
width: 100%;
height: 100%;
background-color: #009933;
flex-direction: column;
}
.nav-button{
display: block;
color: #ffffff;
background-color: transparent;
padding: 0px;
border: 0px;
cursor: pointer;
font-size: 4em;
z-index: 9999;
}
}
<!DOCTYPE html>
<html lang="sl">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="slike\favicon\favicon.png" type="image/gif" size="128x128">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<script src="js\index.js"></script>
<link rel="stylesheet" href="css\index.css">
</head>
<body>
<header>
<div class="logo"><img src="" alt="logo"></div>
<button id="nav-button" onclick="myFunction()">☰</button>
<nav id="nav-line" class="nav-line">
<div class="nav-text"><a href="#">DOMOV</a></div>
<div class="nav-text"><a href="#">O NAS</a></div>
<div class="nav-text"><a href="#">KONTAKT</a></div>
<div class="nav-text"><a href="#">POVPRAŠEVANJE</a></div>
</nav>
</header>
<nav id="nav-hidden" class="nav-hidden">
<div class="nav-text"><a href="#">DOMOV</a></div>
<div class="nav-text"><a href="#">O NAS</a></div>
<div class="nav-text"><a href="#">KONTAKT</a></div>
<div class="nav-text"><a href="#">POVPRAŠEVANJE</a></div>
</nav>
</body>
</html>

最佳答案

当 CSS 依次读取您的代码时,.open-nav 类已被下一个 .nav-hidden 拦截。问题不在您的 JS 代码中,如果您查看此 JSFiddle它有效并且 js 可以很好地切换你的类(class)。只需将 .open-nav 放在 .nav-hidden

之后
@media screen and (max-width:700px){
.nav-line{
display: none;
}
.nav-hidden{
display: none;
position: fixed;
top: 0;
width: 100%;
height: 100%;
background-color: #009933;
flex-direction: column;
}

.open-nav{
display: flex;
}
}

关于javascript - JS不会将导航显示属性从none改为flex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55207229/

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