gpt4 book ai didi

javascript - 边栏打开/关闭

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

我正在尝试制作一个网站,我想要一个按钮来关闭和打开侧边栏,而不是我目前拥有的两个箭头。希望有人可以编辑我的代码片段并帮助我解决问题。请注意,我是 JS 的初学者,我真的不知道它是如何工作的。提前致谢!

这是我得到的箭头:

function openNav() {
document.getElementById("sidenav").style.width = "250px";
}

function closeNav() {
document.getElementById("sidenav").style.width = "0";
}
body {
background-color: #fcfcfc;
font-family: Roboto, Arial, sans-serif;
}

.header {
width: 100%;
height: 70px;
top: 0;
left: 0;
position: fixed;
background-color: #fff;
border-bottom: 1px solid #eee;
}

.toggle {
width: 250px;
height: 70px;
border-right: 1px solid #eee;
position: fixed;
top: 0;
left: 0;
}

.sidenav {
height: 100%;
width: 250px;
top: 0;
left: 0;
position: fixed;
z-index: 1;
overflow-x: hidden;
transition: 0.3s;
margin-top: 71px;
background-color: #fff;
border-right: 1px solid #eee;
line-height: 80px;
overflow: hidden;
}

.close-button {
line-height: 70px;
color: #eee;
font-size: 25px;
margin-left: 20px;
cursor: pointer
}

.close-button:hover {
color: #b9b9b9;
}

.sidebar-videos {
font-size: 18px;
text-align: center;
}

.uploadvideo {
color: #707070;
text-align: center;
text-transform: uppercase;
font-size: 14px;
width: 250px;
height: 80px;
border-bottom: 1px solid #eee;
margin-bottom: 10px;
}

.thumbnail {
border: 1px solid #eee;
height: 120px;
width: 200px;
margin-left: 22px;
cursor: pointer;
margin-bottom: 15px;
}

.thumbnail:hover {
border: 1px solid #b9b9b9;
}

.upload-btn-wrapper {
position: relative;
overflow: hidden;
display: inline-block;
}

.btn {
cursor: pointer;
border: 1px solid #eee;
background-color: #8BC34A;
padding: 13px 15px;
color: #fff;
}

.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}

.search {
float: right;
width: 250px;
height: 70px;
border-left: 1px solid #eee;
}
<!DOCTYPE html>
<html>

<head>
<title>Video CMS</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/sidebar.js"></script>
</head>

<body>
<div class="header">
<div class="search"></div>
</div>
<div class="toggle">
<div class="group-buttons"></div>
<span class="close-button" onclick="closeNav()">&#8592;</span>
<span class="close-button" onclick="openNav()">&#8594;</span>
</div>
<div id="sidenav" class="sidenav">
<div class="uploadvideo">
<div class="upload-btn-wrapper">
<button class="btn">Video Uploaden</button>
<input type="file" name="myfile" />
</div>
</div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
</div>
</body>

</html>

最佳答案

只需给打开按钮一个display:none:

.sidebar-button {
line-height: 70px;
color: #eee;
font-size: 25px;
margin-left: 20px;
cursor:pointer
}

.sidebar-button:hover {
color: #b9b9b9;
}

/** The open button isn't visible by default, since the sidebar is already open **/

#open-button {
display:none;
}

然后根据侧边栏是否打开改变按钮有display:nonedisplay:block:

function openNav() {
document.getElementById("sidenav").style.width = "250px";
document.getElementById("close-button").style.display = "block";
document.getElementById("open-button").style.display = "none";
}

function closeNav() {
document.getElementById("sidenav").style.width = "0";
document.getElementById("close-button").style.display = "none";
document.getElementById("open-button").style.display = "block";
}

您可能还应该给按钮一个 user-select: none; 以防止他们像文本一样选择按钮。

完整代码:

function openNav() {
document.getElementById("sidenav").style.width = "250px";
document.getElementById("close-button").style.display = "block";
document.getElementById("open-button").style.display = "none";
}

function closeNav() {
document.getElementById("sidenav").style.width = "0";
document.getElementById("close-button").style.display = "none";
document.getElementById("open-button").style.display = "block";
}
body {
background-color: #fcfcfc;
font-family: Roboto, Arial, sans-serif;
}

.sidebar-button {
line-height: 70px;
color: #eee;
font-size: 25px;
margin-left: 20px;
cursor: pointer;
user-select: none;
}

.sidebar-button:hover {
color: #b9b9b9;
}


/** The open button isn't visible by default, since the sidebar is already open **/

#open-button {
display: none;
}

.header {
width: 100%;
height: 70px;
top: 0;
left: 0;
position: fixed;
background-color: #fff;
border-bottom: 1px solid #eee;
}

.toggle {
width: 250px;
height: 70px;
border-right: 1px solid #eee;
position: fixed;
top: 0;
left: 0;
}

.sidenav {
height: 100%;
width: 250px;
top: 0;
left: 0;
position: fixed;
z-index: 1;
overflow-x: hidden;
transition: 0.3s;
margin-top: 71px;
background-color: #fff;
border-right: 1px solid #eee;
line-height: 80px;
overflow: hidden;
}

.sidebar-videos {
font-size: 18px;
text-align: center;
}

.uploadvideo {
color: #707070;
text-align: center;
text-transform: uppercase;
font-size: 14px;
width: 250px;
height: 80px;
border-bottom: 1px solid #eee;
margin-bottom: 10px;
}

.thumbnail {
border: 1px solid #eee;
height: 120px;
width: 200px;
margin-left: 22px;
cursor: pointer;
margin-bottom: 15px;
}

.thumbnail:hover {
border: 1px solid #b9b9b9;
}

.upload-btn-wrapper {
position: relative;
overflow: hidden;
display: inline-block;
}

.btn {
cursor: pointer;
border: 1px solid #eee;
background-color: #8BC34A;
padding: 13px 15px;
color: #fff;
}

.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}

.search {
float: right;
width: 250px;
height: 70px;
border-left: 1px solid #eee;
}
<!DOCTYPE html>
<html>

<head>
<title>Video CMS</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/sidebar.js"></script>
</head>

<body>
<div class="header">

<div class="search"></div>
</div>
<div class="toggle">
<div class="group-buttons"></div>
<span class="sidebar-button" id="close-button" onclick="closeNav()">&#8592;</span>
<span class="sidebar-button" id="open-button" onclick="openNav()">&#8594;</span>
</div>
<div id="sidenav" class="sidenav">
<div class="uploadvideo">
<div class="upload-btn-wrapper">
<button class="btn">Video Uploaden</button>
<input type="file" name="myfile" />
</div>
</div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
</div>
</body>

</html>

关于javascript - 边栏打开/关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52137773/

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