sticky div在html和bootstrap中没有正确对齐

时间:2017-04-05 14:59:22

标签: html twitter-bootstrap

我的代码中已经有了粘贴标题。我试图在左侧增加一个粘性div。最初看起来很好。当我尝试滚动内容时,布局正在发生变化。另外,在最小尺寸(移动尺寸)div没有正确坐着。

var onResize = function() {      
  $("body").css("padding-top", $(".navbar-fixed-top").height());
};   
$(window).resize(onResize);
$(function() {
  onResize();
});

$(document).ready(function () {
  $('body').scrollspy({
    target: '#mainNavbar',
    offset: 10
  });
});
html, body {
  height: 100%;
}
 body {
    padding-top: 50px;
  }
.navbar-nav {
  float: left;
  margin: 0;
}    
.navbar-nav>li {
  float: left;
}
.navbar-nav>li>a {
  padding-top: 15px;
  padding-bottom: 15px;
}
.navbar-offset {
    top: 0px;
}
.container {
  background: #ccc;
}
.affix {
  top: 20;
}
<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <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.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
</head>

<body>
    <nav class="navbar navbar-default navbar-fixed-top navbar-offset">
        <div style="font-size:30px;margin-left:30px;">Title and logo here</div>
    </nav>

    <div class="container">
      <div class="row">
        <div class="col-lg-4 col-sm-4 col-md-4" style="background-color:gray;" data-spy="affix" data-offset-top="0">
              <div style="height:280px;background-color:lightblue;margin-top:20px;"></div><br/>
            <div id="mainNavbar">            
              <ul>
                <li class="active"><a href="#divDesert">Desert</a></li>
                <li><a href="#divLighthouse">Lighthouse</a></li>
                <li><a href="#divTulips">Tulips</a></li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    Animals <span class="caret"></span>
                  </a>
                  <ul class="dropdown-menu">
                    <li><a href="#divJellyfish">Jellyfish</a></li>
                    <li><a href="#divPenguins">Penguins</a></li>
                  </ul>
                </li>
              </ul>
            </div>
        </div>
        <div class="col-lg-8 col-sm-8 col-md-8" style="background-color:lightblue;font-size:30px;">
            <div id="divDesert">
                <h1>Desert</h1>
                <p>The Affix plugin allows an element to become affixed (locked) to an area on the page.</p>
            </div>
            <div id="divLighthouse">
                <h1>Lighthouse</h1>
                <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
            </div>
            <div id="divTulips">
                <h1>Tulips</h1>
                <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.</p>
            </div>    
            <div id="divJellyfish">
                <h1>Jellyfish</h1>
                <p>But I must explain to you how all this mistaken idea of denouncing.</p>
            </div>
            <div id="divPenguins">
                <h1>Penguins</h1>
                <p>Li Europan lingues es membres del sam familie.</p>
            </div>
        </div>
      </div>
    </div>

</body>
</html>

0 个答案:

没有答案