gpt4 book ai didi

javascript - window.innerWidth 如果不触发

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

我试图让下拉菜单在屏幕尺寸小于 500 像素时显示。稍后,我将删除导航栏,以便此菜单在小型设备上取而代之。

我觉得代码不错,警报确实触发了,但菜单没有显示。

TLDR:需要在屏幕尺寸小于 500px 时显示菜单

JSFiddle:https://jsfiddle.net/mcgettrm/y4edsu73/

代码:

var dropDownFunction = function(){
var menuContent = document.getElementById("dropDownMenuClass");
menuContent.classList.toggle("menuShow");
}


var windowWidth = window.innerWidth;

if(windowWidth < 500){
alert("window is too small");

var dropDownMenu = getElementById("dropDownMenu");
dropDownMenu.classList.toggle("mainMenuShow");



}
#dropDownMenu {

}
.dropDownMenuClass{
display:none;
}
.mainMenuShow{
display: block;
}

#dropDownMenuContent {

}
.dropDownMenuContentClass {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
width:100%;
text-align: center;
}
#dropDownMenuContent a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;

}
#dropDownMenuContent a:hover {
background-color: #f1f1f1;

}
#dropDownButton {
width:100%;
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

.menuShow {
display: block;
}
<div id="dropDownMenu" class="dropDownMenuClass">
<button onclick="dropDownFunction()" id="dropDownButton">MENU</button>
<div id="dropDownMenuContent" class="dropDownMenuContentClass">
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="itprojects.html">IT Projects</a>
<a href="languageprojects.html">Language Projects</a>
<a href="contact.html">Contact</a>
<a href="essays.html">Essays</a>
</div>
</div>

最佳答案

getElementByIDdocument

的一个方法
var dropDownMenu = document.getElementById("dropDownMenu");

关于javascript - window.innerWidth 如果不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104371/

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