gpt4 book ai didi

javascript - 在更大的屏幕尺寸下消失的导航

转载 作者:行者123 更新时间:2023-11-28 02:35:11 24 4
gpt4 key购买 nike

我有一些导航 html,我正试图使其响应。我已经成功地让汉堡包菜单出现在小于 1050 像素的屏幕尺寸上,并且它可以打开和关闭菜单。问题是,如果您在放大屏幕之前不关闭汉堡包菜单,导航菜单将停留在显示:无模式并且不显示。所以看起来导航突然消失了。我认为普通的 css 会在更大的屏幕尺寸下使用,但可能是因为该属性是在脚本中设置的,所以它会持续存在。谁能帮我解决这个问题。我对响应式设计还很陌生。

html:

<nav>
<div class="navigation">
<a href="/index.html"> <img class="nav-img" src="/incl/image.png"> </a>

<button id="mobile" type="button" onclick="toggleFunction()">
<img src="/incl/menu.png" width="35px" height="35px" alt="menu">
</button>

<ul id="nav-list">
<li> <a href="/proposer/">Proposer</a> </li>
<li> <a href="/cda/">Archive</a> </li>
<li> <a href="/ciao/">Data<br/>Analysis</a> </li>
<li> <a href="/cal/">Instruments and<br/>Calibration</a> </li>
<li> <a href="/public/">For The<br/>Public</a> </li>
</ul>
</div></nav>

<script>
function toggleFunction() {
var x = document.getElementById('nav-list');

if (x.style.display === 'block') {
x.style.display = 'none';
} else {
x.style.display = 'block';
}
}
</script>

CSS:

#nav-list {
list-style-type: none;
padding-top: 30px;
padding-right: 20px;
margin: 0px;
overflow: hidden;
display: inline-table;
float: right;
}

#nav-list li {
display: table-cell;
vertical-align: middle;
padding: 0px 15px;
margin: 0px;
text-align: center;
line-height: 110%;
font-size: 1.3em;
font-family: Helvetica, Arial, sans-serif;
font-weight: 400;
}

#mobile {
display: none;
}

@media only screen and (max-width: 1050px) {
.nav-img {
margin: 0px 5px 5px 5px;
}

#mobile {
display: inline;
float: right;
background-color: transparent;
border: 1px solid #333;
border-radius: 5px;
margin: 10px 10px 10px 0px;
padding: 5px;
}

#nav-list {
list-style-type: none;
margin: 0px;
padding: 0px;
overflow: hidden;
display: none;
float: none
}

#nav-list li {
display: block;
text-align: center;
margin: 10px 0px 10px 0px;
line-height: 110%;
font-size: 1em;
}

最佳答案

解决此问题的最简单方法是 toggling a class#nav-list 元素上。

有了这个变化,这个类只在移动媒体查询的CSS中有意义,它会将#nav-list更改为display: block。无论是否切换,该类都不会影响大于 1050 像素的屏幕。

function toggleFunction() {
var x = document.getElementById('nav-list');
x.classList.toggle('show');
}
#nav-list {
list-style-type: none;
padding-top: 30px;
padding-right: 20px;
margin: 0px;
overflow: hidden;
display: inline-table;
float: right;
}

#nav-list li {
display: table-cell;
vertical-align: middle;
padding: 0px 15px;
margin: 0px;
text-align: center;
line-height: 110%;
font-size: 1.3em;
font-family: Helvetica, Arial, sans-serif;
font-weight: 400;
}

#mobile {
display: none;
}

@media only screen and (max-width: 1050px) {
.nav-img {
margin: 0px 5px 5px 5px;
}

#mobile {
display: inline;
float: right;
background-color: transparent;
border: 1px solid #333;
border-radius: 5px;
margin: 10px 10px 10px 0px;
padding: 5px;
}

#nav-list {
list-style-type: none;
margin: 0px;
padding: 0px;
overflow: hidden;
display: none;
float: none
}

/* show navigation based on class */
#nav-list.show {
display: block;
}

#nav-list li {
display: block;
text-align: center;
margin: 10px 0px 10px 0px;
line-height: 110%;
font-size: 1em;
}
}
<nav>
<div class="navigation">
<a href="/index.html"> <img class="nav-img" src="/incl/image.png"> </a>

<button id="mobile" type="button" onclick="toggleFunction()">
<img src="/incl/menu.png" width="35px" height="35px" alt="menu">
</button>

<ul id="nav-list">
<li> <a href="/proposer/">Proposer</a> </li>
<li> <a href="/cda/">Archive</a> </li>
<li> <a href="/ciao/">Data<br/>Analysis</a> </li>
<li> <a href="/cal/">Instruments and<br/>Calibration</a> </li>
<li> <a href="/public/">For The<br/>Public</a> </li>
</ul>
</div>
</nav>

关于javascript - 在更大的屏幕尺寸下消失的导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892540/

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