你好,我正在準備邊欄,遇到了一個問題。問題是當頁面被重定向到一個頁面時,導航鏈接沒有獲得活動類。如果需要更多信息,請在評論區問我。但希望你能理解。我試著做了,看看我的工作,幫我改正錯誤。
const sidebar = document.querySelector('#mySidebar')
const toggle = document.querySelector('#sidebar-toggle')
toggle.addEventListener('click', toggleSidebar)
function toggleSidebar(e) {
toggle.classList.toggle('open')
sidebar.classList.toggle('open');
}
//for sidebar active class on page
const activePage = window.location.pathname;
const navLinks = document.querySelectorAll('.sidebar-nav ul li a').
forEach(link => {
if(link.href.includes(`${activePage}`)) {
link.classList.add('active');
}
})
//for sidebar active class on page
*{
margin:0;
padding:0;
box-sizing:border-box;
}
header{
padding: 0 20px;
height: 40px;
width:100%;
background: coral;
box-shadow: 0px 0px 2px 0px rgba(0, 0, 0, 0.75);
display: flex;
justify-content: space-between;
z-index: 1001;
}
header img{
width:40px;
height:40px;
}
#sidebar-toggle{
display: inline-block;
cursor: pointer;
}
#sidebar-toggle div{
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
#sidebar-toggle.open .bar4 {
transform: translate(0, 11px) rotate(-45deg);
}
#sidebar-toggle.open .bar5 {
opacity: 0;
}
#sidebar-toggle.open .bar6 {
transform: translate(0, -11px) rotate(45deg);
}
.sidebar-header img{
width:30px;
}
.sidebar {
display:none;
position: fixed;
height: 100vh;
top: 41px;
left: 0;
background-color: #fff;
width: 15.625rem;
box-shadow: 0px 0px 2px 0px rgba(0, 0, 0, 0.75);
}
.open.sidebar {
display: flex;
}
.sidebar-header {
display: flex;
padding: 5px;
}
.sidebar-header img {
flex-wrap: wrap;
pointer-events: none;
-moz-user-select: none;
-webkit-user-select: none;
user-select: none;
border-radius: 50%;
width: 41px;
float: none;
display: block;
object-fit: fill;
height: 40px;
}
.sidebar-header h6 {
display: flex;
justify-content: flex-end;
}
.sidebar-nav{
margin: 0;
overflow: auto;
margin-bottom: 35px;
}
.sidebar-nav ul {
display:flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
list-style:none;
padding: 0 15px;
line-height: 30px;
box-sizing:border-box;
}
.sidebar-nav ul li{
width:100%;
box-sizing:border-box;
color:#007bff !important;
padding: 5px 10px;
margin: 1px 0;
}
.sidebar-nav ul li a{
text-decoration:none;
cursor: pointer;
font-weight: 300!important;
}
.sidebar-nav ul .active,
.sidebar-nav ul .active a .icon{
background:#007bff;
color: #ffffff;
border-radius: 10px;
}
.sidebar-nav ul li:hover,
.sidebar-nav ul li:hover a .icon,
.sidebar-nav ul li:hover a{
background:#007bff;
color: #ffffff;
border-radius: 10px;
}
.sidebar-nav ul li .icon{
color:#007bff;
width:30px;
display: inline-block;
}
.sidebar-nav .sidebar-nav-header{
text-transform: uppercase;
font-size: 11px;
margin: -0.75rem 1.5rem;
color:#0c7db1;
}
.sidebar-nav ul ul{
position: static;
display: none;
}
.sidebar-nav ul ul li{
line-height: 25px;
}
.sidebar-nav ul ul li a{
padding-left: 20px;
}
.sidebar-nav ul li a .caret{
position: relative;
float: right;
margin-top: 8px;
transition: transform 0.4s;
}
.sidebar-nav ul li a:hover .caret{
transform: rotate(-180deg);
}
.sidebar-nav ul .sub-menu.show{
display: block;
}
.sidebar-nav ul li a .caret .sub-menu.show{
transform: rotate(-180deg);
}
.sidebar-nav ul li span.number{
float: right;
font-size: 12px;
color: #007bff;
cursor: pointer;
font-weight: 500!important;
}
.sidebar-nav ul li:hover span.number{
color: #fff;
}
<header>
<img src="https://cdn.dribbble.com/userupload/3158902/file/original-7c71bfa677e61dea61bc2acd59158d32.jpg?resize=400x0">
<div id="sidebar-toggle">
<div class="bar4"></div>
<div class="bar5"></div>
<div class="bar6"></div>
</div>
</header>
<div class="sidebar" id='mySidebar'>
<div class="sidebar-container">
<div class="sidebar-header">
<img src="https://i.pinimg.com/736x/0d/cf/b5/0dcfb548989afdf22afff75e2a46a508.jpg">
<h6>Umann goswami</h6>
</div>
<div class="sidebar-nav">
<ul>
<li><a href="index"><span class="icon"><i class="fas fa-home"></i></span>Home</a></li>
<li><a href="feed"><span class="icon"><i class="fas fa-chart-bar"></i></span>Feed</a></li>
<li class="caret"><a href="#"><span class="icon"><i class="fas fa-pen-nib"></i></span>Posts<span class="caret fas fa-angle-down">
</a>
<ul class="sub-menu">
<li><a href="">Blogs</a></li>
<li><a href="">Images</a></li>
</ul>
</li>
<li class="caret"><a href="#"><span class="icon"><i class="fas fa-pen-nib"></i></span>Posts<span class="caret fas fa-angle-down">
</a>
<ul class="sub-menu">
<li><a href="">Blogs</a></li>
<li><a href="">Images</a></li>
</ul>
</li>
<li><a href=""><span class="icon"><i class="far fa-bell"></i></span>Notifications</a><span class="number">14</span></li>
<li><a href=""><span class="icon"><i class="fas fa-file-alt"></i></span>Categories</a></li>
<li><a href=""><span class="icon"><i class="fas fa-cog"></i></span>Settings</a></li>
</ul>
</div>
</div>
</div>
<div id="main">
Welcome human
</div>