目录
2、iframe实现效果
二、左侧菜单栏样式
菜鸟左侧菜单栏中有5个示例,基本满足了大部分场景需求。
参考文档:Bootstrap5 侧边栏导航(Offcanvas) | 菜鸟教程
我这里采用的是第4个样式,这个样式只展示了左侧菜单栏。
接下来我将实现左侧菜单栏和右侧主界面的联动跳转。
三、菜单栏和右侧主界面的联动跳转
有2中比较常用的实现方式:
1、使用div实现主界面切换
实现思路
- 动态监听 a 的 click事件;
- 取消默认click事件(跳转到一个新的窗口,而不是当前页面的div中);
- 将 a的 href 链接地址load到div元素上
关键代码
主页面div元素:
<div id="main"></div>
div的js跳转代码:
$(document).ready(function () {
$("#main").load('tab1.html');
$("a").click(function (e) {
e.preventDefault();
// $(this).tab('show');
$("#main").load($(this).attr("href"));
})
})
2、使用iframe实现主界面切换,src指定对应的*.html文件。
实现思路
- 动态监听 a 的 click事件;
- 取消默认click事件(跳转到一个新的窗口,而不是当前页面的div中);
- 修改iframe的src属性为click元素的href值;
关键代码
主页面iframe元素:
<iframe id="navFrame" width="100%" height="auto" scrolling="no" frameborder="10" src="tab1.html"></iframe>
iframe的js跳转代码:
<script>
$(document).ready(function () {
$("a").click(function (e) {
if ($(this).attr("href").length) {
e.preventDefault();
$(this).tab('show');
$("#navFrame").attr("src", $(this).attr("href"));
}
})
});
</script>
四、一页完整代码
1、iframe方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="js/jquery-2.2.3.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
</head>
<body>
<!-- 标题 -->
<div class="row bg-light">
<h2 style="font-weight: bold;">BootStrap 胶囊选项卡示例</h2>
</div>
<br>
<div class="container-fluid">
<div class="row">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="tab1.html">Active</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="" role="button"
aria-expanded="false">Dropdown</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="tab1.html">Action</a></li>
<li><a class="dropdown-item" href="tab2.html">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="tab3.html">Separated link</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="tab2.html">Link</a>
</li>
<li class="nav-item">
<a class="nav-link " href="tab3.html">Other</a>
</li>
</ul>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12 bg-success ">
<iframe id="navFrame" width="100%" height="auto" scrolling="no" frameborder="10" src="tab1.html"></iframe>
</div>
</div>
<script>
$(document).ready(function () {
$("a").click(function (e) {
if ($(this).attr("href").length) {
e.preventDefault();
$(this).tab('show');
$("#navFrame").attr("src", $(this).attr("href"));
}
})
});
</script>
</body>
</html>
2、div方式
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="js/jquery-2.2.3.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script>
<style>
body {
min-height: 100vh;
min-height: -webkit-fill-available
}
html {
height: -webkit-fill-available
}
main {
display: flex;
flex-wrap: nowrap;
height: 100vh;
height: -webkit-fill-available;
max-height: 100vh;
overflow-x: auto;
overflow-y: hidden
}
.b-example-divider {
flex-shrink: 0;
width: 1.5rem;
height: 100vh;
background-color: rgba(0, 0, 0, .1);
border: solid rgba(0, 0, 0, .15);
border-width: 1px 0;
box-shadow: inset 0 .5em 1.5em rgba(0, 0, 0, .1), inset 0 .125em .5em rgba(0, 0, 0, .15)
}
.bi {
vertical-align: -.125em;
pointer-events: none;
fill: currentColor
}
.dropdown-toggle {
outline: 0
}
.nav-flush .nav-link {
border-radius: 0
}
.btn-toggle {
display: inline-flex;
align-items: center;
padding: .25rem .5rem;
font-weight: 600;
color: rgba(0, 0, 0, .65);
background-color: transparent;
border: 0
}
.btn-toggle:hover,
.btn-toggle:focus {
color: rgba(0, 0, 0, .85);
background-color: #d2f4ea
}
.btn-toggle::before {
width: 1.25em;
line-height: 0;
content: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='rgba%280,0,0,.5%29' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M5 14l6-6-6-6'/%3e%3c/svg%3e");
transition: transform .35s ease;
transform-origin: .5em 50%
}
.btn-toggle[aria-expanded="true"] {
color: rgba(0, 0, 0, .85)
}
.btn-toggle[aria-expanded="true"]::before {
transform: rotate(90deg)
}
.btn-toggle-nav a {
display: inline-flex;
padding: .1875rem .5rem;
margin-top: .125rem;
margin-left: 1.25rem;
text-decoration: none
}
.btn-toggle-nav a:hover,
.btn-toggle-nav a:focus {
background-color: #d2f4ea
}
.scrollarea {
overflow-y: auto
}
.fw-semibold {
font-weight: 600
}
.lh-tight {
line-height: 1.25
}
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none
}
@media (min-width:768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem
}
}
</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol id="bootstrap" viewBox="0 0 118 94">
<title>Bootstrap</title>
<path fill-rule="evenodd" clip-rule="evenodd"
d="M24.509 0c-6.733 0-11.715 5.893-11.492 12.284.214 6.14-.064 14.092-2.066 20.577C8.943 39.365 5.547 43.485 0 44.014v5.972c5.547.529 8.943 4.649 10.951 11.153 2.002 6.485 2.28 14.437 2.066 20.577C12.794 88.106 17.776 94 24.51 94H93.5c6.733 0 11.714-5.893 11.491-12.284-.214-6.14.064-14.092 2.066-20.577 2.009-6.504 5.396-10.624 10.943-11.153v-5.972c-5.547-.529-8.934-4.649-10.943-11.153-2.002-6.484-2.28-14.437-2.066-20.577C105.214 5.894 100.233 0 93.5 0H24.508zM80 57.863C80 66.663 73.436 72 62.543 72H44a2 2 0 01-2-2V24a2 2 0 012-2h18.437c9.083 0 15.044 4.92 15.044 12.474 0 5.302-4.01 10.049-9.119 10.88v.277C75.317 46.394 80 51.21 80 57.863zM60.521 28.34H49.948v14.934h8.905c6.884 0 10.68-2.772 10.68-7.727 0-4.643-3.264-7.207-9.012-7.207zM49.948 49.2v16.458H60.91c7.167 0 10.964-2.876 10.964-8.281 0-5.406-3.903-8.178-11.425-8.178H49.948z">
</path>
</symbol>
<symbol id="home" viewBox="0 0 16 16">
<path
d="M8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4.5a.5.5 0 0 0 .5-.5v-4h2v4a.5.5 0 0 0 .5.5H14a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146zM2.5 14V7.707l5.5-5.5 5.5 5.5V14H10v-4a.5.5 0 0 0-.5-.5h-3a.5.5 0 0 0-.5.5v4H2.5z" />
</symbol>
</svg>
<main>
<h1 class="visually-hidden">Sidebars examples</h1>
<div class="flex-shrink-0 p-3 bg-white" style="width: 280px;">
<a href="/" class="d-flex align-items-center pb-3 mb-3 link-dark text-decoration-none border-bottom">
<svg class="bi me-2" width="30" height="24">
<use xlink:href="#bootstrap" />
</svg>
<span class="fs-5 fw-semibold">Collapsible</span>
</a>
<ul class="list-unstyled ps-0">
<li class="mb-1">
<button class="btn btn-toggle align-items-center rounded collapsed" data-bs-toggle="collapse"
data-bs-target="#home-collapse" aria-expanded="true">
Home
</button>
<div class="collapse show" id="home-collapse">
<ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
<li><a href="tab1.html" class="link-dark rounded">Tab1</a></li>
<li><a href="tab2.html" class="link-dark rounded">Tab2</a></li>
<li><a href="tab3.html" class="link-dark rounded">Tab3</a></li>
</ul>
</div>
</li>
<li class="mb-1">
<button class="btn btn-toggle align-items-center rounded collapsed" data-bs-toggle="collapse"
data-bs-target="#orders-collapse" aria-expanded="false">
Orders
</button>
<div class="collapse" id="orders-collapse">
<ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
<li><a href="#" class="link-dark rounded">New</a></li>
<li><a href="#" class="link-dark rounded">Processed</a></li>
<li><a href="#" class="link-dark rounded">Shipped</a></li>
<li><a href="#" class="link-dark rounded">Returned</a></li>
</ul>
</div>
</li>
<li class="border-top my-3"></li>
<li class="mb-1">
<button class="btn btn-toggle align-items-center rounded collapsed" data-bs-toggle="collapse"
data-bs-target="#account-collapse" aria-expanded="false">
Account
</button>
<div class="collapse" id="account-collapse">
<ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
<li><a href="#" class="link-dark rounded">New...</a></li>
</ul>
</div>
</li>
</ul>
</div>
<div class="b-example-divider"></div>
<div id="main"></div>
</main>
<script>
/* global bootstrap: false */
(function () {
'use strict'
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
tooltipTriggerList.forEach(function (tooltipTriggerEl) {
new bootstrap.Tooltip(tooltipTriggerEl)
})
})()
$(document).ready(function () {
$("#main").load('tab1.html');
$("a").click(function (e) {
e.preventDefault();
// $(this).tab('show');
$("#main").load($(this).attr("href"));
})
})
</script>
</body>
</html>