侧边栏切换按钮不显示任何操作

Posted

技术标签:

【中文标题】侧边栏切换按钮不显示任何操作【英文标题】:Sidebar Toggle button does not show any action 【发布时间】:2021-07-14 05:29:02 【问题描述】:

我的 html 代码中使用的切换按钮不起作用。我尝试使用引导程序进行设计。我是新手。我给了我的index.html 文件和main.js 文件。我不知道我的错误在哪里。由于我是初学者,因此得到您的帮助非常有帮助。在此先感谢大家。

我的index.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Admin panel</title>

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
  
  <link rel="stylesheet" href="style.css">
</head>
<body>
  

<div class="wrapper">



  <nav id="sidebar">
    <div class="sidebar-header">
      <img src="default.jpg" class="img-fluid" >
      <div class="name" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Name</div>
                    <div class="email">Email</div>
    </div>

    <ul class="list-unstyled components">
      <p>The Providers</p>

      <li>
        <a href="#">Dashboard</a>
      </li>
      <li>
        <a href="#">Pending Request</a>
      </li>
      <li>
        <a href="#pageSubmenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle">Pages</a>
        <ul class="collapse list-unstyled" id="pageSubmenu">
          <li>
            <a href="#">Page 1</a>
          </li>
          <li>
            <a href="#">Page 1</a>
          </li>
        </ul>
      </li>
      <li class="active">
        <a href="#">Policy</a>
      </li>
      <li>
        <a href="#">Logout</a>
      </li>
    </ul>
  </nav>

  <!-- Page content holder -->
<div class="page-content p-5" id="content">
  <!-- Toggle button -->
  <button id="sidebarCollapse" type="button" class="btn btn-light bg-white rounded-pill shadow-sm px-4 mb-4"><i class="fa fa-bars mr-2"></i><small class="text-uppercase font-weight-bold">Toggle</small></button>

  <!-- Demo content -->
  <h2 class="display-4 text-black">Bootstrap vertical nav</h2>
  <p class="lead text-black mb-0">Build a fixed sidebar using Bootstrap 4 vertical navigation and media objects.</p> 
  <div class="separator"></div>
  <div class="row text-black">
    <div class="col-lg-12">
      
    </div>
  </div>

</div>
<!-- End demo content -->
  
</div>


  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" crossorigin="anonymous"></script>

  <script src="main.js"></script>
</body>
</html>

我的main.js 文件:


$(function()  
  $('#sidebarCollapse').on('click', function() 
    $('#sidebar, #content').toggleClass('active');
  );
);

我的style.css 文件

body 
  font-family: 'poppins', sans-serif;
  background: #fafafa;


p 
  font-family: 'poppins', sans-serif;
  font-size: 1.1em;
  font-weight: 300;
  line-height: 1.7em;
  color: #999;

a,
a:hover,
a:focus 
  color: inherit;
  text-decoration: none;
  transition: all 0.3s;



/* Side Bar */

.wrapper 
  display: flex;
  text-decoration: none;
  transition: all 0.3s;


.page-content 
  width: calc(100% - 17rem);
  margin-left: 0rem;
  transition: all 0.4s;
 

#sidebar 
  min-width: 250px;
  max-width: 250px;
  height: 100vh;
  background: #7386D5;
  color: #fff;
  transition: all 0.3s;


#sidebar .active 
  background-color: #e2694b;
    


.components li .active 
  background-color: #261313;


#sidebar .sidebar-header 
  padding: 20px;
  background: #6d7fcc;


#sidebar ul .components 
  padding: 20px 0;
  border-bottom: 1px solid #47748b;


#sidebar ul p 
  color: #fff;
  padding: 10px;


#sidebar ul li a 
  padding: 10px;
  font-size: 1.1em;
  display: block;

#sidebar ul li a:hover 
  color: #7385D5;
  background: #fff;


#sidebar ul li.active>a,
a[aria-expanded="true"] 
  color: #fff;
  background: #6d7fcc;


a[data-toggle="collapase"] 
  position: relative;


.dropdown-toggle::after 
  display: block;
  position: absolute;
  top: 50%;
  right: 20%;
  transform: translateY(-50%);


ul ul a 
  font-size: 0.9em !important;
  padding-left: 30px !important;
  background: #6d7fcc;


#content 
  width: 100%;
  padding: 20px;
  min-height: 100vh;
  transition: all 0.3s;


#sidebar img 
  width: 30%;
  



@media (max-width: 768px) 
  #sidebar 
    margin-left: -250px;
  
  #sidebar .active 
    margin-left: 0px;
  
  #sidebarCollapase span 
    display: none;
  

【问题讨论】:

【参考方案1】:

    function toggleIcon(e) 
        $(e.target)
            .prev('.panel-heading')
            .find(".more-less")
            .toggleClass('glyphicon-plus glyphicon-minus');
    
    $('.panel-group').on('hidden.bs.collapse', toggleIcon);
    $('.panel-group').on('shown.bs.collapse', toggleIcon);
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Admin panel</title>

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
  
  <link rel="stylesheet" href="style.css">
</head>
<body>
  

<div class="wrapper">



  <nav id="sidebar">
    <div class="sidebar-header">
      <img src="default.jpg" class="img-fluid" >
      <div class="name" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Name</div>
                    <div class="email">Email</div>
    </div>

    <ul class="list-unstyled components">
      <p>The Providers</p>

      <li>
        <a href="#">Dashboard</a>
      </li>
      <li>
        <a href="#">Pending Request</a>
      </li>
      <li>
        <a href="#pageSubmenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle">Pages</a>
        <ul class="collapse list-unstyled" id="pageSubmenu">
          <li>
            <a href="#">Page 1</a>
          </li>
          <li>
            <a href="#">Page 1</a>
          </li>
        </ul>
      </li>
      <li class="active">
        <a href="#">Policy</a>
      </li>
      <li>
        <a href="#">Logout</a>
      </li>
    </ul>
  </nav>
  
    
    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">

        <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingOne">
                <h4 class="panel-title">
                
            <button data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne" class="btn btn-light bg-white rounded-pill shadow-sm px-4 mb-4"><i class="fa fa-bars mr-2"></i><small class="text-uppercase font-weight-bold">Toggle</small></button>
                </h4>
            </div>
            <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
                <div class="panel-body">
                       <h2 class="display-4 text-black">Bootstrap vertical nav</h2>
  <p class="lead text-black mb-0">Build a fixed sidebar using Bootstrap 4 vertical navigation and media objects.</p> 
                </div>
            </div>
        </div>

        
    </div>
</div>


  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" crossorigin="anonymous"></script>

  <script src="main.js"></script>
</body>
</html>

【讨论】:

我是新人,所以说在哪里更改非常有用? 你可以从here阅读更多,如果我的回答有帮助你能接受吗? 我给了我的整个代码来看看运行?

以上是关于侧边栏切换按钮不显示任何操作的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式在闪亮仪表板中切换侧边栏菜单的显示

在父悬停过渡后显示子

当使用 Tailwind CSS 单击按钮时,从屏幕左侧制作侧边栏滑动

导航视图 - 侧边栏等注释 - 切换按钮

Sublime Text 2 - 在侧边栏中显示文件导航

在Vue 3中单击路由器链接时如何在不重新加载页面的情况下切换侧边栏