【问题标题】:Inline search bar on bootstrap引导程序上的内联搜索栏
【发布时间】:2015-09-03 16:10:52
【问题描述】:

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

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

<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>

我忘了提一下,由于搜索按钮隐藏了文本框,我也无法在任何地方发布此表单。我能做些什么来解决这个问题?

【问题讨论】:

  • 你能做一个 Plunkr 或 JSFiddle 吗?帮助你会容易得多。
  • @EricMitjans jsfiddle.net/egp6fLog
  • 我不完全理解这个问题。 PC的视图可以吗?只有手机有问题吗?在 jsfiddle 中我没有看到任何问题,按钮在视图中切换搜索框并关闭视图。它被定位为第一个菜单项。你到底想完成什么?
  • @EricMitjans 请看看我提供的图片。移动设备很好,但是当在桌面上并按下搜索按钮时,菜单链接会被推到右侧。我希望搜索栏看起来覆盖所有链接,而不是向右推送它们。
  • @EricMitjans 您还可以看到搜索栏图标已经将链接推向了右侧。我怎样才能阻止这种情况?

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

这可能会有所帮助:搜索栏将显示在点击链接上,并且该按钮将在所有视口中保持显示,而不是在您低于 768 像素时隐藏。见例子。

$(".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();
});
/**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;
  }
}
<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>

【讨论】:

  • 嗨,我需要将链接固定在导航栏的中心,我尝试了一种方法,但我认为这就是我的链接没有被覆盖的原因
  • 嗨,这成功了!但是我有一个小问题,我在页面上的 li 元素在移动设备和桌面设备中覆盖了下拉菜单。此外,我一直在尝试使用“slow”,function(){//动画完成来使切换淡出。但这似乎不起作用,并且将表单的宽度设置为 100% 会覆盖导航上的所有链接吗?
  • 这就是我个人的做法。查看更新的示例。
  • 这正是我所追求的!但是我注意到菜单似乎不适用于移动设备。这是因为我用来引导的链接吗?
  • 这取决于你的依赖顺序。
【解决方案2】:

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

你可以创建一个css类:

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

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

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

jsfiddle updated

【讨论】:

  • 嗨,我刚刚在我的项目文件上试过这个,但似乎没有用。然而,小提琴确实如此。
  • 我需要链接在菜单中居中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多