简体   繁体   English

Bootstrap 5:我需要做什么才能在导航栏中有一个搜索图标,当我单击它时,它会在导航栏下方显示一个搜索框?

[英]Bootstrap 5: What I need to do to have a search icon in navbar that when I click it, it will show a search box under the navbar?

This is what I have done so far for the navbar这是我到目前为止为导航栏所做的

I need to put a search icon beside log in button.我需要在登录按钮旁边放一个搜索图标。 When I click the search icon, a search box will appear under the navbar that look something like in the picture below.当我单击搜索图标时,导航栏下方会出现一个搜索框,如下图所示。 My navbar is transparent, position is relative and the header beneath is a video loop with autoplay, position is relative.我的导航栏是透明的,position 是相对的,下面的 header 是带有自动播放功能的视频循环,position 是相对的。

Taken from DataCamp website取自 DataCamp 网站

I have tried free templates codes from other sites but it will give weird results when combined with my existing code and I have tried to change the template codes, still negative results.我尝试过来自其他网站的免费模板代码,但是当与我现有的代码结合使用时会产生奇怪的结果,并且我尝试更改模板代码,仍然是负面结果。 If possible I want to minimize the usage of js and css so that I can tweak the look easily without hassling too much.如果可能的话,我想尽量减少 js 和 css 的使用,这样我就可以轻松地调整外观而不会太麻烦。 Thank you for the help.感谢您的帮助。

You need a flag shouldShowSearchInput .您需要一个标志shouldShowSearchInput By default, it should be false.默认情况下,它应该是假的。

You can use it to change display of Search Input Element to be none or block您可以使用它将Search Input Elementdisplay更改为noneblock

When you click SearchIcon changes the value of that flag.当您单击SearchIcon时,会更改该标志的值。

You just need to position the search box under the navbar.您只需要 position 导航栏下的搜索框。 Then add a class that will make it visible and add and event listener to the button that toggles that class on and off.然后添加一个 class 使其可见,并将事件侦听器添加到用于打开和关闭 class 的按钮。

See snippet below请参阅下面的片段

 document.querySelector(`#btn-search`).addEventListener(`click`, function() { document.querySelector(`#search`).classList.toggle(`show-search`); });
 #search { bottom: 0; right: 0; width: 25%; z-index: -1; transition: transform 500ms; position: absolute; }.show-search { transform: translate(0, 100%); }
 <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.1/css/bootstrap.min.css' integrity='sha512-siwe/oXMhSjGCwLn+scraPOWrJxHlUgMBMZXdPe2Tnk3I0x3ESCoLz7WZ5NTH6SZrywMY+PB1cjyqJ5jAluCOg==' crossorigin='anonymous' /> <nav class="navbar navbar-dark navbar-expand bg-dark py-3 position-relative"> <div class="container"> <ul class="navbar-nav me-auto"> <li class="nav-item"> <a class="nav-link active" href="#">First Item</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Second Item</a> </li> </ul> <div class="d-block"> <input type="search" id="search" class="form-control position-absolute shadow-sm" placeholder="Search"> <a class="btn btn-light me-2" role="button" id="btn-search" href="#"></a> <a class="btn btn-primary" role="button" href="#">Login</a> </div> </div> </nav>

You could also use the off-canvas component baked into bootstrap: https://getbootstrap.com/docs/5.2/components/offcanvas/您还可以使用烘焙到引导程序中的非画布组件: https://getbootstrap.com/docs/5.2/components/offcanvas/

See off canvas snippet below请参阅下面的 canvas 片段

 <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.1/css/bootstrap.min.css' integrity='sha512-siwe/oXMhSjGCwLn+scraPOWrJxHlUgMBMZXdPe2Tnk3I0x3ESCoLz7WZ5NTH6SZrywMY+PB1cjyqJ5jAluCOg==' crossorigin='anonymous' /> <nav class="navbar navbar-dark navbar-expand bg-dark py-3 position-relative"> <div class="container"> <ul class="navbar-nav me-auto"> <li class="nav-item"> <a class="nav-link active" href="#">First Item</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Second Item</a> </li> </ul> <div class="d-block"> <button class="btn btn-light" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight"></button> <a class="btn btn-primary" role="button" href="#">Login</a> </div> </div> </nav> <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasRight"> <div class="offcanvas-header"> <h5 class="offcanvas-title" id="offcanvasRightLabel">Offcanvas right</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas"></button> </div> <div class="offcanvas-body"> <input type="search" id="search" class="form-control shadow-sm" placeholder="Search"> </div> </div> <script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.1/js/bootstrap.bundle.min.js' integrity='sha512-1TK4hjCY5+E9H3r5+05bEGbKGyK506WaDPfPe1s/ihwRjr6OtL43zJLzOFQ+/zciONEd+sp7LwrfOCnyukPSsg==' crossorigin='anonymous'></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击导航栏按钮时,我的搜索栏宽度正在改变 - My search bar width is changing when i click navbar button Bootstrap导航栏搜索按钮 - Bootstrap navbar search button 当我单击导航栏中的按钮或其他按钮时,它会将我重定向到一个名为 undefined 的页面,但我想要做的是打开一个引导模式 - When I click a button in navbar or other button it redirects me to a page called undefined but what I want to do is open a bootstrap modal 单击图标时如何弹出导航栏 - How do I make a navbar pop up when I click on an icon 如何使用搜索栏过滤我的bootstrap 4导航栏链接? - How can I filter my bootstrap 4 navbar links with a search bar? 为什么单击菜单图标时我的正常导航栏消失了 - Why is my normal navbar disappearing when I click on the menu icon 如何通过点击关闭我的twitter bootstrap导航栏? - How do I get my twitter bootstrap navbar to close on click? 我有一个粘性导航栏(搜索栏),但是当您向下滚动时它仍然与页面内容重叠 - I have a sticky navbar (search bar) but it still overlaps with the contents of the page when you scroll down 如何在Bootstrap 4的Navbar中添加切换全角搜索框? - How to add toggle full width search box to the Navbar in Bootstrap 4? 单击<a>链接</a>时如何关闭导航栏<a>?</a> - How do i get the navbar to close when i click on a <a> link?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM