javascript 如何在较小屏幕上将Bootstrap 5.3垂直导航栏转换为非画布

iyr7buue  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(85)

由于Bootstrap 5的文档中没有相应的垂直导航栏,我决定自己构建一个。我做了一个侧面有导航栏的 Jmeter 板。正如你在标题中所读到的,我希望我的垂直导航栏在大屏幕上可见,每当屏幕变小时,我希望导航栏过渡到offcanvas。
我在我的导航栏上添加了offcanvas类,但是这使得我的导航消失了。我怎样才能最有效地在导航栏和offcanvas之间切换呢?

@import url('https://fonts.googleapis.com/css2?family=Rubik:ital,wght@0,300;0,400;0,500;0,700;0,900;1,300;1,400;1,500;1,700;1,900&display=swap');
@import url("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css");

*{
  font-family: Rubik;
}

.container{
    max-width: 1165px !important;
}

.text-underline-hover {
    text-decoration: none;
}

.text-underline-hover:hover {
    text-decoration: underline;
}
#pricing{
  border-radius: 15px !important;
}

.input-group-lg>.btn, .input-group-lg>.form-control, .input-group-lg>.form-select, .input-group-lg>.input-group-text {
  font-size: 1rem !important;
  padding: 0.75rem 1rem !important;
}

.btn{
  color: white !important;
  background-color: #161616 !important;
  border: 0 none !important;
  border-radius: 100px !important;
  padding: 12.5px 40px !important;
}
.btn-lg{

  padding: 12.5px 65px !important;
}
.btn:hover{
  background-color: #332E33 !important;
  outline:0 !important;
}
.btn:focus{
  outline:0 !important;
}
.btn:active {
  background-color: #171715 !important;
  outline:0 !important;
}

/*custom*/
.background{
  background: #CBF3F0 !important;
}
.background2{
  background: #FFF !important;
}

.fw-100{
  font-weight: 100 !important;
}
.fw-200{
  font-weight: 200 !important;
}
.fw-300{
  font-weight: 300 !important;
}
.fw-400{
  font-weight: 400 !important; /* normal */
}
.fw-500{
  font-weight: 500 !important;
}
.fw-600{
  font-weight: 600 !important;
}
.fw-700{
  font-weight: 700 !important; /* bold */
}
.fw-800{
  font-weight: 800 !important;
}
.fw-900{
  font-weight: 900 !important;
}

.fs-05{
  font-size: 0.5rem !important;
}
.fs-06{
  font-size: 0.6rem !important;
}
.fs-07{
  font-size: 0.7rem !important;
}
.fs-08{
  font-size: 0.8rem !important;
}
.fs-09{
  font-size: 0.9rem !important;
}
.fs-10{
  font-size: 1.0rem !important;
}
.fs-11{
  font-size: 1.1rem !important;
}
.fs-12{
  font-size: 1.2rem !important;
}
.fs-13{
  font-size: 1.3rem !important;
}
.fs-14{
  font-size: 1.4rem !important;
}
.fs-15{
  font-size: 1.5rem !important;
}
.fs-16{
  font-size: 1.6rem !important;
}
.fs-17{
  font-size: 1.7rem !important;
}
.fs-18{
  font-size: 1.8rem !important;
}
.fs-19{
  font-size: 1.9rem !important;
}
.fs-20{
  font-size: 2.0rem !important;
}
.fs-21{
  font-size: 2.1rem !important;
}
.fs-22{
  font-size: 2.2rem !important;
}
.fs-23{
  font-size: 2.3rem !important;
}
.fs-24{
  font-size: 2.4rem !important;
}
.fs-25{
  font-size: 2.5rem !important;
}
.fs-26{
  font-size: 2.6rem !important;
}
.fs-27{
  font-size: 2.7rem !important;
}
.fs-28{
  font-size: 2.8rem !important;
}
.fs-29{
  font-size: 2.9rem !important;
}
.fs-30{
  font-size: 3.0rem !important;
}
.fs-31{
  font-size: 3.1rem !important;
}
.fs-32{
  font-size: 3.2rem !important;
}
.fs-33{
  font-size: 3.3rem !important;
}
.fs-34{
  font-size: 3.4rem !important;
}
.fs-35{
  font-size: 3.5rem !important;
}
.fs-36{
  font-size: 3.6rem !important;
}
.fs-37{
  font-size: 3.7rem !important;
}
.fs-38{
  font-size: 3.8rem !important;
}
.fs-39{
  font-size: 3.9rem !important;
}
.fs-40{
  font-size: 4.0rem !important;
}

.br-5{
  border-radius: 5px !important;
}
.br-10{
  border-radius: 10px !important;
}
.br-15{
  border-radius: 15px !important;
}
.br-20{
  border-radius: 20px !important;
}
.br-25{
  border-radius: 25px !important;
}
.br-30{
  border-radius: 30px !important;
}
.br-35{
  border-radius: 35px !important;
}
.br-40{
  border-radius: 40px !important;
}
.br-45{
  border-radius: 45px !important;
}
.br-50{
  border-radius: 50px !important;
}

.custom-text-truncate {
    -webkit-line-clamp: 3;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.custom-search {
  position: relative;
  width: 100%;
}
.custom-search-input {
  width: 100%;
  border: none;
  border-radius: 100px;
  padding: 25px 27.5px;
  line-height: 1;
  box-sizing: border-box;
  outline: none;
}
.custom-search-botton {
  position: absolute;
  right: 7.5px; 
  top: 7.5px;
  bottom: 7.5px;
  border: 0;
  background: #161616;
  color: #fff;
  outline: none;
  margin: 0;
  padding: 0 80px;
  border-radius: 100px;
  z-index: 2;
  font-size: 1.25rem !important;
}



.btn-toggle-nav a {
  padding: .1875rem .5rem;
  margin-top: .125rem;
  margin-left: .5rem;
}
.btn-toggle-nav a:hover,
.btn-toggle-nav a:focus {
  background-color: #CBF3F0;
  border-radius: 5px;
}
#navbar{
  background-color: transparent !important;
  padding: .1875rem .5rem;
  margin-top: .125rem;
}
#navbar:hover{
  background-color: #CBF3F0 !important;
  border-radius: 5px;
}
.sb-active{
  background-color: #CBF3F0 !important;
  border-radius: 5px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="author" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Status | Hosting</title>
    <link rel="stylesheet" href="../public/css/style.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
</head>
<body style="background-color: #F4F4F5;">

<div class="container-fluid">
    <div class="row">
        <div class="col-2 p-0">
            <div class="offcanvas offcanvas-start show" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
                <div class="d-flex" style="height:100vh;">
                    <nav class="bg-light p-2 h-100 w-100">
                        <div class="offcanvas-header ps-2 pe-2">
                            <h5 class="offcanvas-title" id="offcanvasExampleLabel">Offcanvas</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
                        </div>
                        <!-- <div class="ps-2 pe-2">
                            <a href="index" class="d-flex align-items-center pb-3 mb-3 link-dark text-decoration-none border-bottom">
                                <h3>Hosting</h3>
                            </a>    
                        </div> -->
                        <ul class="nav flex-column ps-2 pe-2">
                            <li class="nav-item">
                                <a href="index" class="link-dark d-inline-flex text-decoration-none fw-500">
                                    <button class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500" id="navbar">
                                        Home
                                    </button>
                                </a>
                            </li>
                            <hr/>
                            <li class="nav-item">
                                <span class="dropdown-toggle">
                                    <button role="button" class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500 collapsed" data-bs-toggle="collapse" data-bs-target="#my-services" aria-expanded="true" id="navbar">
                                        My Services
                                    </button>
                                </span>
                                <div class="collapse" id="my-services">
                                    <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
                                        <li><a href="domain" class="link-dark d-inline-flex text-decoration-none">Domains</a></li>
                                        <li><a href="hosting" class="link-dark d-inline-flex text-decoration-none">Hosting</a></li>
                                        <li><a href="transfer" class="link-dark d-inline-flex text-decoration-none">Transfer</a></li>
                                    </ul>
                                </div>
                            </li>
                            <hr/>
                            <li class="nav-item">
                                <span class="dropdown-toggle">
                                    <button role="button" class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500 collapsed" data-bs-toggle="collapse" data-bs-target="#my-profile" aria-expanded="true" id="navbar">
                                        My Profile
                                    </button>
                                </span>
                                <div class="collapse" id="my-profile">
                                    <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
                                        <li><a href="accountdetails" class="link-dark d-inline-flex text-decoration-none">Account Details</a></li>
                                        <li><a href="changepassword" class="link-dark d-inline-flex text-decoration-none">Change Password</a></li>
                                        <li><a href="newsletter" class="link-dark d-inline-flex text-decoration-none">Newsletter</a></li>
                                    </ul>
                                </div>
                            </li>
                            <hr/>
                            <li class="nav-item">
                                <span class="dropdown-toggle">
                                    <button role="button" class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500 collapsed" data-bs-toggle="collapse" data-bs-target="#billing" aria-expanded="true" id="navbar">
                                        Billing
                                    </button>
                                </span>
                                <div class="collapse" id="billing">
                                    <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
                                        <li><a href="invoice" class="link-dark d-inline-flex text-decoration-none">Overview</a></li>
                                        <li><a href="paymentdetails" class="link-dark d-inline-flex text-decoration-none">Payment Details</a></li>
                                    </ul>
                                </div>
                            </li>
                            <hr/>
                            <li class="nav-item">
                                <a href="status" class="link-dark d-inline-flex text-decoration-none fw-500 sb-active">
                                    <button class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500" id="navbar">
                                        Status
                                    </button>
                                </a>
                            </li>
                            <hr/>
                            <li class="nav-item">
                                <a href="../" class="link-dark d-inline-flex text-decoration-none fw-500">
                                    <button class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500" id="navbar">
                                        Logout
                                    </button>
                                </a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
        <div class="col-10 ps-5 pe-5">
            <div class="container-lg">
                <h1>Status</h1>
                <nav style="--bs-breadcrumb-divider: url(&#34;data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8'%3E%3Cpath d='M2.5 0L1 1.5 3.5 4 1 6.5 2.5 8l4-4-4-4z' fill='%236c757d'/%3E%3C/svg%3E&#34;);" aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="#">Home</a></li>
                        <li class="breadcrumb-item active" aria-current="page">Status</li>
                    </ol>
                </nav>
                <div class="row">
                    <div class="col-12">
                        <div class="alert alert-success" role="alert">
                            All Systems Operational
                        </div>
                        <span class="small">Last Updated on February 10th @ 10:43AM CEST</span>
                    </div>  
                    <div class="col-12">
                        <div class="accordion">
                            <div class="accordion-item p-4">
                                <div class="row">
                                    <div class="col-6">
                                        <span>Servers</span>
                                    </div>
                                    <div class="col-6 text-end">
                                        <span class="text-success">Operational</span>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-item p-4">
                                <div class="row">
                                    <div class="col-6">
                                        <span>Login</span>
                                    </div>
                                    <div class="col-6 text-end">
                                        <span class="text-success">Operational</span>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-item p-4">
                                <div class="row">
                                    <div class="col-6">
                                        <span>Support</span>
                                    </div>
                                    <div class="col-6 text-end">
                                        <span class="text-success">Operational</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasExample" aria-controls="offcanvasExample">
                          Button with data-bs-target
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script src="../public/js/script.js"></script>  
</body>
</html>
xbp102n0

xbp102n01#

看看你的代码和下面的引导文档v5.3(https://getbootstrap.com/docs/5.3/components/navbar/#offcanvas),这里我有done.so看,希望你得到你的答案。

<div class="container-fluid">
    <div class="row">
        <div class="col-12 col-md-2">
            <nav class="navbar bg-body-tertiary navbar-expand-md">
                <div class="container-fluid flex-row flex-md-column">
                    <a class="navbar-brand" href="#">Offcanvas navbar</a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas"
                        data-bs-target="#offcanvasNavbar" aria-controls="offcanvasNavbar">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasNavbar"
                        aria-labelledby="offcanvasNavbarLabel">
                        <div class="offcanvas-header">
                            <h5 class="offcanvas-title" id="offcanvasNavbarLabel">Offcanvas</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="offcanvas"
                                aria-label="Close"></button>
                        </div>
                        <div class="offcanvas-body">
                            <ul class="nav flex-column flex-fill ps-2 pe-2">
                                <li class="nav-item">
                                    <a href="index" class="link-dark d-inline-flex text-decoration-none fw-500">
                                        <button
                                            class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500"
                                            id="navbar">
                                            Home
                                        </button>
                                    </a>
                                </li>
                                <hr />
                                <li class="nav-item">
                                    <span class="dropdown-toggle">
                                        <button role="button"
                                            class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500 collapsed"
                                            data-bs-toggle="collapse" data-bs-target="#my-services"
                                            aria-expanded="true" id="navbar">
                                            My Services
                                        </button>
                                    </span>
                                    <div class="collapse" id="my-services">
                                        <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
                                            <li>
                                                <a href="domain"
                                                    class="link-dark d-inline-flex text-decoration-none">Domains</a>
                                            </li>
                                            <li>
                                                <a href="hosting"
                                                    class="link-dark d-inline-flex text-decoration-none">Hosting</a>
                                            </li>
                                            <li>
                                                <a href="transfer"
                                                    class="link-dark d-inline-flex text-decoration-none">Transfer</a>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                                <hr />
                                <li class="nav-item">
                                    <span class="dropdown-toggle">
                                        <button role="button"
                                            class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500 collapsed"
                                            data-bs-toggle="collapse" data-bs-target="#my-profile"
                                            aria-expanded="true" id="navbar">
                                            My Profile
                                        </button>
                                    </span>
                                    <div class="collapse" id="my-profile">
                                        <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
                                            <li>
                                                <a href="accountdetails"
                                                    class="link-dark d-inline-flex text-decoration-none">Account
                                                    Details</a>
                                            </li>
                                            <li>
                                                <a href="changepassword"
                                                    class="link-dark d-inline-flex text-decoration-none">Change
                                                    Password</a>
                                            </li>
                                            <li>
                                                <a href="newsletter"
                                                    class="link-dark d-inline-flex text-decoration-none">Newsletter</a>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                                <hr />
                                <li class="nav-item">
                                    <span class="dropdown-toggle">
                                        <button role="button"
                                            class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500 collapsed"
                                            data-bs-toggle="collapse" data-bs-target="#billing" aria-expanded="true"
                                            id="navbar">
                                            Billing
                                        </button>
                                    </span>
                                    <div class="collapse" id="billing">
                                        <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
                                            <li>
                                                <a href="invoice"
                                                    class="link-dark d-inline-flex text-decoration-none">Overview</a>
                                            </li>
                                            <li>
                                                <a href="paymentdetails"
                                                    class="link-dark d-inline-flex text-decoration-none">Payment
                                                    Details</a>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                                <hr />
                                <li class="nav-item">
                                    <a href="status"
                                        class="link-dark d-inline-flex text-decoration-none fw-500 sb-active">
                                        <button
                                            class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500"
                                            id="navbar">
                                            Status
                                        </button>
                                    </a>
                                </li>
                                <hr />
                                <li class="nav-item">
                                    <a href="../" class="link-dark d-inline-flex text-decoration-none fw-500">
                                        <button
                                            class="d-inline-flex align-items-middle text-decoration-none border-0 fw-500"
                                            id="navbar">
                                            Logout
                                        </button>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </nav>
        </div>
    </div>
</div>

并将以下css添加到样式表中

@media (min-width: 768px) {
        .navbar-expand-md .offcanvas {
            width: 100% !important;
        }
    }

这只是使用offcanvas的侧边栏导航,所以你需要处理侧边栏的高度和宽度以及内容,正如你所看到的,侧边栏的高度与导航链接的边距和填充一样多。希望你得到了你的答案:)

相关问题