因此,我認爲最好的辦法是實際使用粘性CSS位置與stickyfill填充工具沿着這一點,因爲你可以那麼就非常簡單地使用CSS來定製這個移動和你也不必擔心你的頭大小,它會爲你停止頁腳爲好。
你可以閱讀粘這裏https://www.sitepoint.com/css-position-sticky-introduction-polyfills/
CSS位置,這樣我有一點點時間對我的漢我想我會把一些東西放在一起。我想我只是整個頁面,所以你可以複製和粘貼它看到它的工作。我只是鏈接到cdn的bootstrap,jquery和stickyfill polyfill,但你可以去url並複製它們,以便你可以在你的本地服務器上運行它們。
這是它應該等同於你的頁面有一些不同複製並粘貼整個頁面,看看它是如何工作的,那麼你可以自定義自己的喜好:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<style>
body{padding-top:50px; overflow-x:hidden;}
#masthead{
min-height:200px;
padding:40px 0;
}
footer{
height:1000px;
background:#efefef;
margin-top:40px;
}
@media screen and (min-width:768px){
#sidebar-wrapper{
position: -webkit-sticky;
position: sticky;
top: 70px;
/*the following gives a smoother scrooll in webkit broswers*/
-webkit-backface-visibility:hidden;
backface-visibility:hidden;
-webkit-transform: translateZ(0);
transform: translateZ(0);
}
.sidebar-toggle{display:none; }
}
@media screen and (max-width:768px){
#sidebar{
position:fixed;
top:20px;bottom:20px;right:-90%;left:auto;
width:90%;
background:#fff;
padding:20px;
-ms-transition: all 500ms ease-in-out;
-webkit-transition: all 500ms ease-in-out;
-moz-transition: all 500ms ease-in-out;
-o-transition: all 500ms ease-in-out;
transition: all 500ms ease-in-out;
z-index:99999;
/*the following gives a smoother scrooll in webkit broswers*/
-webkit-backface-visibility:hidden;
backface-visibility:hidden;
-webkit-transform: translateZ(0);
transform: translateZ(0);
}
.sidebar-open #sidebar{
right:0;
}
.sidebar-open .sidebar-overlay{
position:fixed;
top:0;left:0;
width:100%;
height:100%;
background:rgba(0,0,0,0.7);
z-index:9999;
}
#sidebar .sidebar-toggle{
width:100%;
height:50px;
border:none;
background:#efefef;
outline:0;
font-weight:bold;
}
}
</style>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="banner">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/" class="navbar-brand">Bootply</a>
</div>
<nav class="collapse navbar-collapse" role="navigation">
<ul class="nav pull-left">
<button type="button" class="btn btn-primary navbar-btn" data-toggle="modal" data-target="#mylinks">Links</button>
<button type="button" class="btn btn-danger navbar-btn" data-toggle="modal" data-target="#myModal">Open Small</button>
<button type="button" class="btn btn-primary btn-md navbar-btn" data-toggle="modal" data-target="#myModal">Open Small</button>
<button type="button" class="btn btn-danger btn-md navbar-btn" data-toggle="modal" data-target="#myModal">Open Small</button>
</ul>
<ul class="nav navbar-nav pull-right">
<li>
<a href="#sec">Get Started</a>
</li>
<li>
<a href="#sec">Edit</a>
</li>
<li>
<a href="#sec">Visualize</a>
</li>
<li>
<a href="#sec">Prototype</a>
</li>
</ul>
</nav>
</div>
</nav>
<div id="masthead">
<div class="container">
<div class="row">
<div class="col-md-7">
<h1>Bootstrap Sidebar
<p class="lead">With Css Sticky and Off Canvas</p>
</h1>
</div>
<div class="col-md-5">
<div class="well well-lg">
<div class="row">
<div class="col-sm-6">
<img src="http://placehold.it/180x100" class="img-responsive">
</div>
<div class="col-sm-6">
nothing here
</div>
</div>
</div>
</div>
</div>
</div><!--/container-->
</div><!--/masthead-->
<div class="container">
<div class="row">
<div class="col-sm-9">
<button type="button" class="btn btn-primary sidebar-toggle">Open Sidebar</button>
<h2 id="sec0">Content</h2>
<p>
At Bootply we like to build simple Bootstrap templates that utilize the code Bootstap CSS without a lot of customization. Sure you can
find a lot of Bootstrap themes and inspiration, but these templates tend to be heavy on customization.</p>
<hr>
<p>
Rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores
eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit,
sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
Rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores
eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit,
sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut!</p>
<h2 id="sec1">Content</h2>
<p>
Rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut.
</p>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading"><h3>Hello.</h3></div>
<div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pharetra varius quam sit amet vulputate.
Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. Aenean sit amet felis
dolor, in sagittis nisi. Sed ac orci quis tortor imperdiet venenatis. Duis elementum auctor accumsan.
Aliquam in felis sit amet augue.
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading"><h3>Hello.</h3></div>
<div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pharetra varius quam sit amet vulputate.
Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. Aenean sit amet felis
dolor, in sagittis nisi. Sed ac orci quis tortor imperdiet venenatis. Duis elementum auctor accumsan.
Aliquam in felis sit amet augue.
</div>
</div>
</div>
</div>
<hr>
<h2 id="sec2">Section 2</h2>
<p>
Rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores
eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit,
sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut!
</p>
<div class="row">
<div class="col-md-4"><img src="http://placehold.it/300x300" class="img-responsive"></div>
<div class="col-md-4"><img src="http://placehold.it/300x300" class="img-responsive"></div>
<div class="col-md-4"><img src="http://placehold.it/300x300" class="img-responsive"></div>
</div>
<hr>
<h2 id="sec3">Section 3</h2>
<p>
Images are responsive sed @mdo but sum are more fun peratis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores
eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit,
sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
</p>
<p>
Fos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit,
sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
</p>
<p>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores
eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit,
sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
</p>
<h2 id="sec4">Section 4</h2>
<p>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores
eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit,
sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
</p>
<p>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores
eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit,
sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
</p>
</div>
<div class="sidebar-overlay"></div>
<div class="col-sm-3" id="sidebar-wrapper">
<ul class="nav nav-pills nav-stacked" id="sidebar">
<button class="sidebar-toggle">Close Sidebar</button>
<li class="active"><a href="#sec0">Section 0</a></li>
<li><a href="#sec1">Section 1</a></li>
<li><a href="#sec2">Section 2</a></li>
<li><a href="#sec3">Section 3</a></li>
<li><a href="#sec4">Section 4</a></li>
</ul>
</div><!--/left-->
</div>
</div>
<footer></footer>
<!-- JavaScripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stickyfill/1.1.4/stickyfill.min.js"></script>
<script>
$(document).ready(function(){
$('#sidebar-wrapper').Stickyfill();
});
$('body').scrollspy({ target: '#sidebar-wrapper', offset:70});
$('#sidebar a').click(function() {
if($('body').hasClass("sidebar-open")){
$('body').removeClass("sidebar-open");
}
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') || location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top- 50
}, 1000);
return false;
}
}
});
$('.sidebar-toggle, .sidebar-overlay').click(function(){
$('body').toggleClass("sidebar-open");
});
</script>
</body>
</html>
首先,我用jQuery來切換側欄開放類的身體。這將允許您在單擊邊欄切換按鈕時自定義邊欄和覆蓋圖。然後,在較大的屏幕上,您可以使用位置粘貼到側邊欄包裝,這將使您在滾動時遵循。然後,polyfill將照顧不支持css位置粘滯的瀏覽器。
你可以找到和了解這裏https://github.com/wilddeer/stickyfill
您需要發佈您的代碼在你的問題中填充工具,否則一旦問題得到解決的問題沒有什麼價值。另外,您應該先檢查控制檯是否有錯誤。 – vanburen