在导航栏上使切换按钮生效。

huangapple go评论83阅读模式
英文:

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.

 &lt;nav class=&quot;navbar navbar-expand-lg navbar-light bg-yellow px-5 py-3 font-weight-bold&quot; id=&quot;navbar&quot;&gt;
&lt;div class=&quot;logo d-flex flex-column&quot;&gt;
&lt;a href=&quot;index.html&quot; class=&quot;mx-auto&quot;&gt;&lt;img src=&quot;./Design/Logo.png&quot;  width=&quot;40&quot; height=&quot;40&quot; class=&quot;logo-img&quot;&gt;&lt;/a&gt;
&lt;a class=&quot;navbar-brand mx-auto font-weight-bold&quot; href=&quot;#&quot;&gt;Logo&lt;/a&gt;
&lt;/div&gt;
&lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarNav&quot; aria-controls=&quot;navbarNav&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;
&lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarNav&quot;&gt;
&lt;ul class=&quot;navbar-nav w-90 justify-content-around ml-lg-auto&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Academy&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Academy&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Academy&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Academy&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item justify-self-end&quot;&gt;
&lt;a href=&quot;./contact.html&quot; class=&quot;btn btn-danger&quot;&gt;Button&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;

enter image description here

enter image description here

This is how its supposed to look like when clicked on the dropdown button when on tablet or mobile&mdash;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:

&lt;script&gt;
function toggleNav() {
document.querySelector(&quot;#navbar&quot;).style.height = &quot;100%&quot;
document.body.overflow = &quot;hidden&quot; // added this for making the page unscrollable.
}
&lt;/script&gt;

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=&quot;toggleNav();&quot; to the button. Like this:

&lt;button onclick=&quot;toggleNav();&quot; class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarNav&quot; aria-controls=&quot;navbarNav&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;

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() {
$(&#39;.navbar-nav&#39;).css(
&#39;max-height&#39;,
document.documentElement.clientHeight - 150
);
};
fixHeight();
$(window).resize(function() {
fixHeight();
});
$(&#39;.navbar .navbar-toggler&#39;).on(&#39;click&#39;, function() {
fixHeight();
});
$(&#39;.navbar-toggler, .overlay&#39;).on(&#39;click&#39;, function() {
$(&#39;.mobileMenu, .overlay&#39;).toggleClass(&#39;open&#39;);
});
});

<!-- 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;
&amp;.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;
&amp;.open {
display: block;
z-index: 1029;
}
}
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot; /&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot; /&gt;
&lt;title&gt;Off-canvas Sidebar Navigation For Bootstrap 4 Example&lt;/title&gt;
&lt;link href=&quot;https://www.jqueryscript.net/css/jquerysctipttop.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css&quot; integrity=&quot;sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=&quot; crossorigin=&quot;anonymous&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;nav class=&quot;navbar navbar-expand-lg navbar-dark bg-dark fixed-top&quot;&gt;
&lt;div class=&quot;container-fluid&quot;&gt;
&lt;a class=&quot;navbar-brand order-1 order-lg-0 ml-lg-0 ml-2 mr-auto&quot; href=&quot;#&quot;&gt;jQueryScript&lt;/a&gt;
&lt;button class=&quot;navbar-toggler align-self-start&quot; type=&quot;button&quot;&gt;
&lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;div class=&quot;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&quot; id=&quot;navbarSupportedContent&quot;&gt;
&lt;ul class=&quot;navbar-nav align-self-stretch&quot;&gt;
&lt;li class=&quot;nav-item active&quot;&gt;
&lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Home &lt;span class=&quot;sr-only&quot;&gt;(current)&lt;/span&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item dropdown&quot;&gt;
&lt;a class=&quot;nav-link dropdown-toggle&quot; href=&quot;#&quot; id=&quot;navbarDropdown&quot; role=&quot;button&quot; data-toggle=&quot;dropdown&quot; aria-haspopup=&quot;true&quot; aria-expanded=&quot;false&quot;&gt;
Dropdown
&lt;/a&gt;
&lt;div class=&quot;dropdown-menu&quot; aria-labelledby=&quot;navbarDropdown&quot;&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Action&lt;/a&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Another action&lt;/a&gt;
&lt;div class=&quot;dropdown-divider&quot;&gt;&lt;/div&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Something else here&lt;/a&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item dropdown&quot;&gt;
&lt;a class=&quot;nav-link dropdown-toggle&quot; href=&quot;#&quot; id=&quot;navbarDropdown&quot; role=&quot;button&quot; data-toggle=&quot;dropdown&quot; aria-haspopup=&quot;true&quot; aria-expanded=&quot;false&quot;&gt;
Dropdown
&lt;/a&gt;
&lt;div class=&quot;dropdown-menu&quot; aria-labelledby=&quot;navbarDropdown&quot;&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Action&lt;/a&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Another action&lt;/a&gt;
&lt;div class=&quot;dropdown-divider&quot;&gt;&lt;/div&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Something else here&lt;/a&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link disabled&quot; href=&quot;#&quot; tabindex=&quot;-1&quot; aria-disabled=&quot;true&quot;&gt;Disabled&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;form class=&quot;form-inline my-2 my-lg-0 align-self-stretch&quot;&gt;
&lt;input class=&quot;form-control mr-sm-2&quot; type=&quot;search&quot; placeholder=&quot;Search&quot; aria-label=&quot;Search&quot; /&gt;
&lt;button class=&quot;btn btn-outline-success my-2 my-sm-0&quot; type=&quot;submit&quot;&gt;
Search
&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;div class=&quot;overlay&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;container py-4 my-5&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-md-12&quot;&gt;
&lt;h1&gt;Off-canvas Sidebar Navigation For Bootstrap 4&lt;/h1&gt;
&lt;div id=&quot;carbon-block&quot; style=&quot;margin:50px auto&quot;&gt;&lt;/div&gt;
&lt;div style=&quot;margin:50px auto&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot; integrity=&quot;sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha256-OUFW7hFO0/r5aEGTQOz9F/aXQOt+TwqI1Z4fbVvww04=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 05:14:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定