首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用.affix时,元素宽度错误

使用.affix时,元素宽度错误
EN

Stack Overflow用户
提问于 2017-08-09 07:30:48
回答 1查看 72关注 0票数 0

在此example中,您可以看到,当我们向下滚动时,搜索字段显示较大的宽度。如果我删除标签(带有搜索标志图标)-一切正常。我在其他标签上也遇到了同样的问题,比如,还有其他元素类,比如.panel-group、.panel-heading和e.t.c。我怎么才能修复它?

HTML代码:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2017-08-09 07:34:16

解决这个问题的一种方法是添加left: 15px; right: 15px;来匹配父col的填充,这就是当元素没有被附加时控制宽度的东西。

代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45579484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档