I got the code below from the official bootstrap 5 demo and for the life of me, can't figure how to move the off-canvas menu from Left-to-Right. The documentation code to place the offcanvas-start
and the demo code are completely different.
JSFiddle DEMO: https://jsfiddle.net/fn0vypzt/
HTML:
<nav class="navbar navbar-expand-lg fixed-top navbar-dark bg-dark" aria-label="Main navigation">
<div class="container-fluid">
<a class="navbar-brand" href="#">Offcanvas navbar</a>
<button class="navbar-toggler p-0 border-0" type="button" id="navbarSideCollapse" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse offcanvas-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Notifications</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Switch account</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-bs-toggle="dropdown" aria-expanded="false">Settings</a>
<ul class="dropdown-menu" aria-labelledby="dropdown01">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
CSS:
@media (max-width: 991.98px) {
.offcanvas-collapse {
position: fixed;
top: 56px; /* Height of navbar */
bottom: 0;
left: 100%;
width: 100%;
padding-right: 1rem;
padding-left: 1rem;
overflow-y: auto;
visibility: hidden;
background-color: #343a40;
transition: transform .3s ease-in-out, visibility .3s ease-in-out;
}
.offcanvas-collapse.open {
visibility: visible;
transform: translateX(-100%);
}
}
JS:
(function () {
'use strict'
document.querySelector('#navbarSideCollapse').addEventListener('click', function () {
document.querySelector('.offcanvas-collapse').classList.toggle('open')
})
})()
2条答案
按热度按时间bbuxkriu1#
如果你只是想让菜单从左边而不是从右边滑进去,那么把
.offcanvas-collapse
从left: 100%
改为right: 100%
,把.offcanvas-collapse.open
从transform: translateX(-100%);
改为100%(没有减号)就可以了。第一个
w6mmgewl2#
.offcanvas-start将offcanvas放置在 windows 的左侧。在offcanvas*div下面切换按钮**上使用此类