我想要建立一個頁面,就像Facebook
或Twitter
,其中中間div滾動不定式,左或右div只滾動到其height
,然後停在那裏。向下滾動固定位置div,然後停止
在我的網站中,我也在右側顯示中間div和一些fixed
高度數據中的不定數據。我想scroll
兩個div與頁面scroll
和右div停止滾動,當它達到其高度,並停留在那裏。
我所做的是先製作position: static
,當它到達bottom
的右格時,使其成爲fixed
。但這不是我想要的,因爲當我將static
更改爲fixed
時,右div再次返回頂部。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> I am using style disple flex bcoz I dont know how to use bootstap here(On this site)</div>
<div class="col=lg-12" style="display:flex">
<div class="col-lg-3" style="position:fixed;float:left;left:0">some text</div>
<div class="col-lg-6" id="displayPost" style="float:left;left:0;right:0;text-align:center">post display using ajax<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br></div>
<div id="staticDiv" class="col-lg-3" style="height:100px;float:right;right:0">//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br></div>
</div>
JS代碼:
$(document).ready(function() {
$(window).scroll(function() {
var distanceFromTop = $(document).scrollTop();
if (distanceFromTop >= $('#staticDiv').height()) {
$('#staticDiv').css({
position: 'fixed'
});
} else {
$('#staticDiv').css({
position: 'absolute'
});
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> I am using style disple flex bcoz I dont know how to use bootstap here(On this site)</div>
<div class="col=lg-12" style="display:flex">
<div class="col-lg-3" style="position:fixed;float:left;left:0">some text</div>
<div class="col-lg-6" id="displayPost" style="float:left;left:0;right:0;text-align:center">post display using ajax<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br></div>
<div id="staticDiv" class="col-lg-3" style="height:200px;float:right;right:0">//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br>//some text<br></div>
</div>
$(document).ready(function() {
$(window).scroll(function() {
var distanceFromTop = $(document).scrollTop();
if (distanceFromTop >= $('#staticDiv').height()) {
$('#staticDiv').css({
position: 'fixed'
});
} else {
$('#staticDiv').css({
position: 'absolute'
});
}
});
});
所有我想要的是右側格留在底部,而中間格保持滾動固定。
老實說,我只想用這個庫:http://leafo.net/sticky-kit/ – Kiwad
@Kiwad thnkx烏拉圭回合迴應,我會在這個戰利品。 – Vivek