Left side div is not aligning properly in bootstra

2020-05-10 00:00发布

问题:

I have already sticky header in my code. And I am trying to add one more sticky div in the left side. Initially it is looking fine. When I try to scroll the content, layout is changing. Also at minimum size (mobile size) left side div is not aligning properly. Please suggest.

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>

回答1:

When your left sidebar gets position: fixed it is taken out of the flow of the page, which means the main content moves over to take its place.

A way to solve this is by adding a margin to the main content when you scroll. The margin should be equal to the amount of space occupied by the sidebar (33.333%).

You haven't tagged your question with jQuery, but if you are open to it here is a possible approach. You may have to tweak the numbers using media queries.

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
  });
});

$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  if (scroll > 0) {
    $("#content").addClass("fix");
  } else {
    $("#content").removeClass("fix");
  }
});
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;
}

#content.fix {
  margin-left: 33.333333%;
}
<!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" id="content" 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>