javascript - 如何在小屏幕上折叠侧边栏?

标签 javascript html css

我在做一个固定的侧边栏,但它不会崩溃..我真的不知道该怎么做。
我在互联网上搜索过.. 仍然找不到任何结果。

这是我的 sidebar.html

<div class="container-fluid" style="background-color:#19334d">
<div class="sidebar" id="sidebar">
    <div class="pt-5 text-center pb-2 ">
        <a href="../index.php" class="logo-normal pr-3">
            <img src='../assets/img/favicon.ico' width="50px">
        </a>
        <a href="../index.php" class="logo-normal">
            Fitness Addict 
        </a>
    </div>
    <hr style="border-color:white; width:90%" align=center>
    <ul class="nav pl-4">
        <li >
            <a href="../home/myhome.php">
                <i class="fa fa-home icon pr-4"></i>
                My Home
            </a>
        </li>                
        .
        .
        .
    </ul>
</div>

CSS:

.sidebar{
  height: calc(100vh - 90px);
  width: 230px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  background-size: cover;
  background-position: center center;
  display: block;
  background: #408000;
  box-shadow: 0px 0px 45px 0px rgba(0, 0, 0, 0.6);
  margin-top: 80px;
  margin-left: 20px;
  border-radius: 5px;
}

.sidebar .nav {
 margin-top: 20px;
 display: block;
}

我曾尝试通过 JavaScript 来实现,但我不知道如何实现。 任何人都可以帮忙吗? here is the view of my project

最佳答案

您的 JavaScript 可能如下所示:

/* Set the width of the sidebar to 250px and the left margin of the page content to 250px */

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

/* Set the width of the sidebar to 0 and the left margin of the page content to 0 */

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

然后您需要使用相关代码将此 JS 作为源添加到 HTML 中。通过W3Schools有关创建可折叠侧边栏的教程。

您可能还想在这里看看很棒的响应式侧边栏 tutorial on W3Schools .

关于javascript - 如何在小屏幕上折叠侧边栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52622183/

相关文章:

javascript - JQuery Remove 在页面刷新后返回?

html - 悬停后,下拉菜单无法在 Microsoft Edge 中正确隐藏

html - div 中的图标定位 - 忽略文本空格

html - 如何使按钮和搜索栏居中

css - 导航栏中的水平列表项

javascript - 返回服务对本地存储的 Angular 数据的 promise

javascript - 固定div重叠/隐藏下一个div

javascript - 在 react 状态下用数组替换字符串时出错

html - 通过 CSS 更改 PNG 图像的颜色?

html - 菜单中央的 Logo 图像位置不正确