[英]Scrolling floating/sliding sidebar issue
我已經瀏覽了與我所面臨的內容相關的所有內容,但仍無法解決。
我正在嘗試做的是:
我得到的是:
這是腳本(改編自慷慨的喬登·米爾斯):
<script type="text/javascript">
function animate_box() {
var offset = -15; /* set this to the starting margin-top in the css */
var element = document.getElementById('animate_box');
if(element) {
var top = Number(String(element.style.marginTop).substring(0,String(element.style.marginTop).indexOf('px')));
try {
if(document.body.scrollTop > 500) {
var difference = (document.body.scrollTop + offset);
} else if(document.documentElement.scrollTop > 0) {
var difference = (document.documentElement.scrollTop + offset);
} else {
var difference = offset;
}
} catch(e) {
var difference = offset;
}
difference = difference - top;
if(difference > 200) {
element.style.marginTop = (top + Math.abs(Math.ceil(difference / 30))) + 'px';
} else if(difference < 190) {
element.style.marginTop = (top - Math.abs(Math.ceil(difference / 30))) + 'px';
}
}
}
window.setInterval(animate_box, 50);
</script>
我建議使用另一種方法:
像這樣:
$(function() {
var backup_position_toolbar = $('#sidebar').offset().top;
$(window).scroll(function() {
if ( $('#sidebar').offset().top - $(window).scrollTop() < 0) $('#sidebar').addClass('fixed');
if ( $(window).scrollTop() < backup_position_toolbar ) $('#sidebar').removeClass('fixed');
});
});
請注意,我使用的“固定”類定義如下:.fixed {position:fixed; 最高:0; }
這使菜單更加可用。 如果要在某個點上阻止側欄,則可以添加更多的邏輯(即,當底部太近時)。 這樣,您無需設置數值(500、200等)。
但是,如果您不費吹灰之力就嘗試使用帶“綴”的引導程序(請看左側菜單,這就是您想要的) http://twitter.github.com/bootstrap/javascript.html#affix
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.