英文:
Getting a toggle button on navbar to work
问题
我在平板电脑和移动设备上的导航栏遇到了问题。当我单击下拉按钮时,需要它占据整个页面。基本上,当我在移动设备或平板电脑上单击下拉按钮时,它必须占据整个高度。我认为我需要使用JavaScript来实现这一点,但我不知道具体如何做。
这是我的导航栏。我正在使用Bootstrap 4和一些CSS对其进行编辑,但我只需要在平板电脑和移动设备上实现上述所述的效果。
<nav class="navbar navbar-expand-lg navbar-light bg-yellow px-5 py-3 font-weight-bold" id="navbar">
<div class="logo d-flex flex-column">
<a href="index.html" class="mx-auto"><img src="./Design/Logo.png" width="40" height="40" class="logo-img"></a>
<a class="navbar-brand mx-auto font-weight-bold" href="#">Logo</a>
</div>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav w-90 justify-content-around ml-lg-auto">
<li class="nav-item">
<a class="nav-link" href="#">Academy</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Academy</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Academy</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Academy</a>
</li>
<li class="nav-item justify-self-end">
<a href="./contact.html" class="btn btn-danger">Button</a>
</li>
</ul>
</div>
</nav>
这是在平板电脑或移动设备上单击下拉按钮时所期望的外观,基本上它必须占据整个空间。
英文:
I am having trouble with a navbar when it's on tablet and mobile size. When I click on the dropdown button I need it to take up the whole page. Basically it has to take up the whole height when I click on the dropdown button for mobile or tablet. I think I need to use JavaScript for it but I don't know how exactly.
This is my navbar. I'm using Bootstrap 4 and also some CSS to edit it but I just need to somehow make it for tablet and mobile like I explained above.
<nav class="navbar navbar-expand-lg navbar-light bg-yellow px-5 py-3 font-weight-bold" id="navbar">
<div class="logo d-flex flex-column">
<a href="index.html" class="mx-auto"><img src="./Design/Logo.png" width="40" height="40" class="logo-img"></a>
<a class="navbar-brand mx-auto font-weight-bold" href="#">Logo</a>
</div>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav w-90 justify-content-around ml-lg-auto">
<li class="nav-item">
<a class="nav-link" href="#">Academy</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Academy</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Academy</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Academy</a>
</li>
<li class="nav-item justify-self-end">
<a href="./contact.html" class="btn btn-danger">Button</a>
</li>
</ul>
</div>
</nav>
This is how its supposed to look like when clicked on the dropdown button when on tablet or mobile—basically it has to take up the whole space.
答案1
得分: 1
我猜你想要在点击按钮时使导航栏变成全屏高度。为此,你需要一个JavaScript函数:
<script>
function toggleNav() {
document.querySelector("#navbar").style.height = "100%";
document.body.overflow = "hidden"; // 为了使页面不可滚动,添加了这一行。
}
</script>
这个脚本标签将放在body
标签的底部。它的基本作用是在调用时将 #navbar
的高度设置为 100%
。
现在,我们需要在点击按钮时调用这个函数。所以,我们将在按钮上添加 onclick="toggleNav();"
,像这样:
<button onclick="toggleNav();" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
如果你现在检查你的网页,你会看到在触发按钮时 #navbar
的高度确实变成了 100%
。但你不会看到任何变化,这是因为我们的HTML标签的高度受限制。因此,让我们通过添加以下CSS代码来修复这个问题:
html, body {
height: 100%;
}
如果这是你想要的,请告诉我。
英文:
I'm guessing that you want the navbar to be full-height when the button is clicked. For that you'll need a javascript function:
<script>
function toggleNav() {
document.querySelector("#navbar").style.height = "100%"
document.body.overflow = "hidden" // added this for making the page unscrollable.
}
</script>
This script tag will be placed at bottom of the body tag. And what it basically does is giving height: 100%;
to the #navbar
when called.
Now we need to call the function when the button is clicked. So, we'll add onclick="toggleNav();"
to the button. Like this:
<button onclick="toggleNav();" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
If you check your webpage now, you'll see that the #navbar
DOES get the height: 100%;
when the button is triggered. But you won't see anything happen, that is because our html tag's height is limited. So, lets fix that by adding these css codes:
html, body {
height: 100%;
}
Let me know if this is what you wanted.
答案2
得分: 1
根据我理解的,您希望通过隐藏内容使导航栏覆盖整个屏幕。在 Bootstrap 5 中,有一个名为 offcanvas 的组件提供此功能。
要在 Bootstrap 4 中实现类似的功能,您需要使用 jQuery。以下是在 Bootstrap 4 中使用 off-canvas 功能的示例代码。希望对您有帮助。
$(document).ready(function() {
var fixHeight = function() {
$('.navbar-nav').css(
'max-height',
document.documentElement.clientHeight - 150
);
};
fixHeight();
$(window).resize(function() {
fixHeight();
});
$('.navbar .navbar-toggler').on('click', function() {
fixHeight();
});
$('.navbar-toggler, .overlay').on('click', function() {
$('.mobileMenu, .overlay').toggleClass('open');
});
});
@media (max-width: 992px) {
.mobileMenu {
transform: translateX(-100%);
position: fixed;
top: 0px;
bottom: 0;
margin: auto;
left: 0;
transition: all ease 0.25s;
&.open {
transform: translateX(0%);
}
.navbar-nav {
overflow-y: auto;
}
}
.overlay {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
background-color: rgba(0, 0, 0, 0.5);
display: none;
&.open {
display: block;
z-index: 1029;
}
}
}
<!-- 在此处插入您的HTML代码 -->
英文:
From what I am able to understand you want the navbar to cover the entire screen by hiding the content.
In bootstrap 5 there is offcanvas component for this functionality.
To implement similar functionality in bootstrap 4 you would need to use jQuery.
Here is an to use off-canvas functionality in bootstrap 4.
Hope this helps.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
var fixHeight = function() {
$('.navbar-nav').css(
'max-height',
document.documentElement.clientHeight - 150
);
};
fixHeight();
$(window).resize(function() {
fixHeight();
});
$('.navbar .navbar-toggler').on('click', function() {
fixHeight();
});
$('.navbar-toggler, .overlay').on('click', function() {
$('.mobileMenu, .overlay').toggleClass('open');
});
});
<!-- language: lang-css -->
@media (max-width: 992px) {
.mobileMenu {
transform: translateX(-100%);
position: fixed;
top: 0px;
bottom: 0;
margin: auto;
left: 0;
transition: all ease 0.25s;
&.open {
transform: translateX(0%);
}
.navbar-nav {
overflow-y: auto;
}
}
.overlay {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
background-color: rgba(0, 0, 0, 0.5);
display: none;
&.open {
display: block;
z-index: 1029;
}
}
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Off-canvas Sidebar Navigation For Bootstrap 4 Example</title>
<link href="https://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container-fluid">
<a class="navbar-brand order-1 order-lg-0 ml-lg-0 ml-2 mr-auto" href="#">jQueryScript</a>
<button class="navbar-toggler align-self-start" type="button">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse bg-dark p-3 p-lg-0 mt-5 mt-lg-0 d-flex flex-column flex-lg-row flex-xl-row justify-content-lg-end mobileMenu" id="navbarSupportedContent">
<ul class="navbar-nav align-self-stretch">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0 align-self-stretch">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" />
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">
Search
</button>
</form>
</div>
</div>
</nav>
<div class="overlay"></div>
<div class="container py-4 my-5">
<div class="row">
<div class="col-md-12">
<h1>Off-canvas Sidebar Navigation For Bootstrap 4</h1>
<div id="carbon-block" style="margin:50px auto"></div>
<div style="margin:50px auto">
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.bundle.min.js" integrity="sha256-OUFW7hFO0/r5aEGTQOz9F/aXQOt+TwqI1Z4fbVvww04=" crossorigin="anonymous"></script>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论