bootstrap css searchbox不适合导航栏

时间:2015-12-25 10:49:22

标签: html css twitter-bootstrap

在我的jsp页面中,我想将searchbox放在导航栏项目中。 所以我已经完成了以下代码,点击链接(jsfiddle代码)。

jsfiddlecode link

但问题是,当它处于移动视图中时,搜索栏不适合导航栏itmes。为什么它不适合导航栏?

2 个答案:

答案 0 :(得分:0)

您可以将按钮和搜索表单放在navbar-header div中,以便在单击切换按钮时不会与移动设备navbar混在一起,因为它当前包含在导航链接中。然后你只需要调整切换按钮的位置。

*我还调整了您的navbar-nav,因为它在某个宽度上与搜索按钮重叠。

请参阅工作示例摘录。

$('#buttonsearch').click(function() {
  $('#formsearch').slideToggle("fast", function() {
    $('#content').toggleClass("moremargin");
  });
  $('#searchbox').focus()
  $('.openclosesearch').toggle();
});
body {
  padding-top: 70px;
}
.searchbardiv {
  display: block;
  position: fixed;
  background: #7D1935;
  top: 50px;
  right: 0px;
  width: 100%;
  max-width: 100%;
  padding: 10px;
  margin: 0px;
}
#formsearch {
  display: none;
}
.buttonsearch {
  position: fixed;
  color: white;
  top: 15px;
  right: 10px;
  background-color: transparent;
  border: 0px;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
}
.buttonsearch:hover {
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  border: 0px;
}
.buttonsearch:focus {
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  outline: none;
  border: 0px;
}
.glyphicon.glyphicon-search {
  font-size: 18px;
}
#searchbox {
  box-shadow: none;
  padding: 8px 14px;
}
#searchbox:hover {
  box-shadow: none;
}
.form-control:focus {
  border-color: #ccc;
}
@media (min-width: 768px) {
  .navbar .navbar-nav {
    margin-right: 10px;
  }
}
@media (max-width: 767px) {
  .navbar .navbar-toggle {
    margin-right: 50px;
  }
}
<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.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">MyCompany</a>
      <button type="button" class="buttonsearch" id="buttonsearch">
        <i class="glyphicon glyphicon-search openclosesearch"></i>
        <i class="glyphicon glyphicon-remove openclosesearch" style="display:none"></i>
      </button>
      <form role="search" method="get" class="searchbardiv" id="formsearch">
        <div class="input-group">
          <input type="text" id="searchbox" class="form-control" name="s" id="s">
          <div class="input-group-btn">
            <button class="btn btn-default" id="searchsubmit" type="submit">
              <strong>Search</strong>
            </button>
          </div>
        </div>
      </form>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav navbar-right">
        <li class="active">
          <a href="home.html"> Home</a>
        </li>
        <li>
          <a href="home.html"> link1</a>
        </li>
      </ul>
    </div>
  </div>
</nav>
<div class="container">
  <div class="well">
    Content
  </div>
</div>

答案 1 :(得分:-1)

您可以为手机编写媒体css ..只需添加

   @media (max-width: 767px) { 
#buttonsearch{
  margin-right:20%;
 }
}

它将对景观手机,手机平板电脑进行保证 -