html - 使用 .affix 时元素宽度错误

标签 html css twitter-bootstrap

在此example你可以看到,当我们向下滚动时,搜索字段的宽度变大了。如果我删除标签(带有搜索字形图标) - 一切正常。我对其他标签(如 )和其他元素类(如 .panel-group、.panel-heading 和 e.t.c)也遇到了同样的问题。我该如何解决?

HTML代码:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
  .affix {
      top: 20px;
}
</style>

<body>
<div class="container-fluid" style="background-color:#2196F3;color:#fff;height:200px;">
  <h1>Bootstrap Affix Example</h1>
</div>
<br>

<div class="container">
  <div class="row">
    <nav class="col-sm-3">
      <div class="nav nav-pills nav-stacked" data-spy="affix" data-offset-top="205">
        <div class="input-group">
            <i class="input-group-addon glyphicon glyphicon-search"></i>
            <input name="search" type="text" class="form-control" placeholder="search">
        </div>
        <li>TEST 1</li>
        <li>TEST 2</li>
        <li>TEST 3</li>
      </div>
    </nav>
    <div class="col-sm-9">   
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
    </div>
  </div>
</div>

</body>

最佳答案

解决它的一种方法是添加 left: 15px; right: 15px; 以匹配父 col 的 padding,这是在元素未附加时控制宽度的内容。

<title>Bootstrap Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
  .affix {
    top: 20px;
    left: 15px;
    right: 15px;
  }
</style>

<body>

  <div class="container-fluid" style="background-color:#2196F3;color:#fff;height:200px;">
    <h1>Bootstrap Affix Example</h1>
    <h3>Fixed (sticky) vertical sidenav on scroll</h3>
    <p>Scroll this page to see how the left navigation menu behaves with data-spy="affix".</p>
    <p><strong>The left menu sticks to the page when you have scrolled a specified amount of pixels.</strong></p>
  </div>
  <br>

  <div class="container">
    <div class="row">
      <nav class="col-sm-3">
        <div class="nav nav-pills nav-stacked" data-spy="affix" data-offset-top="205">
          <div class="input-group">
            <i class="input-group-addon glyphicon glyphicon-search"></i>
            <input name="search" type="text" class="form-control" placeholder="search">
          </div>
          <li>TEST 1</li>
          <li>TEST 2</li>
          <li>TEST 3</li>
        </div>
      </nav>
      <div class="col-sm-9">
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
      </div>
    </div>
  </div>

</body>

</html>

关于html - 使用 .affix 时元素宽度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45579484/

相关文章:

html - 如何使绿色背景颜色占整个 div 的一半?

html - 如何在面板中包含实时日期和时钟?

javascript - bootstrap 中的按钮不起作用

javascript - Twitter bootstrap 通过 Javascript in rails 管理多个单选按钮

css - 白色边框没有出现

html - CSS 导航栏内容缩放

html - 下拉菜单不会样式

javascript - 单击时一次仅顺序显示一个 div

javascript - 带有 css 和 javascript 的 HTML 背景

javascript - 在 HTML header 或正文中包含 &lt;script&gt; 标记有什么区别