gpt4 book ai didi

javascript - 侧边栏中的下拉菜单

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

我正在尝试借助 w3schools.com 示例在我的侧边栏中创建一个下拉菜单。出于某种原因,我的代码在 codepen.io 和 stackoverflow 上运行良好,但是一旦我在 Chrome 浏览器上运行它,我的下拉菜单就不会弹出。有什么我可以做的吗?我已经清除了我的浏览历史记录和缓存,但没有什么可以修复它。

有人能帮帮我吗?

(警告,编码新手)

Code example on CodePen

/*Toggle sidebar*/
function openNav() {
document.getElementById("mySidebar").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
document.getElementById("mySidebar").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
}



/*Dropdown in sidebar*/
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;

for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function () {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
});
}
body {
font-family: Arial, Helvetica, sans-serif;
}

/*Sidebar with collapseble dropdown from w3school.com */
.sidebar{
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}

.sidebar a, .dropdown-btn{
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
border: none;
background: none;
text-align: left;
cursor: pointer;
outline: none;
width:100%;
transition: 0.3s;
}

.sidebar a:hover, .dropdown-btn:hover{
color: #f1f1f1;
}

/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
.dropdown-container {
display: none;
background-color: #262626;
padding-left: 8px;
}

/* Optional: Style the caret down icon */
.fa-caret-square-down {
float: right;
padding-right: 8px;
}

/* Add an active class to the active dropdown button */
.active {
background-color: green;
color: white;
}

.sidebar .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}

.openbtn {
font-size: 20px;
cursor: pointer;
background-color: #111;
color: white;
padding: 10px 15px;
border: none;
}

.openbtn:hover {
background-color: #444;
}

#main {
transition: margin-left .5s;
padding: 16px;
}

/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidebar {padding-top: 15px;}
.sidebar a {font-size: 18px;}
}
<div id="mySidebar" class="sidebar">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<button class="dropdown-btn active">Dropdown
<i class="far fa-caret-square-down"></i>
</button>
<div class="dropdown-container">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>

<div id="main">
<button class="openbtn" onclick="openNav()">☰</button>
<h2>Sidebar Dropdown</h2>
<p>Click on the dropdown button to open the dropdown menu inside the side navigation.</p>
<p>This sidebar is of full height (100%) and always shown.</p>
<p>Some random text..</p>
</div>

最佳答案

如果您的代码在 Codepen 上运行,这可能是因为您的 javascript 在页面加载之前正在执行(codepen 自己处理)

尝试在其中运行所有 JS:

document.addEventListener('DOMContentLoaded', function() {
// All your JS here
}, false);

关于javascript - 侧边栏中的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54383847/

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