我的代码中已经有了粘贴标题。我试图在左侧增加一个粘性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>
&#13;
答案 0 :(得分:1)
当您的左侧边栏获得position: fixed
时,它将从页面流中取出,这意味着主要内容会移动以取代它。
解决此问题的方法是在滚动时为主要内容添加边距。边距应等于侧边栏占用的空间量(33.333%)。
你没有用jQuery标记你的问题,但是如果你对它开放,这是一种可能的方法。您可能需要使用媒体查询来调整数字。
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>