我实际上有两个问题需要帮助:
较小屏幕上的搜索栏由于某些原因从导航栏折叠中退出,我不知道为什么。
Awesomplete.js或CSS似乎覆盖了搜索栏的宽度,并将其推到左侧,我也不确定为什么。
如果有人想破解它,那就是jsfiddle:https://jsfiddle.net/x4vusam1/
.bg-steel {
/* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#09c6f9+0,045de9+100 */
/* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#1e3b70+0,29539b+100 */
background: purple;
}
.navbar-brand {
color: white !important;
}
.btn {
background-color: transparent !important;
}
.navbar-collapse {
position: relative;
padding-top: 20px !important;
max-height: 400px;
}
.navbar-collapse form[role="search"] {
position: absolute;
right: 0;
padding: 0;
margin: 0;
z-index: 9;
}
.navbar-collapse form[role="search"] button,
.navbar-collapse form[role="search"] input {
padding: 8px 12px;
border-width: 0;
box-shadow: none;
outline: none;
}
.navbar-collapse form[role="search"] input {
padding: 16px 12px;
font-size: 14pt;
font-style: italic;
color: rgb(160, 160, 160);
box-shadow: none;
}
.navbar-collapse form[role="search"] button[type="reset"] {
display: none;
}
@media (min-width: 768px) {
.navbar-collapse {
padding-top: 0 !important;
padding-right: 38px !important;
}
.navbar-collapse form[role="search"] {
width: 38px;
}
.navbar-collapse form[role="search"] input {
font-size: 15pt;
opacity: 0;
display: none;
}
.navbar-collapse form[role="search"].active {
width: 100% !important;
}
.navbar-collapse form[role="search"].active button,
.navbar-collapse form[role="search"].active input {
display: table-cell;
opacity: 1;
}
}
.awesomplete [hidden] {
display: none;
}
.awesomplete .visually-hidden {
position: absolute;
clip: rect(0, 0, 0, 0);
}
.awesomplete {
display: inline-block;
position: relative;
}
.awesomplete > input {
display: block;
}
答案 0 :(得分:1)
因此,我对您的代码执行了以下操作:
position: absolute
上的.navbar-collapse form[role="search"]
来将其放置到 navbar 中-而是使用justify-content-end
到#navbarTogglerDemo02
元素。justify-content-end
添加input-group
类,以将放置在右侧的搜索栏中。 请参见下面的演示和updated fiddle
:
$(function() {
$('body, .navbar-collapse form[role="search"] button[type="reset"]').on('click keyup', function(event) {
//console.log(event.currentTarget);
if (event.which === 27 && $('.navbar-collapse form[role="search"]').hasClass('active') ||
$(event.currentTarget).attr('type') === 'reset') {
closeSearch();
}
});
function closeSearch() {
let $form = $('.navbar-collapse form[role="search"].active');
$form.find('input').val('');
$form.removeClass('active');
}
$(document).on('click', '.navbar-collapse form[role="search"]:not(.active) button[type="submit"]', function(event) {
event.preventDefault();
let $form = $(this).closest('form'),
$input = $form.find('input');
$form.addClass('active');
$input.focus();
});
});
var aweInput = new Awesomplete(search);
search.addEventListener('awesomplete-select', function(e) {
var url = e.text.value; // The value associated with the selection
// Some optional actions:
e.preventDefault(); // Prevent the URL from appearing in the input box
e.target.value = e.text.label; // Set the value to the selected label
aweInput.close(); // close the drop-down
window.location.href = url;
});
.bg-steel {
/* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#09c6f9+0,045de9+100 */
/* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#1e3b70+0,29539b+100 */
background: purple;
}
.navbar-brand {
color: white !important;
}
.btn {
background-color: transparent !important;
}
.navbar-collapse {
position: relative;
padding-top: 20px !important;
max-height: 400px;
}
.navbar-collapse form[role="search"] {
/* position: absolute; */
right: 0;
padding: 0;
margin: 0;
z-index: 9;
}
.navbar-collapse form[role="search"] button,
.navbar-collapse form[role="search"] input {
padding: 8px 12px;
border-width: 0;
box-shadow: none;
outline: none;
}
.navbar-collapse form[role="search"] input {
padding: 16px 12px;
font-size: 14pt;
font-style: italic;
color: rgb(160, 160, 160);
box-shadow: none;
}
.navbar-collapse form[role="search"] button[type="reset"] {
display: none;
}
@media (min-width: 768px) {
.navbar-collapse {
padding-top: 0 !important;
padding-right: 38px !important;
}
.navbar-collapse form[role="search"] {
width: 38px;
}
.navbar-collapse form[role="search"] input {
font-size: 15pt;
opacity: 0;
display: none;
}
.navbar-collapse form[role="search"].active {
width: 100% !important;
}
.navbar-collapse form[role="search"].active button,
.navbar-collapse form[role="search"].active input {
display: table-cell;
opacity: 1;
}
}
.awesomplete [hidden] {
display: none;
}
.awesomplete .visually-hidden {
position: absolute;
clip: rect(0, 0, 0, 0);
}
.awesomplete {
display: inline-block;
position: relative;
}
.awesomplete>input {
display: block;
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/awesomplete/1.1.4/awesomplete.js"></script>
<nav class="navbar fixed-top navbar-light bg-steel navbar-expand-md" role="navigation">
<div class="container">
<a class="navbar-brand" href="{% url 'main-home' %}">What's The Update?</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarTogglerDemo02"> <!-- ADDED class -->
<form class="d-flex" role="search">
<div class="input-group justify-content-end"> <!-- ADDED class -->
<input class="form-control awesomplete" id="search" type="text" list="games" placeholder="Search games..." />
<datalist id="games">
<option value="Google.com">Google</option>
<option value="Yahoo.com">Yahoo</option>
<option value="Wikipedia.org">Wikipedia</option>
<option value="Bing.com">Bing</option>
</datalist>
<div class="input-group-append">
<button type="reset" class="btn">
<span class="far fa-times-circle">
<span class="sr-only">Close</span>
</span>
</button>
</div>
<div class="input-group-append">
<button type="submit" class="btn btn-light">
<span class="fas fa-search">
<span class="sr-only">Search</span>
</span>
</button>
</div>
</div>
</form>
</div>
</div>
</nav>
答案 1 :(得分:0)
从class="d-flex"
// form <form class="d-flex" role="search"> // remove the form
<div class="input-group">
// your content
</div>
// </form>