引导程序上的内联搜索栏

时间:2015-09-02 10:20:25

标签: javascript jquery html css twitter-bootstrap

我正在尝试在导航菜单的左侧添加一个内嵌搜索栏。它是一个按钮,在其旁边显示一个文本框,以允许用户输入他们想要的帽子。我试图将其定位在线但由于某种原因它出现在较小设备上的菜单图标的顶部。

我也想知道如何让文本框淡入和淡出菜单元素而不是它们出现在它们旁边。

<nav class="navbar navbar-default" role="navigation">
  <div class="container-fluid">
     <div class="navbar-header">

        <form class="navbar-form navbar-left" role="search">

            <button id='search-button' class='btn btn-default '><span class='glyphicon glyphicon-search'></span></button>

            <div id='search-form' class="form-group">
              <div class="input-group">
                <span id='search-icon' class="input-group-addon"><span class='glyphicon glyphicon-search'></span></span>
                <input type="text" class="form-control" placeholder="Search">
              </div>
            </div>
          </form>

          <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
          </button>
    </div>


  </div>
</nav>
<script type='text/javascript'>

      $(function(){

         $("#search-button, #search-icon").click(function(e){
             e.preventDefault();
             $("#search-button, #search-form").toggle();
         });
      })  
</script>

我忘了提及,我也无法在任何地方发布此表单,因为搜索按钮会隐藏文本框。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望搜索栏覆盖链接。

您可以创建一个css类:

   .fix-menu {
    position: absolute;
    z-index: -1;
    }

并将其添加到您的链接列表中:

    <ul class="nav navbar-nav fix-menu">

jsfiddle updated

答案 1 :(得分:0)

这可能会有所帮助:搜索栏会在点击时显示在链接上,按钮将在所有视口中保持显示,而不是在您使用768px后隐藏。见例。

&#13;
&#13;
$(".btn-search").click(function(e) {
  $("#search-input, #btn-go").fadeToggle();
});
$(".hasclear").keyup(function() {
  var t = $(this);
  t.next('span').toggle(Boolean(t.val()));
});

$(".clearer").hide($(this).prev('input').val());

$(".clearer").click(function() {
  $(this).prev('input').val('').focus();
  $(this).hide();
});
&#13;
/**Custom**/

body,
html {
  margin-top: 60px;
}
.navbar.navbar-custom {
  height: 50px;
  border: none;
}
.navbar.navbar-custom .navbar-toggle {
  z-index: 1;
  border: none;
  background: none;
}
.navbar.navbar-custom .navbar-collapse {
  background: #f5f5f5;
}
.formSearch {
  left: 0;
  position: absolute;
}
.formSearch .has-feedback span.glyphicon {
  padding: 7px;
  margin-right: 10px;
  cursor: pointer;
  pointer-events: auto;
  color: #444;
}
.btn.btn-search,
.btn.btn-search:hover,
.btn.btn-search:focus {
  background: none;
  height: 50px;
  box-shadow: none;
  outline: none;
  border-radius: 0;
  border: none;
}
#search-input,
#search-input:focus {
  height: 50px;
  background: #f5f5f5;
  box-shadow: none;
  outline: none;
  font-size: 20px;
  border: none;
  display: none;
}
.input-group-btn #btn-go {
  height: 50px;
  border-radius: 0;
  border: none;
  box-shadow: none;
  outline: none;
  background: #444;
  color: #fff;
  display: none;
}
@media (min-width: 768px) {
  .navbar-custom .navbar-nav {
    width: 100%;
    text-align: center;
  }
  .navbar-custom .navbar-nav > li {
    float: none;
    display: inline-block;
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default navbar-fixed-top navbar-custom">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>

      </button>
      <form class="formSearch" role="search"> <span class="input-group"> <span class="input-group-btn">
        <button class="btn btn-default btn-search" type="button"><span class="glyphicon glyphicon-search"></span>

        </button>
        </span>
        <div class="has-feedback">
          <input type="text" class="form-control hasclear" id="search-input" placeholder="Search for..."> <span class="clearer glyphicon glyphicon-remove form-control-feedback"></span>

        </div> <span class="input-group-btn">
        <button class="btn btn-default" id="btn-go" type="button"> Search</button>
      </span>
        </span>
    </div>
    </form>
  </div>
  <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav">
      <li><a href="#">Home</a>

      </li>
      <li><a href="#">Forums</a>

      </li>
      <li><a href="#">About Us</a>

      </li>
      <li><a href="#">Sponsorship</a>

      </li>
      <li><a href="#">Bespoke Events</a>

      </li>
      <li><a href="#">Vacancies</a>

      </li>
      <li><a href="#">Contact</a>

      </li>
    </ul>
  </div>
  <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>
<div class="container">
  <div class="well well-lg">Boostrap Search Bar</div>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer
    took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
    sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
    standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the
    printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,
    but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker
    including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled
    it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
    and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since
    the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in
    the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting
    industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic
    typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem
    Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer
    took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
    sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
    standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the
    printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,
    but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker
    including versions of Lorem Ipsum.</p>
</div>
&#13;
&#13;
&#13;